Appending data from excel to access using VBA

Yusheel

New Member
Joined
Mar 12, 2015
Messages
5
Hi all

Im trying to append data to send data from an excel spreadsheet to an access database using VBA, browsing the forums I found some old posts that illustrate how to do so, but I cant seem to get it to work.Please note I have never coded in Access before. Using excel and access 2007.

My code:

Public Sub DoTrans()


Dim appAccess As Object
Set appAccess = CreateObject("Access.Application")
Set cn = CreateObject("ADODB.Connection")
dbpath = "C:\mytemp\DatabaseYush.accdb"
dbWb = Application.ActiveWorkbook.FullName
dbWs = Application.ActiveSheet.Name
scn = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" & dbpath
dsh = "[" & Application.ActiveSheet.Name & "$]"
Call appAccess.OpenCurrentDatabase(dbpath)
appAccess.Visible = True
ssql = "INSERT INTO Mytable ([a], , [c])"
ssql = ssql & " SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh



'stuck here it says "operation is not allowed when object is closed". I tried to manually open the table and then run that portion but no luck
cn.Execute ssql




End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
solved:


Public Sub DoTrans2()




Set cn = CreateObject("ADODB.Connection")
dbPath = Application.ActiveWorkbook.Path & "\DatabaseYush.accdb"
dbWb = Application.ActiveWorkbook.FullName
dbWs = Application.ActiveSheet.Name
scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
dsh = "[" & Application.ActiveSheet.Name & "$]"
cn.Open scn


ssql = "INSERT INTO PRIDE ([a], , [c]) "
ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh




cn.Execute ssql




End Sub
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top