2017-08-12 147 views
-1

我按照this guide,結果發現,如果我的用戶名和密碼都錯了,它會拋出一個運行時錯誤:多用戶登錄表單

"6" Overflow error.

不過,我已經使用下面的代碼嘗試:

ElseIf Username <> u And Password <> p Then 
    MsgBox "Username & Password not matched", vbCritical + vbOKCancel 
Exit Do 

我試過使用此代碼,但即使我的用戶名和密碼匹配它仍然會拋出下面的MsgBox。

enter image description here 這裏

Private Sub LoginButton_Click() 
    Application.ScreenUpdating = False 
    Dim Username As String 
    Dim Password As String 
    Dim i As Integer 
    Dim j As Integer 
    Dim u As String 
    Dim p As String 
    If Trim(TextBox1.Text) = "" And Trim(TextBox2.Text) = "" Then 
    MsgBox "Enter username and password.", vbOKOnly 
    ElseIf Trim(TextBox1.Text) = "" Then 
    MsgBox "Enter the username ", vbOKOnly 
    ElseIf Trim(TextBox2.Text) = "" Then 
    MsgBox "Enter the Password ", vbOKOnly 
    Else 
    Username = Trim(TextBox1.Text) 
    Password = Trim(TextBox2.Text) 
    i = 1 
    Do While Cells(1, 1).Value <> "" 
    j = 1 
    u = Cells(i, j).Value 
    j = j + 1 
    p = Cells(i, j).Value 
If Username = u And Password = p And Cells(i, 3).Value = "fail" Then 
    MsgBox "Your Account temporarily locked", vbCritical 
Exit Do 

ElseIf Username = u And Password = p Then 
    Call clr 
    'LoginFlag = True 
    Unload Me 
    MsgBox ("Welcome " + u + ", :)") 
Exit Do 

ElseIf Username <> u And Password = p Then 
    MsgBox "Username not matched", vbCritical + vbOKCancel 
Exit Do 
ElseIf Username = u And Password <> p Then 
If Cells(i, 3).Value = "fail" Then 
    MsgBox "Your account is blocked", vbCritical + vbOKCancel 
Exit Do 

ElseIf Cells(i, 4).Value < 2 Then 
    MsgBox "Invalid password", vbCritical 
    Cells(i, 4).Value = Cells(i, 4) + 1 
Exit Do 
Else 
    Cells(i, 4).Value = Cells(i, 4) + 1 
    Cells(i, 3).Value = "fail" 
    'Cells(i, 2).Value = "" 
    Cells(i, 2).Interior.ColorIndex = 3 
Exit Do 
End If 

ElseIf Username <> u And Password <> p Then 
    MsgBox "Username & Password not match", vbCritical + vbOKCancel 
Exit Do 

Else 
    i = i + 1 
End If 
Loop 
End If 
    Application.ScreenUpdating = True 
End Sub 
+0

請下次使用「代碼格式化按鈕」{}'來格式化您的代碼!另外,請通過添加錯誤消息的*文本*來改進您的問題,而不僅僅是截圖。此外,如果有東西說「在這裏輸入圖像說明」,*請輸入圖像說明*。要問一個正確的問題真的不是那麼難! –

+0

另請注意,這是密碼對話框最糟糕的例子。密碼是其保護的未加密數據的一部分。從來沒有想過使用這樣的東西! –

+0

沒有看到你的代碼我不能確定 - 但我的猜測是你有3條線你已經在循環內顯示我們。因此,只要您點擊不是您正在查找的用戶名**和**密碼不是您正在查找的那個,就會顯示該消息。 – YowE3K

回答

0

我認爲有以下可以更好地工作:

Private Sub LoginButton_Click() 
    Application.ScreenUpdating = False 
    Dim Username As String 
    Dim Password As String 
    'Use a variable to flag whether the userid is valid or not 
    Dim useridValid As Boolean 
    Dim i As Integer 
    'Dim j As Integer 
    Dim u As String 
    Dim p As String 
    If Trim(TextBox1.Text) = "" And Trim(TextBox2.Text) = "" Then 
     MsgBox "Enter username and password.", vbOKOnly 
    ElseIf Trim(TextBox1.Text) = "" Then 
     MsgBox "Enter the username ", vbOKOnly 
    ElseIf Trim(TextBox2.Text) = "" Then 
     MsgBox "Enter the Password ", vbOKOnly 
    Else 
     Username = Trim(TextBox1.Text) 
     Password = Trim(TextBox2.Text) 
     useridValid = False 
     i = 1 
     'Don't perform a loop which is dependent on a fixed cell that 
     'isn't updated within the loop 
     'Use a variable row counter instead 
     'Do While Cells(1, 1).Value <> "" 
     Do While Cells(i, 1).Value <> "" 
      'There is no point in having a variable simply to specify a 
      'column that doesn't change 
      'j = 1 
      u = Cells(i, "A").Value 
      'j = j + 1 
      p = Cells(i, "B").Value 
      'Only perform tests once a valid username has been found 
      If Username = u Then 
       'Flag that we have found the userid 
       useridValid = True 
       If Cells(i, "C").Value = "fail" Then 
        'Too many login attempts 
        MsgBox "Your Account temporarily locked", vbCritical 
       ElseIf Password = p Then 
        'Clear invalid attempts count 
        Cells(i, 4).Value = 0 
        Cells(i, 3).Value = "" 

        Call clr 
        Unload Me 
        MsgBox ("Welcome " + u + ", :)") 
       Else 
        'Invalid password 
        'Increment failed attempts counter 
        Cells(i, 4).Value = Cells(i, 4) + 1 
        'Lock account on 3rd failed password 
        If Cells(i, 4).Value > 2 Then 
         'lock the account 
         Cells(i, 3).Value = "fail" 
         'Cells(i, 2).Value = "" 
         Cells(i, 2).Interior.ColorIndex = 3 
         'Tell the user that password was invalid and now locked 
         MsgBox "Invalid password - account locked", vbCritical 
        Else 
         'Tell the user that password was invalid 
         MsgBox "Invalid password", vbCritical 
        End If 
       End If 
       'Don't check any further usernames 
       Exit Do 
      End If 
      i = i + 1 
     Loop 
     'If the flag saying that we found the userid isn't set, display 
     'a message 
     If Not useridValid Then 
      MsgBox "Username not matched", vbCritical + vbOKCancel 
     End If 
    End If 
    Application.ScreenUpdating = True 
