在if语句VBA MSAccess中错误地使用DLookup

时间:2017-09-04 05:00:03

标签: sql vba ms-access

我正在尝试使用if语句来确定文本字段是否包含数据库中的值。除非在输入任何内容时出现错误。

以下是代码:

Private Sub btnLogEntry_Click()


If Me.txtMemberID.Value <> "" And DLookup("MemberID", "Member", "MemberID = " & Me.txtMemberID.Value & "") > 0 Then
MsgBox "Entry Successful", vbInformation, "Alert"
MsgBox "Welcome", vbInformation, "Alert"
TempVars!activtyMemberID = Me.txtMemberID.Value
TempVars!activityTime = Me.txtTime.Value
TempVars!activityAccessType = Me.txtAccessType.Value
TempVars!activityFranchiseID = DLookup("[FranchiseID]", "Member", "MemberID = " & Me.txtMemberID.Value)
DoCmd.RunSQL "INSERT INTO GymActivity VALUES (Tempvars!activtyMemberID, 
Tempvars!activityTime, Tempvars!activityAccessType, 
Tempvars!activityFranchiseID)"

DoCmd.Close
DoCmd.OpenForm "frmMainPage"
Else
MsgBox "Please re-enter your Member ID"

End If
End Sub

以下是我收到的错误: https://imagebin.ca/v/3ZBvRiXlZM2t

2 个答案:

答案 0 :(得分:0)

问题在于你的边缘情况。即当txtMemberID.Text == ""时。

请注意,VBA对延迟评估的支持非常有限 - 即评估嵌套条件,只有当评估的那些已通过检查到目前为止 - 并且And运算符不属于此支持,这意味着与And语句连接的所有条件都按照出现的顺序进行评估,即使行中的第一个(最左边的)结果为False

因此,当txtMemberID文本框中没有文字时,DCount(...函数仍然会被评估,并且......你最终会得到格式错误的表达式;正如错误消息中所示。

有两种选择可以克服这种情况。延迟评估仅支持within the Select Case statement

另一种选择是嵌套If语句,如下所示:

Private Sub btnLogEntry_Click()

If Me.txtMemberID.Value <> "" Then
    If DLookup("MemberID", "Member", "MemberID = " & Me.txtMemberID.Value & "") > 0
        MsgBox "Entry Successful", vbInformation, "Alert"
        MsgBox "Welcome", vbInformation, "Alert"
        TempVars!activtyMemberID = Me.txtMemberID.Value
        TempVars!activityTime = Me.txtTime.Value
        TempVars!activityAccessType = Me.txtAccessType.Value
        TempVars!activityFranchiseID = DLookup("[FranchiseID]", "Member", "MemberID = " & Me.txtMemberID.Value)
        DoCmd.RunSQL "INSERT INTO GymActivity VALUES (Tempvars!activtyMemberID, 
        Tempvars!activityTime, Tempvars!activityAccessType, 
        Tempvars!activityFranchiseID)"

        DoCmd.Close
        DoCmd.OpenForm "frmMainPage"
    Else
        MsgBox "The specified MemberID not found."
    End If
Else
    MsgBox "Please re-enter your Member ID"    
End If

End Sub

答案 1 :(得分:0)

你应该利用这样一个事实,即DLookup返回 Null 为&#34; not found&#34;并一次查看FranchiseID:

Private Sub btnLogEntry_Click()

    Dim FranchiseID As Variant

    If Not IsNumeric(Me.txtMemberID.Value) Then
        ' None or invalid user input. Exit.
        MsgBox "A member ID is required."
        Exit Sub
    End If

    FranchiseID = DLookup("[FranchiseID]", "Member", "MemberID = " & Me.txtMemberID.Value & "")

    If Not IsNull(FranchiseID) Then
        MsgBox "Entry Successful", vbInformation, "Alert"
        MsgBox "Welcome", vbInformation, "Alert"

        TempVars!activtyMemberID = Me.txtMemberID.Value
        TempVars!activityTime = Me.txtTime.Value
        TempVars!activityAccessType = Me.txtAccessType.Value
        TempVars!activityFranchiseID = FranchiseID

        DoCmd.RunSQL "INSERT INTO GymActivity VALUES (Tempvars!activtyMemberID, Tempvars!activityTime, Tempvars!activityAccessType, Tempvars!activityFranchiseID)"

        DoCmd.Close
        DoCmd.OpenForm "frmMainPage"
    Else
        MsgBox "Please re-enter your Member ID"            
    End If

End Sub