Hi,
I want to create Access tables with table names taken from a range of cells in excel.
I am not using the VBA variable in SQL statement correctly. Please see the FOR NEXT construct in the code below
I tried this but doesn't work
Sub ADOCreatenewtables()
On Error Resume Next
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim X As String
strDBName = "ADO2.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
' open the connection
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open strDB
End With
For Each Cell In Range("K1:K3")
X = Cell.Value
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn
'create Table
cmd.CommandText = "CREATE TABLE 'X' (Style Char(10) Primary Key, A int, B int, C Int, RecActive YesNo)"
cmd.Execute , , adCmdText
Next Cell
Set cmd = Nothing
Set cnn = Nothing
Exit Sub
End Sub
I want to create Access tables with table names taken from a range of cells in excel.
I am not using the VBA variable in SQL statement correctly. Please see the FOR NEXT construct in the code below
I tried this but doesn't work
Sub ADOCreatenewtables()
On Error Resume Next
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim X As String
strDBName = "ADO2.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
' open the connection
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open strDB
End With
For Each Cell In Range("K1:K3")
X = Cell.Value
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn
'create Table
cmd.CommandText = "CREATE TABLE 'X' (Style Char(10) Primary Key, A int, B int, C Int, RecActive YesNo)"
cmd.Execute , , adCmdText
Next Cell
Set cmd = Nothing
Set cnn = Nothing
Exit Sub
End Sub