insert data from one excel file to another one using ADODB

拈花ヽ惹草 提交于 2019-12-25 08:49:39

问题


I'm trying to insert data from one excel file to another one using adodb and faced a problem with insert method. It gives me an error message like below:

Syntax error in INSERT INTO statement

I tried different date formats, with and without quotes, also I tried different method with record sets and still got the same problem with this sql statement. Can someone help me to find out what's the problem?

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

This code I use to get Name from ResourceTasks and load it into master page; with very similar functions I retrieve tasks, load and dates the only difference it's a formatting on the screen

I made my connection with next parameters : 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

Thanks


回答1:


I think you have a syntax problem with the Date field in the query.

& CDate("24 / 5 / 2017") &...

A date should be either enclosed with #, or, Excel-specific, can be passed as a number.

First option:

... VALUES ('Name11', 'task2', #24/5/2017#, 0.50)"
'                              ^^^^^^^^^^^

Second option:

... VALUES ('Name11', 'task2'," & CDbl(DateSerial(2017, 5, 24)) & ", 0.50)"
'                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Also if ResourceTasks is a worksheet, it should be referenced in the query as [ResourceTasks$]

INSERT INTO [ResourceTasks$] VALUES...



回答2:


You can achieve this running your query against a recordset and then copying it to your target range. Code would be like:

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

Regards

* EDIT * From post update from OC. Note that I am unfamiliar with command clsDAL so I have only edited copying recordset to range. If you are pulling all your rows into a recordset and just want to output it into a worksheet you can use the following:

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



回答3:


so i found it's easier for me to AddNew than to Insert for some reason It looks like this; hope it would help someone

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


来源:https://stackoverflow.com/questions/45329909/insert-data-from-one-excel-file-to-another-one-using-adodb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!