我已在用户名和密码字段下的Access 2007表中保存了一条记录。 现在我想检查是否在VB6文本框中输入了正确的大小写以验证访问表中的用户名和密码。 请帮助我这方面。 谢谢
Sarfaraz
答案 0 :(得分:2)
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
答案 1 :(得分:1)
您可能会收到一些使用 RegEx 的帖子,而不是我真正使用的帖子,因此此功能可以帮助您确定两个字符串是否匹配且区分大小写。
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
答案 2 :(得分:0)
我看到人们投入各种花哨的字符串来比较代码和函数......我很困惑,为什么不只是比较2个变量?
变量strUserName_Entered
和strPassword_Entered
由用户输入,变量strUserName_DB
和strPassword_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
答案 3 :(得分:0)
我终于得到了答案:
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
享受编码