Goal: Insert WS data into MS Access Table
using ADO and SQL
Error:Invalid Argument
SQL Used:
INSERT INTO MyNewTable (C1,C2,C3) SELECT * FROM
[Excel 12.0;HDR=YES;C:\Development\Excel_Sandy_NaturalFactors\A_Programs\NatFactors.xlsm].[BY_X$]
Connection:
Valid Connection
Table Structure:
C1 - Text
C2 - Text
C3 - Text
Table Name:
MyNewTable
WS NAME:
BY_X
Code:
The code is below but now useful
in that it calls related properties.
Public Function Add_Records_InsertInto_OneShot() As Boolean
On Error GoTo EH_Add_Records_InsertInto_OneShot
Dim i As Integer
Dim colCursor As Integer
Dim rowCursor As Integer
Dim curCell As String
Dim sValue As String
'---------------------
' Init
'---------------------
Init_Cat_Vars
ADO_CloseConnection = False
Cat_TableName = "MyNewTable"
'---------------------
' WB_StartUp_Quick
'---------------------
WSMyDataClear = False
ADO_RunADOProviders = True
ADO_RunADOExtenders = True
WB_StartUp_Quick WSMyDataClear, ADO_RunADOProviders, ADO_RunADOExtenders
'-----------------------
' Data Range
'-----------------------
With WS_BY_X
LR = .Range(.Cells(.Rows.Count, 1).End(xlUp).Address).Row
LC = .Range(.Cells(1, .Columns.Count).End(xlToLeft).Address).Column
LR = .Cells(.Rows.Count, 1).End(xlUp).Row
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set Rng_Insert = .Range(.Cells(1, 1), .Cells(LR, LC))
With Rng_Insert
CntRow = .Rows.Count
CntCol = .Columns.Count
End With
End With
'---------------------
' SQL STATEMENT
'---------------------
SQL_Header = vbNullString
SQL_ColHD = vbNullString
SQL_RowDT = vbNullString
SQL1 = vbNullString
'-----------------------
' Column Header Loop
'-----------------------
With Rng_Insert
For i = 1 To CntCol
Select Case i
Case 1: SQL_ColHD = SQL_ColHD & WB_CON_LP & .Columns(i).Rows(1).Value & "" & ","
Case Is <> CntCol: SQL_ColHD = SQL_ColHD & "" & .Columns(i).Rows(1).Value & "" & ","
Case CntCol: SQL_ColHD = SQL_ColHD & "" & .Columns(i).Rows(1).Value & "" & ")"
End Select
Next
End With
'---------------------
' SQL STATEMENT
'---------------------
SQL_Header = "INSERT INTO "
SQL_Header = SQL_Header & Cat_TableName
SQL_Header = SQL_Header & WB_CON_SPACE
SQL_Header = SQL_Header & SQL_ColHD
'---------------------
' SQL STATEMENT
'---------------------
SQL_RowDT = SQL_RowDT & " SELECT * FROM " & vbCrLf
SQL_RowDT = SQL_RowDT & "["
SQL_RowDT = SQL_RowDT & "Excel 12.0;HDR=YES;"
SQL_RowDT = SQL_RowDT & WB_This.Path & "\" & WB_This.Name
SQL_RowDT = SQL_RowDT & "]"
SQL_RowDT = SQL_RowDT & "."
SQL_RowDT = SQL_RowDT & "["
SQL_RowDT = SQL_RowDT & "BY_X$"
SQL_RowDT = SQL_RowDT & "]"
'---------------------
' SQL STATEMENT
'---------------------
SQL1 = SQL_Header & SQL_RowDT
'---------------------
' Connect
'---------------------
Catalog_ConnectUsingWBFolder
'---------------------
'---------------------
Set ADO_Command = Nothing
Set ADO_Command = New ADODB.Command
With ADO_Command
.ActiveConnection = ADO_CatConnection
.CommandText = SQL1
.Execute
End With
'-----------------------
'-----------------------
Add_Records_InsertInto_OneShot = True
Exit Function
EH_Add_Records_InsertInto_OneShot:
MsgBox Err.Number & " " & Err.Description, vbCritical, "Add_Records_InsertInto_OneShot"
ADO_Kill
Add_Records_InsertInto_OneShot = False
Exit Function
End Function
Bookmarks