如何在 Microsoft Access 中运行 INSERT SQL 查询?
例如:
INSERT INTO tbl_UserPurchase (ID, Name) Values (321, Joe)
您可以在 Access 的查询设计器中构建查询,然后仍在设计视图中单击功能区上的“运行”。 (寻找红色说明点。)
如果您想从代码中执行此操作,可以使用.Execute
DAO 数据库对象或 ADO 的方法CurrentProject.Connection
object.
Dim strInsert As String
strInsert = "INSERT INTO tbl_UserPurchase (ID, [Name])" & vbCrLf & _
"VALUES(321, 'Joe');"
CurrentDb.Execute strInsert, dbFailOnError
但是,参数查询会更灵活(对其他用户名有用,而不仅仅是 Joe)并防止 SQL 注入。
Dim db As DAO.database
Dim qdf As DAO.QueryDef
Dim strInsert As String
strInsert = "INSERT INTO tbl_UserPurchase (ID, [Name])" & vbCrLf & _
"VALUES (321, [which_user]);"
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", strInsert)
' you could read the parameter value from a text box on a form,
' but this example will just hard code Joe
qdf.Parameters("[which_user]").value = "Joe"
qdf.Execute dbFailOnError
还有其他方法可以剥这只猫的皮。如果没有一个建议令人满意,请向我们提供有关您希望如何以及在何处运行您的更多详细信息INSERT
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)