如何制作带有数字值的 Access 文本框,等于以文本框为条件的 DLookup
我真的不知道为什么这不起作用,但我试图让以下代码起作用:
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Me.Text1") Then
MsgBox "It works"
Else
End If
上面的代码是我正在开发的一个较大项目的测试代码,因此表和字段名称仅用于测试。 此外,Field1 是一个数字字段。 基本上,当我在 Text1 中输入 1 时,我希望 DLookup 功能在 Test1_Table.Field1 中查找 1 并给我一条消息,说明它有效。 我能够让它与字符串值一起使用,例如:
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1='" _
& Me.Text1 & "'")Then
同样,当在 Text1 中输入 1 时,这也有效:
If 1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Me.Text1") Then
但是,我已经尝试过:
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Forms!TestSearch_Form!Text0)Then
等等
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Forms!TestSearch_Form!Text0)Then
...
我尝试了许多不同的组合,看起来好像在处理数值时,我无法使两者相等。 有谁知道我缺少什么或有什么想法?
谢谢你,
达米恩
I really have no idea why this doesn't work, but I am trying to get the following to work:
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Me.Text1") Then
MsgBox "It works"
Else
End If
The above code is a test code for a larger project that I am working on, so the table and field names are just for testing. Also, Field1 is a numberic field. Basically, when I enter 1 within Text1, I would like the DLookup feature to find 1 within Test1_Table.Field1 and give me a message stating that it works. I am able to get this to work with string values such as:
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1='" _
& Me.Text1 & "'")Then
Likewise this works, when 1 is entered in the Text1:
If 1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Me.Text1") Then
However, I've tried:
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Forms!TestSearch_Form!Text0)Then
and
If Me.Text1 = DLookup("Field1", "Test1_Table", "Test1_Table.Field1= _
& Forms!TestSearch_Form!Text0)Then
etc...
I've tried many different combinations and it seems as though I cannot get the two to equal, when dealing with numeric values. Does anyone know what I am missing or have any ideas?
Thank you,
Damion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
这仅在 Field1 定义为数字字段时才有效,如果它是日期时间或文本字段,则需要分隔符。
编辑:
上述语句要么等于 Me.Text1 的值,要么为 null。 使用 DllookUp 的另一种方法是:
SomeVar 要么为 null,即未找到,要么返回值或 Field1,它等于 Me.Text1,因为这是我们在Where 语句中所要求的。 从这里可以看出,返回Field1的值是没有意义的,要么找到并且等于text1,要么没有找到,并且为null。 获取 DlookUp 值的唯一原因是您要在表中查找其他值或计算。
此后,重要的是要记住,您正在寻找精确匹配,并且小数点右侧的小数值可能会非常不同,而您不太可能在此处查看。
How about:
This will only work if Field1 is defined as a numeric field, you will need delimiters if it is a datetime or text field.
EDIT:
The above statement is either equal to the value of Me.Text1, or is null. Another way to use DllookUp would be to say:
SomeVar will be either null, that is, not found, or return the value or Field1, which is equal to Me.Text1, because that is what we asked for in the Where statement. You can see from this that it is pointless to return the value of Field1, it is either found andequal to text1, or not found, and null. The olnly reason for getting the value of DlookUp is if you are looking up some other value or calculation in the table.
After this, it is important to remember that you are looking for an exact match and decimal values can be quite different far to the right of the decimal point, where you are unlikely to look.
尝试在 DLookup 中执行过多的 vba 可能会出现问题?
Trying to do too much vba inside the DLookup could be an issue?
如果 CInt(Me.Text1) = DLookup("Field1", "Test1_Table", "Test1_Table.Field1=" & Me.Text1) 那么...
上面的代码完美运行。 感谢 Guness,我能够利用我的原始代码,只需将 CInt 添加到 if 语句中即可。
另外,感谢大家的意见。
直接制造
If CInt(Me.Text1) = DLookup("Field1", "Test1_Table", "Test1_Table.Field1=" & Me.Text1) Then...
The above code works perfectly. Thanks to Guiness, I was able to utilize my original code and just add CInt to the if statement.
Also, thank you everyone for your input.
DFM