I am brand new to access and struggling to understand how to relate access table/column/row formatting to excel exporting in VBA.
I have created an access table called Data and three columns Food, Drinks, Color.
I would like to export these range of cells to Access from my excel spreadsheet:
Foodrng = Workbooks(xlFile).Sheets("ToBeExported").Range("D6")
Drinksrng = Workbooks(xlFile).Sheets("ToBeExported").Range("E6")
Colorrng= Workbooks(xlFile).Sheets("ToBeExported").Range("B12:B21")
Everything online says I should use this for because of my version:
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"
I would like to use INSERT TO
formatting to write to my database because I will be expanding the database columns quite a bit, and I feel this is the easiest way to keep track of which is going where.
strSql = "INSERT INTO Data (Food, Drinks, Color) VALUES (Foodrng, Drinksrng,Colorrng)"
I always get a syntax error when executing:
Set rs = cn.Execute(strSql)
What is the correct way to export to the Access database using the above method? Any/all information will be super helpful as I am brand new to Access
My full code:
Foodrng = Workbooks(xlFile).Sheets("ToBeExported").Range("D6")
Drinksrng = Workbooks(xlFile).Sheets("ToBeExported").Range("E6")
Colorrng= Workbooks(xlFile).Sheets("ToBeExported").Range("B12:B21")
Set cn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source= C:\Users\User1\MyDBase.accdb"
cn.Open strConnection
strSql = "INSERT INTO Data (Food, Drinks, Color) VALUES (Foodrng, Drinksrng,Colorrng)"
Set rs = cn.Execute(strSql)
'MsgBox rs.Fields(0) & " rows in MyTable"
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
strSql = "INSERT INTO Data (Food, Drinks, Color) VALUES (" & Foodrng & ", " & Drinksrng & ", " & Colorrng & ")"
(You may also tryFoodrng.Address
if it doesn't work) – BruceWayne