1

我在用户名和密码字段下的 Access 2007 表中保存了一条记录。现在我想检查是否在 VB6 文本框中输入了正确的大小写,以验证访问表中的用户名和密码。请在这方面帮助我。谢谢

萨法拉兹

4

4 回答 4

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

另请参阅:http: //support.microsoft.com/kb/209674

于 2012-12-31T11:58:56.230 回答
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
于 2012-12-31T09:42:22.337 回答
0

我看到人们抛出各种花哨的字符串比较代码和函数......我很困惑,为什么不只比较两个变量呢?

变量strUserName_EnteredstrPassword_Entered由用户和变量输入strUserName_DBstrPassword_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 
于 2012-12-31T17:20:20.393 回答
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

享受编码

于 2013-01-01T04:12:57.967 回答