VBA Insert Into VALUES为局部变量读取null

时间:2014-07-18 13:47:10

标签: sql vba ms-access access-vba

我正在使用Microsoft Access并尝试从表(名为Hazards)中提取ID,该表具有与表单(名为Edit Hazards)上的ID相同的ID。在我提取此值之后,我想将其插入到标题下的另一个表(名为People_Hazards)中(由于查找到另一个表,这也是一个ID)。

然而,在我将其插入局部变量后,当我尝试将其放入VALUES的insert语句时,我收到错误消息,其中包含'预期参数1'。

我尝试使用MsgBox来检查查找是否正确,并且它工作正常。但是当使用Insert语句时,它似乎没有收到任何变量。

我还检查过我的所有数据类型都是正确的,但即使这样,似乎没有任何工作。任何帮助将非常感谢!我的代码如下。

Dim x
x = DLookup("[ID]", "Hazards", "[Hazards].[ID] = " & Forms![Edit Hazards]![ID]) 

MsgBox x   'I receive the correct value in the message box here'

CurrentDb.Execute "INSERT INTO People_Hazards([Title]) VALUES (x)"

1 个答案:

答案 0 :(得分:1)

我不知道它是否是拼写错误,但您插入x而不是value of x

我建议先创建一个字符串,然后尝试使用CurrentDb.Execute

Dim strSql as string
Dim x as String
x = DLookup("[ID]", "Hazards", "[Hazards].[ID] = " & Forms![Edit Hazards]![ID]) 
strSql="Insert into People_Hazards([Title]) VALUES ("& x & ")"
Msgbox(strSql)
CurrentDb.Execute strSql