我在Access 2007表中保存了用戶名和密碼字段下的記錄。 現在我想檢查是否在VB6文本框中輸入了正確的大小寫以驗證訪問表中的用戶名和密碼。 請在這方面幫助我。 謝謝在訪問2007和VB6中創建區分大小寫的登錄
Sarfaraz
我在Access 2007表中保存了用戶名和密碼字段下的記錄。 現在我想檢查是否在VB6文本框中輸入了正確的大小寫以驗證訪問表中的用戶名和密碼。 請在這方面幫助我。 謝謝在訪問2007和VB6中創建區分大小寫的登錄
Sarfaraz
你可能會得到一些職位,其中利用正則表達式,不是我真正使用,因此該功能可以幫助您確定兩個字符串是否匹配,並且區分大小寫。
Public Function ExactMatch(varFirst As Variant, varSecond As Variant) As Boolean
Dim inti As Integer
'Initialise to False and amend to True if function passes
ExactMatch = False
'Initial checks before proceeding (Null?, Length mismatch?)
If IsNull(varFirst) And IsNull(varSecond) Then
ExactMatch = True
Exit Function
ElseIf IsNull(varFirst) And Not IsNull(varSecond) Then
Exit Function
ElseIf Not IsNull(varFirst) And IsNull(varSecond) Then
Exit Function
End If
If Len(CStr(varFirst)) <> Len(CStr(varSecond)) Then Exit Function
'Begin
For inti = 1 To Len(CStr(varFirst))
If Asc(Mid(varFirst, inti, 1)) <> Asc(Mid(varSecond, inti, 1)) Then Exit Function
Next
ExactMatch = True
End Function
StrComp可能更方便。 – Fionnuala
+1 @Remou,我忘記了所有關於** strComp ** –
StrComp可能適合:
Sub TestMatch()
'string1 is less than string2 -1
'string1 is equal to string2 0
'string1 is greater than string2 1
'string1 or string2 is Null Null
Debug.Print StrComp("ABC", "AB", vbBinaryCompare) ''= 1
Debug.Print StrComp("ABC", "abc", vbBinaryCompare) ''= -1
Debug.Print StrComp("ABC", "ABC", vbBinaryCompare) ''= 0
Debug.Print StrComp(Null, "ABC", vbBinaryCompare) ''= Null
End Sub
我看到人們扔在各種花式的字符串進行比較的代碼和功能......我百思不得其解,爲什麼不比較2個變量?
變量strUserName_Entered
和strPassword_Entered
由用戶輸入,變量strUserName_DB
和strPassword_DB
從DB加載。
If strUserName_Entered = strUserName_DB AND strPassword_Entered = strPassword_DB Then
' Everything is OK, entered username/password combination is exactly the same as the one from DB
Else
' Username/password combination do not match
End If
如果你特別希望當用戶輸入正確的用戶名/密碼,但在錯誤的情況,那麼你可以使用這個
If strUserName_Entered = strUserName_DB AND strPassword_Entered = strPassword_DB Then
' Everything is OK, entered username/password combination is exactly the same as the one from DB
Else If UCase(strUserName_Entered) = UCase(strUserName_DB) AND UCase(strPassword_Entered) = UCase(strPassword_DB) Then
' Username/password combination match, but at least one or more characters is/are in a wrong case
Else
' Username/password combination do not match (not the case error)
End If
我有最後的答案來區分:
Connection
Set rs = New ADODB.Recordset
Qry = "SELECT Password FROM Login WHERE UserName = '" & Text1.Text & "'"
rs.Open Qry, Con, adOpenDynamic, adLockOptimistic
If Not rs.EOF Then
If rs(0) = Text2.Text Then
msgbox"OK"
exit sub
Else
MsgBox "Invalid Username or Password", vbInformation, "Login..."
End If
Else
MsgBox "Invalid Username or Password", vbInformation, "Login..."
End If
享受編碼
您只需將用戶名發送至數據庫,並從數據庫獲取密碼,最後與if語句進行比較。 – user1939189
這種情況如何敏感?如果您在代碼模塊的聲明中使用「Option Compare Binary」,則應該明確這一點。 – HansUp
我同意HansUp的看法,OP有點混亂,因爲提到了正確的案例。 OA,請你澄清一下?另一方面,爲了優化代碼,您有2個錯誤消息框 - 1是不必要的。擦除1並將另一個移到第二個「End If」之後(在代碼的最後) – George
可能重複[如何使SQL區分大小寫](http://stackoverflow.com/questions/574 7698/how-to-make-sql-case-sensitive) –