1

我正在尝试使用 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
4

3 回答 3

2

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

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

使用StrComp

于 2013-05-01T02:16:31.390 回答
2

使用 StrComp

result = StrComp(actPassword, userPassword, vbTextCompare)

在此处输入图像描述

于 2013-05-01T02:16:50.083 回答
0

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

String.Compare可以返回任何整数,如果它们匹配,它只会返回零。您的特定测试(与 比较-1)仅检查一种可能性,即实际密码小于所需的密码,并且仅检查可能的负值之一。

在这种情况下,您会更好,例如:

if result <> 0 Then

但是,如果您只想在 VBA 中比较两个字符串是否相等(而不是找出相对顺序),您可以使用:

if actPassword = userPassword Then
于 2013-05-01T02:16:59.970 回答