尝试使用String.Compare时VB编译错误

时间:2013-05-01 02:12:01

标签: vb.net ms-access access-vba

我正在尝试使用vb.net检查密码是否相同(就上限而言)。但行

 result = String.Compare(actPassword,userPassword)

不断给出错误“编译错误:预期:(”

Private Sub Login_Click()
    'Check to see if data is entered into the UserName combo box
     If IsNull(Me.cmbLoginID) Or Me.cmbLoginID = "" Then
        MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
        Me.cmbLoginID.SetFocus
       Exit Sub
      End If
      'Check to see if data is entered into the password box
       If IsNull(Me.txtPW) Or Me.txtPW = "" Then
           MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
            Me.txtPW.SetFocus
        Exit Sub
        End If
      'Check value of password in tblEmployees to see if this matches value chosen in combo box
      Dim userPassword As String
      Dim actPassword As String
      Dim result As Integer
      userPassword = txtPW.Text
      actPassword = DLookup("EmpPassword", "Employee", "EmployeeID='" + Me.cmbLoginID.Value + "'")
      result = String.Compare(actPassword,userPassword)
      If result = -1 Then
        'Close logon form and open splash screen
            DoCmd.Close acForm, "Login", acSaveNo
            DoCmd.OpenForm "HomePage"
         Else
        MsgBox "Password Invalid. Please Try Again", vbCritical + vbOKOnly, "Invalid Entry!"
         Me.txtPW.SetFocus
        End If
     'If User Enters incorrect password 3 times database will shutdown
        intLogonAttempts = intLogonAttempts + 1
        If intLogonAttempts > 3 Then
          MsgBox "You do not have access to this database. Please contact your system administrator.", vbCritical, "Restricted Access!"
        Application.Quit
       End If

    End Sub

3 个答案:

答案 0 :(得分:2)

那是因为VBA中没有String.Compare()(VB.NET中有)

另外,请注意VB.NET不是VBA

使用StrComp

答案 1 :(得分:2)

使用StrComp

result = StrComp(actPassword, userPassword, vbTextCompare)

enter image description here

答案 2 :(得分:0)

虽然VB.Net中存在String.Compare(),但我不认为它在VBA中存在。无论如何,你所做的事情无论如何都是错误的。

String.Compare可以返回任何整数,只有匹配才会返回零。您的特定测试(与-1进行比较)仅检查一种可能性,即实际密码而不是所需的密码,并且只检查一个可能的负值。

在这种情况下你最好用以下的东西:

if result <> 0 Then

但是,如果你只想在VBA中比较两个字符串是否相等(而不是计算相对排序),你可以使用:

if actPassword = userPassword Then