VBA to move worksheet to MS access.

jvoss

Board Regular
Joined
Jun 13, 2015
Messages
51
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
problem 1 need to move a worksheet from a closed workbook to a closed ACCESS DB. should be simple yes?

problem 2 this worksheet can not append current access table it must OVERWRITE the access table. (table is used in an access query with joins).

this code will run from current workbook and use late binding to run..

i would like to add the code to import worksheet before the query is ran

this will be done 100's of times a day.

VBA Code:
Option Explicit
Sub RunExistingQuery()
'------------------------------------------------------------------------------------
'This macro opens the Sample.accdb database and runs the (existing) qrRegions query
'(counting the number of customers from each region, based on table Customers).
'Then, it copies all the query results back in the Excel sheet.
'The code uses late binding, so no reference to an external library is required.
'Written By: Christos Samaras
'Date: 05/10/2013
'Last Updated: 29/11/2014
'E-mail: xristos.samaras@gmail.com
'Site: https://www.myengineeringworld.net
'------------------------------------------------------------------------------------
'Declaring the necessary variables.
Dim con As Object
Dim rs As Object
Dim AccessFile As String
Dim strQuery As String
Dim i As Integer
'Disable screen flickering.
Application.ScreenUpdating = False
'Specify the file path of the accdb file. You can also use the full path of the file like:
'AccessFile = "C:\Users\Christos\Desktop\Sample.accdb"
AccessFile = ThisWorkbook.Path & "\" & "Sample.accdb"
'Set the name of the query you want to run and retrieve the data.
strQuery = "qrRegions"
On Error Resume Next
'Create the ADODB connection object.
Set con = CreateObject("ADODB.connection")
'Check if the object was created.
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection Error"
Exit Sub
End If
On Error GoTo 0
'Open the connection.
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
On Error Resume Next
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Check if the object was created.
If Err.Number <> 0 Then
'Error! Release the objects and exit.
Set rs = Nothing
Set con = Nothing
'Display an error message to the user.
MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
Exit Sub
End If
On Error GoTo 0
'Set thee cursor location.
rs.CursorLocation = 3 'adUseClient on early binding
rs.CursorType = 1 'adOpenKeyset on early binding
'Open the recordset.
rs.Open strQuery, con
'Check if the recordset is empty.
If rs.EOF And rs.BOF Then
'Close the recordset and the connection.
rs.Close
con.Close
'Release the objects.
Set rs = Nothing
Set con = Nothing
'Enable the screen.
Application.ScreenUpdating = True
'In case of an empty recordset display an error.
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
Sheets("Existing Access Query").Cells(1, i + 1) = rs.Fields(i).Name
Next i
'Write the query values in the sheet.
Sheets("Existing Access Query").Range("A2").CopyFromRecordset rs
'Close the recordset and the connection.
rs.Close
con.Close
'Release the objects.
Set rs = Nothing
Set con = Nothing
'Adjust the columns' width.
Columns("A:B").AutoFit
'Enable the screen.
Application.ScreenUpdating = True
'Inform the user that the macro was executed successfully.
MsgBox "All data were successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"
End Sub
 

Some videos you may like

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Watch MrExcel Video

Forum statistics

Threads
1,114,235
Messages
5,546,649
Members
410,752
Latest member
MC01_
Top