我正在尝试使用adodb
将数据从一个excel文件插入到另一个excel文件中,并且遇到insert方法的问题。它给出了如下错误信息:
INSERT INTO语句中的语法错误
我尝试了不同的日期格式,有和没有引号,我也尝试了不同的方法与记录集,并仍然遇到与此sql语句相同的问题。 有人可以帮我找出问题所在吗?
Public Sub InsertIntoTable()
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Users\User1\Documents\Excel files\tracker.xls; Extended Properties=""Excel 8.0;HDR=Yes"""
mySQL = "INSERT INTO ResourceTasks (Name, Task, Date, Load) VALUES ('Name11', 'task2'," & CDate("24 / 5 / 2017") & ", 0.50)"
Debug.Print mySQL
objConnection.Execute mySQL
End Sub
此代码用于从ResourceTasks获取Name并将其加载到母版页中;具有非常相似的功能我检索任务,加载和日期唯一的区别是它是屏幕上的格式
我与下一个参数建立了联系:adOpenStatic,adLockReadOnly
Public Sub SearchNames()
Dim oResources As ADODB.Recordset
Dim oDal As New clsDAL
Worksheets("Master").Activate
Set oResources = oDal.GetReadonlyRS("SELECT DISTINCT [Name] FROM [ResourceTasks$]") 'where Date >= **Drop date from selection here** and Date < **Drop date from selection here** 'where Active like TRUE ")
oResources.MoveFirst
'get all record from record set one by one to 2nd table
Do Until oResources.EOF
RowNumber = 30 + oResources.AbsolutePosition
Range("A" & RowNumber).Value = oResources.Fields("Name").Value
oResources.MoveNext
Loop
'! get all record from record set one by one to 2nd table
'Close connection and file
oResources.Close
Set oResources = Nothing
oDal.closeFile
Set oDal = Nothing
'! close connection and file
End Sub
由于
答案 0 :(得分:0)
您可以针对记录集运行查询,然后将其复制到目标范围。代码就像:
Public Sub InsertIntoTable()
Dim objConnection As Object
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Users\User1\Documents\Excel files\tracker.xls; Extended Properties=""Excel 8.0;HDR=Yes"""
Dim mySQL As String
mySQL = "INSERT INTO ResourceTasks (Name, Task, Date, Load) VALUES ('Name11', 'task2'," & CDate("24 / 5 / 2017") & ", 0.50)"
Debug.Print mySQL
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
rs.Open mySQL, objConnection
ThisWorkbook.Sheets("1").Cells(1, 1).CopyFromRecordset Data:=rs
End Sub
此致
*编辑*
来自OC的更新后。请注意,我不熟悉命令clsDAL
所以我只编辑了将记录集复制到范围。如果要将所有行拖入记录集并只想将其输出到工作表中,则可以使用以下命令:
Public Sub SearchNames()
Dim oResources As ADODB.Recordset
Dim oDal As New clsDAL
Set oResources = oDal.GetReadonlyRS("SELECT DISTINCT [Name] FROM [ResourceTasks$]") 'where Date >= **Drop date from selection here** and Date < **Drop date from selection here** 'where Active like TRUE ")
ThisWorkbook.Sheets("Master").Range("A31").CopyFromRecordset Data:=oResources
oResources.Close
Set oResources = Nothing
oDal.closeFile
Set oDal = Nothing
'! close connection and file
End Sub
答案 1 :(得分:0)
我认为查询中的Date
字段存在语法问题。
&安培; CDate(&#34; 24/5/2017&#34;)&amp; ...
日期应该用#
括起来,或者特定于Excel,可以作为数字传递。
第一个选项:
... VALUES ('Name11', 'task2', #24/5/2017#, 0.50)"
' ^^^^^^^^^^^
第二个选项:
... VALUES ('Name11', 'task2'," & CDbl(DateSerial(2017, 5, 24)) & ", 0.50)"
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
如果ResourceTasks
是工作表,则应在查询中将其引用为[ResourceTasks$]
INSERT INTO [ResourceTasks$] VALUES...
答案 2 :(得分:0)
因此我发现AddAdw比插入更容易因为某些原因 看起来像这样;希望它会帮助某人
Public Function InsertNewRecord() As Boolean
Dim oTasks As ADODB.Recordset
Dim oDal As New clsDAL
Set oTasks = oDal.GetUpdateableRS("SELECT * FROM [ResourceTasks$] WHERE Task=''")
With oTasks
.AddNew
.Fields("Name").Value = Sheets("Master").ComboBox24.Value
.Fields("Task").Value = Sheets("Master").ComboBox25.Value
.Fields("Date").Value = Sheets("Master").ComboBox23.Value
.Fields("Load").Value = Sheets("Master").TextBox24.Value
.Update
.Close
End With
Set oTasks = oDal.GetUpdateableRS("SELECT Task FROM [Tasks$] WHERE Task=''")
With oTasks
.AddNew
.Fields("Task").Value = sTask
.Update
.Close
End With
Set oDal = Nothing
Set oTasks = Nothing
End Function