在DLookup标准中使用Null

问题描述 投票:1回答:1

我正在创建一个数据库来管理事件系统内外的人员流动。我的想法是使用DLookup函数允许我在结帐日期中识别空值。这样我就可以防止有人在没有先退出的情况下跳过事件。

如果有可能我想获得EVENT ID和COMMAND POST ID,这样我就可以创建一条错误消息告诉我成员仍然附着的事件。

'***Section 1 - Ensure member is Checked out and DEMOBed from previous incidents
'******Variables
'*********Variable to hold the Member ID
Dim id As Integer
'*********Variable to hold the checkout status
Dim checkout As String
'*********Variable to hold the Event ID
Dim eventID As Integer


'******Code Block 1 - Check for Null Values
id = Me.Text18
Forms![frm_ics238Table].Refresh

If Not IsNull(DLookup("[eventID]", "[frm_ics238Table]", "[checkoutDate] is Null And employeeID = '" & Me.Text18 & "'")) Then
    MsgBox "y"
End If
End Sub
vba ms-access access-vba ms-access-2010
1个回答
0
投票

从你原来的问题来看,你现在看起来并没有编辑它来表明更新的代码和错误,你有:

id = Me.Text18
Forms![frm_ics238Table].Refresh

If Not IsNull(DLookup("[eventID]", "[frm_ics238Table]", "[checkoutDate] is Null And employeeID = '" & Me.Text18 & "'")) Then
    MsgBox "y"
End If

我认为这些可能有所帮助:

  1. "[frm_ics238Table]"应该是桌子:"[tbl_ics238Table]"
  2. ID似乎被声明为整数,但您将其用作字符串。如果employeeID实际上是一个数字字段,则删除employeeID = '" & Me.Text18 & "'"周围的引号它应该看起来像这样employeeID = " & ID & "
  3. 您也可以尝试使用IsNull([checkoutDate])而不是[checkoutDate] is Null

它可能看起来像:

If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", "IsNull([checkoutDate]) And (employeeID = " & ID & ")")) Then
    MsgBox "y"
End If

更好的方法是将标准放入字符串变量中,以便您可以先调试并测试它

Dim strCriteria as String
strCriteria = "IsNull([checkoutDate]) And (employeeID = " & ID & ")"
Debug.Print strCriteria

If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", strCriteria)) Then
    MsgBox "y"
End If

编辑:工作代码证明:

并不是说我不相信你,但如果你所描述的与我的假设相符,我不相信它不起作用。

我使用以下假设成功测试:

  • eventID - 数字字段
  • checkoutDate - 日期/时间字段
  • employeeID - 数字字段

我的样本数据表

table

然后我在表单上放置一个命令按钮来测试代码:

Private Sub Command8_Click()
    Dim strCriteria As String
    Dim ID As Integer

    ID = 4

    strCriteria = "IsNull([checkoutDate]) And (employeeID = " & ID & ")"
    Debug.Print strCriteria

    If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", strCriteria)) Then
        MsgBox "y"
    End If

End Sub

最后 - 测试按钮点击导致:

Results

总而言之 - 如果您仍然收到错误,那么您可以使用您的代码和表格设计更新您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.