【发布时间】:2021-09-10 19:44:02
【问题描述】:
我正在尝试将数据从 Excel 添加到访问数据库中,我想一次性添加许多值。
问题在于 Excel 报告 SQL 缺少尾随分号 (;) 这是失败的 SQL 代码的摘录。
这是构建的 SQL 语句
INSERT INTO ThisTable (FirstName, LastName) VALUES ('John','Smith'),('Ringo','Star'),('Chris','Jones');
' --- Test Access Creation --- '
Sub Example2()
'the path to create the new access database
Dim strPath As String
'an DAO object
Dim db As DAO.Database
Dim command As String
Let command = "INSERT INTO ThisTable (FirstName, LastName) " & _
"VALUES " & _
"('John','Smith')," & _
"('Ringo','Star')," & _
"('Chris','Jones');"
Debug.Print command
strPath = "A:\NewDB"
Set db = DAO.OpenDatabase(strPath)
'Set db = DAO.CreateDatabase(strPath, DAO.dbLangGeneral)
'db.Execute "CREATE TABLE ThisTable " _
& "(FirstName CHAR, LastName CHAR);"
db.Execute command
Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("Select * FROM ThisTable")
'Begin row processing
Do While Not rst.EOF
Debug.Print Trim(rst.Fields(0)) & " " & Trim(rst.Fields(1))
rst.MoveNext
Loop
db.Close
End Sub
【问题讨论】:
-
我可能错了,但我认为 MS Access 不支持
VALUES。您必须改用多个UNIONs。 -
MS Access 确实支持 VALUES 子句,但仅适用于一条记录。 @JonathanWillcock,可以写一个显示 UNION 方法的答案。