0

2つのテキストボックス、2つのラベル、およびログインボタンを備えたユーザーフォームがあります。

私のExcelシートには、ID、名前、ピン、および残高を含む一種のデータベースがあります。

問題は、ログインボタンをクリックするたびに、IDテキストボックスの値が0にリセットされることですが、PINテキストボックスは正常に機能します。

完全なコードを貼り付けます:

Dim ID As Integer
Dim PIN As Integer
Dim PINField As String
Dim Balance As Double
Dim Attempts As Integer
Dim BalanceField As String


Private Sub btnLogin_Click()
    txtID.Text = ID
    Call SetId
    Call Authenticate
End Sub

Sub Authenticate()
    If txtPin.Text = PIN Then
        Call Welcome
    ElseIf Attempts > 3 Then
        Call Bye
    Else
        lblWelcome.Caption = "Wrong Pin"
        lblWelcome.ForeColor = RGB(255, 0, 0)
        Attempts = Attempts + 1
    End If
End Sub

Sub SetId()
    PINField = "C" & Str(ID)
    PINField = Replace(PINField, " ", "")
    MsgBox (PINField)
    BalanceField = "D" & Str(ID)
    BalanceField = Replace(BalanceField, " ", "")
    MsgBox (BalanceField)
End Sub

Sub Welcome()
    MsgBox ("Login Successful. Welcome")
End Sub

Sub Bye()
    MsgBox ("Max Pin Attempts reached. Contact Your Bank")
    Unload frmLogin
End Sub
4

2 に答える 2

0

これを行う理由は、値のない変数を使用しているためです。であるため、Integer0 を返します。

ID = txtID.Textおそらく実際には、txtID テキストボックスの値を取得し、その値を ID 変数に格納したいでしょう。

Textテキストボックスのプロパティは文字列であるため、これはおそらくエラーになります。を使用する必要がありますID = CInt(txtID.Text)。また、割り当て前に txtID.Text が整数に評価されることを確認するために、いくつかのチェックを行う必要があります。

于 2013-01-12T13:37:40.167 に答える
0

resetここに示していないコードのどこにも txtIDがないことを確認してください。IDあなたのコードを見ると、値をどちらに設定するかについては何も言わPINれていPINません...

ニックが指摘したように、これは人々が入ることFormを許可するものであり、..そして、あなたはそれと比較しています. でも何と比べてるの?あなたが言ったように、シートにはデータベースのような構造があります。あなたはそれを評価して使用する必要があります。textboxesIDPINPINIDPIN

これがあなたのシートのビジュアライゼーションです。これは私の盲目的な推測です。

ユーザーは、フォームから に値を入力する必要がありますtxtID。その番号は、実際には、関連する を含むcell numberfor 列です。次に、その PIN を txtPIN 値と比較します。次に、それに基づいてfrom 列を返します。CPINbalanceDPIN

これを試して:

Private Sub btnLogin_Click()
    If txtID.Text <> "" Or txtID.value > 0 or txtPIN.Text <> "" Then
      ID = CInt(txtID.Text)
      Call SetID
      Call Authentication
    Else
      MsgBox "ID and PIN" can't be empty!"
    End If
End Sub

Sub Authenticate()
    If CInt(txtPin.Text) = PIN Then '-- here
        Call Welcome
        '-- idealy Blance can be shown at this point... 
    ElseIf Attempts > 3 Then
        Call Bye
    Else
        lblWelcome.Caption = "Wrong Pin"
        lblWelcome.ForeColor = RGB(255, 0, 0)
        Attempts = Attempts + 1
    End If
End Sub   

Sub SetId()       
      PIN = CInt(Trim(Sheets(1).Range("C" & ID).value))
      '-- NOT sure why you are showing this PIN here since you want to authenticate...?
      MsgBox PIN
      BalanceField = Sheets(1).Range("D" & ID).value  
      BalanceField = Trim(BalanceField) '--here
      '-- doesn't make sense to show Balance before authentication... 
      MsgBox BalanceField        
End Sub

于 2013-01-12T14:40:19.987 に答える