试图修复If ... End If块

时间:2016-02-29 21:27:31

标签: vba ms-access

我收到了这个编译错误:

  

如果没有结束则阻止

这是我正在使用的代码

Private Sub Command1_Click()
    If IsNull(Me.txtusername) Then
        'MsgBox "Please enter Username", vbInformation, "Username Required"
    If IsNull(Me.txtpassword) Then
        'MsgBox "Please enter Password", vbInformation, "Password Required"
    Else
        'processs the job
        If (IsNull(DLookup("[username]", "tbllogin info", "[username] ='" & Me.txtusername.Value & "' And password = '" & Me.txtpassword.Value & "'"))) Then
            MsgBox "login unsucessful"
        End If

        'MsgBox "Login Succeddful"
        DoCmd.OpenForm "s-1 page"

End Sub

2 个答案:

答案 0 :(得分:1)

你连续使用两个,而不是你应该使用ElseIf。

If IsNull(Me.txtusername) Then
    'MsgBox "Please enter Username", vbInformation, "Username Required"
ElseIf IsNull(Me.txtpassword) Then
    'MsgBox "Please enter Password", vbInformation, "Password Required"
Else
    'processs the job
    If (IsNull(DLookup("[username]", "tbllogin info", "[username] ='" & Me.txtusername.Value & "' And password = '" & Me.txtpassword.Value & "'"))) Then
    MsgBox "login unsucessful"
    End if
End If

答案 1 :(得分:1)

您有三条If ... Then行,但只关闭其中一行。您应该将它们加入到ElseIf

的一个构造中

像这样修复:

Private Sub Command1_Click()
    If IsNull(Me.txtusername) Then
        'MsgBox "Please enter Username", vbInformation, "Username Required"
    ElseIf IsNull(Me.txtpassword) Then
        'MsgBox "Please enter Password", vbInformation, "Password Required"
    ElseIf IsNull(DLookup("[username]", "tbllogin info", "[username] ='" & Me.txtusername.Value & "' And password = '" & Me.txtpassword.Value & "'")) Then
        MsgBox "login unsuccessful"
    Else
        'MsgBox "Login successful"
        DoCmd.OpenForm "s-1 page"
    End If
End Sub