简体   繁体   English

具有多个条件的 Microsoft Access VBA DLookup

[英]Microsoft Access VBA DLookup with multiple criteria

Sorry for any dumb questions, I am very green with Access and VBA.抱歉有任何愚蠢的问题,我对 Access 和 VBA 非常熟悉。

I am creating a form for employees to input their hourly output.我正在为员工创建一个表格来输入他们的每小时产出。 It goes to a table called tracking, where there are 4 fields;它转到一个名为 tracking 的表,其中有 4 个字段; Shift, Operator, Date_Field, and Machine. Shift、运算符、Date_Field 和机器。 These fields are a unique index, and I want it to work so that if the person filling out the form adds a date/shift/worker/machine combo that already exists it will just take them to that record.这些字段是一个唯一索引,我希望它能够工作,这样如果填写表格的人添加了一个已经存在的日期/班次/工人/机器组合,它只会将它们带到该记录。

This code runs On Change after each combobox is selected but it obviously has issues.选择每个组合框后,此代码在更改时运行,但显然存在问题。 Please let me know what I am doing wrong and how I could implement this.请让我知道我做错了什么以及如何实现这一点。

Dim int_ID As Integer

If IsNull(DLookup("[ID]", "Tracking", "[Shift]='" & [Forms]![Tracking Form]![ShiftCbo] & "'" And "[Operator]='" & [Forms]![Tracking Form]![OpCbo] & "'" And "[Date_Field]='" & [Forms]![Tracking Form]![DateBox] & "'" And "[Machine]='" & [Forms]![Tracking Form]![MachineCbo] & "'")) = False Then
    int_ID = DLookup("[ID]", "Tracking", "[Shift]='" & [Forms]![Tracking Form]![ShiftCbo] & "'" And "[Operator]='" & [Forms]![Tracking Form]![OpCbo] & "'" And "[Date_Field]='" & [Forms]![Tracking Form]![DateBox] & "'" And "[Machine]='" & [Forms]![Tracking Form]![MachineCbo] & "'")
    DoCmd.GoToRecord acDataTable, "Tracking", acGoTo, int_ID
End If

End Sub

Use AfterUpdate instead of Change event.使用 AfterUpdate 而不是 Change 事件。

First off, the AND operator needs to be within quote marks as it is literal text.首先,AND 运算符需要在引号内,因为它是文字文本。 If code is behind [Tracking Form], don't need full form reference.如果代码在 [Tracking Form] 后面,则不需要完整的表单参考。 Code expects value of comboboxes to be text, not numbers, therefore fields must be text type.代码要求组合框的值是文本,而不是数字,因此字段必须是文本类型。 However, you have one field that appears to be date type and date parameters are defined with #.但是,您有一个字段似乎是日期类型,并且日期参数是用 # 定义的。

Dim int_ID As Integer
With Me
int_ID = Nz(DLookup("ID", "Tracking", "Shift='" & .ShiftCbo & _
         "' And Operator='" & .OpCbo & "' And Date_Field=#" & .DateBox & _
         "# And Machine='" & .MachineCbo & "'"), 0) 
End With
If int_ID <> 0 Then
    DoCmd.GoToRecord acDataTable, "Tracking", acGoTo, int_ID
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM