DLookup的语法错误

时间:2014-09-18 17:16:59

标签: vba ms-access access-vba

我似乎无法弄清楚我的代码在哪里出错了。它一直说有语法错误

Dim NullorNot As Date
NullorNot = Nz(DLookup("[Week]", "[Weeks on Call]", "[Week] = #" & cboWeek.Column(1) & "#"), [#01/01/2000#])

编辑:

以下是插入按钮的完整代码。这样做的目的是,我希望能够为两名员工添加一个条目,一个是主要的,另一个是两周的备份。

Private Sub cmdInsert_Click()
Response = MsgBox("Are you sure you would like to add this form?", [vbYesNoCancel], "Confirm Insert")
If Response = vbYes Then
Dim TempCount As Date
Dim TempCount2 As Date
Debug.Print "[Week] = #" & cboWeek.Column(1) & "#"
TempCount = Nz(DLookup("[Week]", "[Weeks on Call]", "[Week] = #" & cboWeek.Column(1) & "#"), #1/1/2000#)
TempCount2 = Nz(DLookup("[Week]", "[Weeks on Call]", "[Week] = #" & cboWeek2.Column(1) & "#"), #1/1/2000#)
If ((TempCount = #1/1/2000#) And (TempCount2 = #1/1/2000#)) Then
GoTo Method_Run
Else
Dim Msg2 As String
Msg2 = "Employees have already been assigned to be On Call for that date."
Dim title2 As String
title2 = "Duplicate Error"
MsgBox Msg2, [vbOKOnly], title2

End If
Method_Run:
Dim SQL As String
Dim SQL2 As String
On Error GoTo Err_Insert
DoCmd.SetWarnings False
SQL = "INSERT INTO [Weeks on Call]([Primary Employee], [Backup Employee], [Week]) VALUES ('" + cboName.Column(1) + "','" + cboName2.Column(1) + "','" + cboWeek.Column(1) + "')"
SQL2 = "INSERT INTO [Weeks on Call]([Primary Employee], [Backup Employee], [Week]) VALUES ('" + cboName2.Column(1) + "','" + cboName.Column(1) + "','" + cboWeek2.Column(1) + "')"

DoCmd.RunSQL SQL
DoCmd.RunSQL SQL2



Exit_Err:
    Exit Sub

Err_Insert:
If Err.Number = 94 Then GoTo Err_Msg
Err_Msg:
Dim Msg As String
Msg = "One or more of the fields is null, please make sure each field is filled"
Dim title As String
title = "Null Error"
MsgBox Msg, [vbOKOnly], title
End If

If Response = vbNo Then
Response = MsgBox("Entry was not saved", [vbOKOnly], "Not Saved")
End If
If Response = vbCancel Then
Response = MsgBox("Entry was not saved", [vbOKOnly], "Not Saved")
End If

End Sub

1 个答案:

答案 0 :(得分:2)

将日期值括在方括号中时,Access会将其解释为对象标识符,而不是文字日期/时间值。

删除方括号。

NullorNot = Nz(DLookup("[Week]", "[Weeks on Call]", "[Week] = #" & cboWeek.Column(1) & "#"), #01/01/2000#)