Run Access VBA from Excel

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hi there,

I would like the below code (The code is in excel). to create a Access database, and then import excel data from other file files. the code is not completed yet as I still need to loop a few things. the end result is to import excel data into access database, but because there is a size limitation in access (2GIG), I need the code to crete new access databases and then it will continue with the loop etcc... I know how to do most of that. but I am struggling to get an Access function to run from inside excel.

I am commented where I am getting the error.

Code:
Function DoImport()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, str_AccessName As String
Dim blnHasFieldNames As Boolean
Dim xlApp As Object, wb_Book As Object, objAccess As Object
Dim DoCmd As Object


Set objAccess = CreateObject("Access.Application")
Set xlApp = CreateObject("Excel.Application")
Set DoCmd = objAccess.Application


blnHasFieldNames = True


strPath = "D:\Dropbox\eXceler8\Projects\Excel Add-Ins\Clients\iRam Internal\Live files\iRAM_ADDIN_APP_01\SSF\SF\SFS\2PBI_DB\"
str_AccessName = "D:\Dropbox\eXceler8\New"
Call objAccess.NewCurrentDatabase(str_AccessName)
'Call objAccess.OpenCurrentDatabase("D:\Dropbox\eXceler8\New.accdb")
objAccess.Visible = True




 strTable = "CLIPPA SALES"
 strFile = Dir(strPath & "*.xlsx")
 
 Do While Len(strFile) > 0
    If InStr(strFile, " MAIN") > 0 Then
        Set wb_Book = xlApp.Workbooks.Open(strPath & strFile)
        wb_Book.Sheets("MAIN").Range("D:M,O:O,P:P,T:T,U:U,V:V,AC:AC,AD:AD,AG:AG,AJ:AJ,AK:AK,AB:AB,AA:AA,X:X").Delete
        strPathFile = strPath & strFile
      
        With objAccess
            .DoCmd.SetWarnings False
            .UserControl = True
            .DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames, "MAIN!" ' Gettng the error here <<<<<<<<<<<<<<<<<<<<
            .DoCmd.SetWarnings True
        End With
        
        Debug.Print strFile
        wb_Book.Close False
        Set wb_Book = Nothing
    End If


        Call DeleteImportErrorTables
       strFile = Dir()
       
 Loop
 Set xlApp = Nothing


End Function
Function DeleteImportErrorTables()


 Dim oTable As DAO.TableDef
 
For Each oTable In CurrentDb.TableDefs
If oTable.Name Like "*ImportErrors*" Then
CurrentDb.TableDefs.Delete oTable.Name
End If
Next oTable
 
End Function
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,221,418
Messages
6,159,795
Members
451,589
Latest member
Harold14

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