Exporting data from Access 2007 to Excel 2007 using VBA

mrblack85

New Member
Joined
Oct 24, 2011
Messages
2
Hi, i am automating data transfer from Access 2007 to Excel 2007 and am having trouble with setting up the connection.

below is what i have so far but i am having no luck with connecting to the data base via the connection string an defining my record set.

i am also having trouble with figuring out how to paste the data selected from the table over to excel.

Any ideas????

Dim strMyDB As String
Dim conMyDB As ADODB.Connection
Dim rstMyDB As ADODB.Recordset
Dim strQuery As String

strMyDB = "C:\Users\Shadow\Documents\University\Year 4 Information systems\Project\DataBase.accdb;"
Set conMyDB = New ADODB.Connection
conMyDB.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & strMyDB & "';" _
& "ACE.OLEDB"

Set rstMyDB = New ADODB.Recordset
strQuery = "SELECT * FROM Table2"
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello and welcome to The Board.
There may be some errors in the following - I have just done a quick bit of copy/paste to put it together:
Code:
Sub MyProc()
Dim wb As Workbook
Dim ws As Worksheet
Dim objConn As ADODB.Connection
Dim objRS As ADODB.Recordset
Dim strDB_Path As String
Dim strSQL As String
Set wb = ThisWorkbook
Set ws = wb.Worksheets("My Sheet")
strDB_Path = "C:\MyDatabases"
'
ws.Activate
            '
            strSQL = "SELECT * FROM MyDatabaseTable;"
            Set objConn = New ADODB.Connection
            objConn.Provider = "Microsoft.ACE.OLEDB.12.0"
            objConn.Open strDB_Path
            If objConn.State = adStateOpen Then
                Set objRS = New ADODB.Recordset
                objRS.Open strSQL, objConn, adOpenForwardOnly
                '
                On Error Resume Next
                objRS.MoveFirst
                If Err.Number = 0 Then
                    On Error GoTo 0
                    ws.Cells(2, 1).CopyFromRecordset objRS
                End If
            End If
On Error Resume Next
    objRS.Close
    Set objRS = Nothing
    objConn.Close
    Set objConn = Nothing
    Set ws = Nothing
    Set wb = Nothing
End Sub
Hopefully you can adapt this for your needs.
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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