End Sub 

注:這絕對是一個壞主意,一個工作表中持有明文密碼。人們很容易獲得整個列表。

+0

非常感謝!我想這有助於(現在我想我會試圖瞭解everyline意味着哈哈)。是的,我意識到它太容易讓人們獲得整個用戶列表,但我想我現在想吸菸,因爲我仍然是超級noob程序員:(。 – Xen

1

因爲代碼沒有被照顧的情況下當兩個Username以及Password失敗,這是一個無限循環Do While Cells(1, 1).Value <> ""您收到此錯誤代碼。因此,數據類型設置爲Integer的計數器i的值不斷增加,一旦超過32,767的限制,它將生成Overflow錯誤。

在支持我的上述論斷,考慮在你的代碼中的這些參數:

條件1 - 登錄狀態爲 「失敗」:

If Username = u And Password = p And Cells(i, 3).Value = "fail" Then 

條件2 - 既UsernamePassword匹配:

ElseIf Username = u And Password = p Then 

條件3 - Username與之不匹配:

ElseIf Username <> u And Password = p Then 

條件4 - Password沒有匹配:

ElseIf Username = u And Password <> p Then 

解決方案:

我們需要將無限循環改爲有限,即一旦它到達的空白單元格,將停止 - 所以它會像Do While Cells(i, 1).Value <> ""

此外,我們可以在上面添加UsernamePassword失敗的情況,您已經正確識別,但我懷疑,如果它仍然出現錯誤,它需要放在正確的位置,也就是循環後Do While

另一個小的修正 - i應該從2開始,而不是1,因爲我們要從2 nd行查找。

因此,讓我們把它放在一起:

Private Sub LoginButton_Click() 
    Application.ScreenUpdating = False 
    Dim Username As String, Password As String, i As Integer, j As Integer, u As String, p As String 
    If Trim(TextBox1.Text) = "" And Trim(TextBox2.Text) = "" Then 
     MsgBox "Enter username and password.", vbOKOnly 
    ElseIf Trim(TextBox1.Text) = "" Then 
     MsgBox "Enter the username ", vbOKOnly 
    ElseIf Trim(TextBox2.Text) = "" Then 
     MsgBox "Enter the Password ", vbOKOnly 
    Else 
     Username = Trim(TextBox1.Text) 
     Password = Trim(TextBox2.Text) 
     i = 2 
     Do While Cells(i, 1).Value <> "" 
      j = 1 
      u = Cells(i, j).Value 
      j = j + 1 
      p = Cells(i, j).Value 
      If Username = u And Password = p And Cells(i, 3).Value = "fail" Then 
       MsgBox "Your Account temporarily locked", vbCritical 
      Exit Do 
      ElseIf Username = u And Password = p Then 
       Call clr 
       'LoginFlag = True 
       Unload Me 
       MsgBox ("Welcome " + u + ", :)") 
       Exit Do 
      ElseIf Username <> u And Password = p Then 
       MsgBox "Username not matched", vbCritical + vbOKCancel 
       Exit Do 
      ElseIf Username = u And Password <> p Then 
       If Cells(i, 3).Value = "fail" Then 
        MsgBox "Your account is blocked", vbCritical + vbOKCancel 
        Exit Do 
       ElseIf Cells(i, 4).Value < 2 Then 
        MsgBox "Invalid password", vbCritical 
        Cells(i, 4).Value = Cells(i, 4) + 1 
        Exit Do 
       Else 
        Cells(i, 4).Value = Cells(i, 4) + 1 
        Cells(i, 3).Value = "fail" 
        'Cells(i, 2).Value = "" 
        Cells(i, 2).Interior.ColorIndex = 3 
        Exit Do 
       End If 
      Else 
       i = i + 1 
      End If 
     Loop 
     If Username <> u And Password <> p Then MsgBox "Username & Password not match", vbCritical + vbOKCancel 
    End If 
    Application.ScreenUpdating = True 
End Sub 

但請記住,這僅僅是一個演示。實際上,消息警報不應該清楚地說明錯誤是在用戶標識,密碼還是兩者上。

+0

嗨,好奇,謝謝你的回覆:)。我已經取代了IF以外的第五個條件,但即使用戶名和密碼匹配,我仍然被拋出第五個條件。你有什麼其他解決方案? – Xen

+0

嗨昂維恩,如果你有正確的嵌套,那麼它不應該拋出任何錯誤。將你的整個代碼塊添加到問題中。我會看看 – curious

+0

該代碼具有相同的問題,因爲我懷疑OP的代碼有 - 如果用戶名不是用戶輸入的用戶名,並且用戶的密碼與其他用戶的密碼不匹配,那麼'MsgBox「密碼和用戶名都不匹配」'執行。 (什麼樣的安全系統告訴用戶何時他們已經輸入了屬於某個其他用戶的密碼,即'MsgBox「用戶名不匹配」'?) – YowE3K