我是VBA的新手,需要帮助才能获得命令。当按下按钮时,我需要查找2个值并创建新记录。我对dlookup有疑问。
Private Sub Add_Record_Click()
Dim db As Database
Dim r As Recordset
Dim x As Integer
Set db = CurrentDb
Set r = db.OpenRecordset("Copy Of Employee Work Statistics")
Y = r.Fields("School")
z = r.Fields("School Id")
Y = DLookup(Table![Routes].School, Table![Routes], "Route" = Table![Routes].[Route Name])
z = DLookup("school ID", "Customer Database", "school" = Y)
r.Update
DoCmd.GoToRecord , , acNewRec
End Sub
答案 0 :(得分:0)
DLookup("school ID", "Customer Database", "school" = Y)
您的语法错误,您应该在字符串中包含所有标准(第3个参数),如下所示:
DLookup("[school ID]", "[Customer Database]", "[school] = '" & Y & "'")
其中&
用于字符串连接,Y是您先前声明的变量。我还建议您在引用列或表名时使用方括号([
,]
),以避免由于保留字而导致的任何冲突,或由于空格而导致的任何有趣错误在名字中。
对于更新部分,Y和z仅仅是变量。我猜你的意图是直接更新你的记录集。因此:
r.Fields("School") = DLookup("[" & Table![Routes].School & "]", _
"[" & Table![Routes] & "]", "[Route] = '" & Table![Routes].[Route Name] & "'" )
r.Fields("School Id") = DLookup("[school ID]", "[Customer Database]", _
"[school] = '" & Y & "'")
r.Update