Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 如何将两个表的未绑定字段中的新记录添加到子窗体中_Ms Access_Ms Access 2007_Vba - Fatal编程技术网

Ms access 如何将两个表的未绑定字段中的新记录添加到子窗体中

Ms access 如何将两个表的未绑定字段中的新记录添加到子窗体中,ms-access,ms-access-2007,vba,Ms Access,Ms Access 2007,Vba,嗨,你好,有人能帮我吗? 我得到了一个主窗体和一个子窗体,我创建了一个连接两个表的查询,我尝试使用该查询插入未绑定的字段,我的子窗体字段,我从我的连接查询中获得,如果我插入到我的字段中,它会显示,然后单击“添加”按钮,我得到一个错误,它会显示: 运行时错误“3134”: insert into语句中出现语法错误 我使用的代码如下: Private Sub Add_Click() CurrentDb.Execute "INSERT INTO PlantTransactionQuery(Trans

嗨,你好,有人能帮我吗? 我得到了一个主窗体和一个子窗体,我创建了一个连接两个表的查询,我尝试使用该查询插入未绑定的字段,我的子窗体字段,我从我的连接查询中获得,如果我插入到我的字段中,它会显示,然后单击“添加”按钮,我得到一个错误,它会显示: 运行时错误“3134”: insert into语句中出现语法错误

我使用的代码如下:

Private Sub Add_Click() 
CurrentDb.Execute "INSERT INTO PlantTransactionQuery(TransactionID,Plant Number,Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,Hours Worked,Fuel,Fuel Cons Fuel/Hours,Hour Meter Replaced,Comments)" & _
"VALUES(" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
PlantTransactionsubform.Form.Requery 
End Sub
我不知道你是否可以在查询中插入,我也不知道我必须在保留名称的括号中插入哪个名称。 对于如何在同一页面上单击一个按钮,在一个子表单中添加两个表,甚至可能是我在代码中犯了一个错误,任何帮助都将不胜感激。一切看起来都很好。
提前感谢

您需要在任何表或字段名称周围放置方括号
[

  • 包含空格或“有趣的字符”,或

  • 这是一个很好的例子

请尝试以下方法:

CurrentDb.Execute "INSERT INTO PlantTransactionQuery (TransactionID,[Plant Number],Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,[Hours Worked],Fuel,[Fuel Cons Fuel/Hours],[Hour Meter Replaced],Comments) " & _
"VALUES (" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
编辑 如果将SQL语句逐行分解,可以使SQL语句更容易进行可视化验证,如下所示:

CurrentDb.Execute _
        "INSERT INTO [PlantTransactionQuery] (" & _
                "[TransactionID], " & _
                "[Plant Number], " & _
                "[Categories], " & _
                "[Description], " & _
                "[Location], " & _
                "[TransactionDate], " & _
                "[Opening_Hours], " & _
                "[Closing_Hours], " & _
                "[Hours Worked], " & _
                "[Fuel], " & _
                "[Fuel Cons Fuel/Hours], " & _
                "[Hour Meter Replaced], " & _
                "[Comments] " & _
            ") VALUES (" & _
                Me.txt13 & ", " & _
                "'" & Me.txt1 & "', " & _
                "'" & Me.txt2 & "', " & _
                "'" & Me.txt3 & "', " & _
                "'" & Me.txt4 & "', " & _
                "#" & Me.txt5 & "#, " & _
                "'" & Me.txt6 & "', " & _
                "'" & Me.txt7 & "', " & _
                "'" & Me.txt8 & "', " & _
                "'" & Me.txt9 & "', " & _
                "'" & Me.txt10 & "', " & _
                Me.txt11 & ", " & _
                Me.txt12 & " " & _
            ")"
如果字段(列)名称拼写错误,或者恰好是保留字的字段名称未包含在方括号中,则可能出现3061错误消息“参数太少…”。为了安全起见,我编辑了上面的查询,所以所有表名和列名都用括号括起来

此外,请检查您是否引用字符串而不是数字。错误也可能导致上述错误

至于[TransactionDate],您可能应该取消绑定文本框以保持一致性。请注意,该字段的值用散列标记(
#
)而不是单引号括起来。您可以在文本框中添加“日期”输入掩码,也可以使用类似于

Format(CDate(Me.txt5), "yyyy-mm-dd")
…为您设置日期格式

<>最后,请注意,这样构建SQL字符串会使您对<强> SQL注入<强>问题开放,因此您也可以考虑替换< <代码> >执行< /代码>,如

Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("[PlantTransactionQuery]", dbOpenTable)
rst.AddNew
rst![TransactionID] = CLng(Me.txt13)
rst![Plant Number] = Me.txt1
rst![Categories] = Me.txt2
...
rst![Comments] = Me.txt12
rst.Update

我收到一个运行时错误“3061”,预期参数太少1。只有我的TransactionDate是绑定字段,因为我不知道如何在访问表单中创建日期字段。有任何建议吗