2013-05-01 96 views
1

我正在尝试使用vb.net检查密码是否相同(也是大写)。但线尝试使用String时VB编译错误。比较

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 

回答

1

这是因为在VBA没有String.Compare()(有一个在VB.NET)

而且,请注意,VB.NET是不是VBA

使用StrComp

+0

谢谢你的帮助:) – user2304995 2013-05-01 03:46:26

1

使用STRCOMP

result = StrComp(actPassword, userPassword, vbTextCompare) 

enter image description here

0

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

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

你会在这种情况下,最好的东西,如:

if result <> 0 Then 

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

if actPassword = userPassword Then 
相关问题