教一下关于Dlookup的用法:
窗体:设备配发
窗体文本框控件:[SN],[Old Owner],[Old Location]
表:Devices
字段:[Serial Number],[location],[Owner]
目的:窗体中输入序列号后,自动查找输入的SN是否存在,不存在则提示“设备尚未入库”,如果存在则更新窗体中[Old Owner],[Old Location]文本框
始终不成功!求各位高手指点
窗体在SN更新前设置VBA如下:
Private Sub SN_BeforeUpdate(Cancel As Integer)
Dim OWstr As String
If DCount("[Serial Number]", "[Devices]", "[Serial Number] =form![设备配发].[sn]") = 0 Then
MsgBox "该设备尚未入库"
Else
[设备配发].[Old Owner] = DLookup("[Owner]", "[devices]", "[Serial Number] =form![设备配发].[sn]")
[设备配发].[Old Location] = DLookup("[Location]", "[devices]", "[Serial Number] =form![设备配发].[sn]")
End If
"[Serial Number] =" & form![设备配发].[sn])
[设备配发].[Old Owner] = DLookup("[Owner]", "[devices]", "[Serial Number] =form![设备配发].[sn]")
如果[设备配发]是当前窗体,没必要在每个控件前都加窗体名
me.[Old Owner]
me. 可省略
如果要加应该是
forms.[设备配发].[Old Owner]
forms![设备配发]![Old Owner]
"[Serial Number] =form![设备配发].[sn]")
可以改为
"[Serial Number] =form![sn]")
"[Serial Number] =form.[sn]")
也可以为
Visual Basic code
"[Serial Number] =" & forms![设备配发].[sn]) '如果sn是数字
"[Serial Number] ='" & forms![设备配发].[sn]) & "'" '文本
Visual Basic code
Private Sub SN_BeforeUpdate(Cancel As Integer)
Dim OWstr As String
If DCount("[Serial Number]", "[Devices]", "[Serial Number] ='" & Form![设备配发].[sn] & "'") = 0 Then
MsgBox "该设备尚未入库"
Cancel = True
Exit Sub
Else
[Old Owner] = DLookup("[Owner]", "[devices]", "[Serial Number] ='" & Form![设备配发].[sn] & "'")
[Old Location] = DLookup("[Location]", "[devices]", "[Serial Number] ='" & Form![设备配发].[sn] & "'")
End If
End Sub
1)Cancel=True是啥作用?是将已经输入的SN置空吗?
2)'" &<span style="color: #0000FF;"> Forms</span>![设备配发].[sn] & "'" 这个完全没有明白是啥意思?
加上 cancel = true 对[SN]的修改不会被保存,当然这里也可以不加.
'" &<span style="color: #0000FF;"> Forms</span>![设备配发].[sn] & "'"
应该是 '" & Forms ![设备配发].[sn] & "'" ,本打算把Forms变个颜色,结果不支