Transfer Excel data to Access - excel

I'm following the instructions here http://software-solutions-online.com/excel-vba-export-worksheet-to-existing-access-table/ to transfer data from an Excel spreadsheet to an Access database. The script I have adapted is:
Sub MailMerge2()
Dim strPath As String
Dim objAccess As Access.Application
Dim strExcelPath As String
strPath = "C:...Documents\MailMerge2"
strExcelPath = Application.ActiveWorkbook.FullName
Set objAccess = New Access.Application
Call objAccess.OpenCurrentDatabase(strPath)
objAccess.Visible = True
Call objAccess.DoCmd.TransferSpreadsheet(acImport, _
acSpreadsheetTypeExcel8, "MyTable1", strExcelPath, _
True, "A1:D11")
End Sub
However, running this gives me an error saying:
Run-time error: 7866, Microsoft Access can't open the database
because it is missing, or opened exclusively by another user, or it is
not an ADP file.
Any suggestions on which of these the problem is? I'm fairly new to Access, and haven't quite got the hang of the terminology yet.

Found the problem. I left out .accdb in my Access db file names.

Related

Connect Excel to Access Form via VBA

I wanted to connect my Excel to my Access DB.
I want to extract Data from a Form into Excel.
Sub GetDataFromDB()
Dim conn As Object
Dim DBPATH, PRVD, connString, query As String
DBPATH = "...."
PRVD = "Microsoft.ace.OLEDB.12.0;"
connString = "Provider=" & PRVD & "Data Source=" & DBPATH
Set conn = CreateObject("ADODB.Connection")
conn.Open connString
DoCmd.OpenForm Forms![frm_name], acFormDS, "", "", , acNormal
(Do Something)
End Sub
Now I get an error 424 that an Object is needed but I don't know to to get access to that specific Form in my DB.
Can someone help me please?
Best regards
Normally you get your data form a table or a query. May be you should
explain better what you want to accomplish. Getting data directly from
a form doesn't sound logical. If you want to open an access form try;
Ps, Dim declarations are not correct, Dim x as string, y as string
otherwise they will be variant.
Public Sub Displayform(conPath As String)
Dim objAccess As Access.Application
'Check that this is the path on YOUR PC for
'Northwind.mdb, which ships with Access.
'Const conPATH = "C:\Users\Myname\documents\PrintMe.accdb"
'Create an instance of the Access application object.
Set objAccess = CreateObject("Access.Application")
'Access and display the application window.
objAccess.OpenCurrentDatabase conPath
objAccess.Visible = True
'Open the Employees form.
'objAccess.DoCmd.OpenForm "Cust List"
' Maximize other Access window and quit current application.
objAccess.DoCmd.RunCommand acCmdAppMaximize
Application.DoCmd.RunCommand acCmdAppMinimize
End Sub

Excel VBA procedure crashes when opening a Access database connection/using Access Runtime

When executing a VBA procedure from Excel to connect to an Access database using Microsoft Access Runtime 2016 on my second laptop, the VBA procedure seems to freeze processing for a few seconds, crashes and closes the Excel application without triggering the error handler in my code so there is no error message for me to decipher. What can be the issue or how can I trap the error?
This VBA application works fine on my first laptop which has the full version of Access with no issues. The second laptop was working before I loaded Microsoft Access Runtime 2016. I was using a database application called "MDB Plus" which reads Access database files but now that doesn't work anymore.
I'm using the following:
OS: Windows 10,
MS Office: 2007,
MS Access Runtime 2007-2016,
Excel VBA 2007
To try to resolve this I: 1.) Uninstalled MS Access Runtime 2016 and the Excel application still crashed, 2.) Install MS Access Runtime 2007 and the Excel application still crashes.
Here is my code:
Sub TestGetTblPrimKey()
Dim oDBConn As ADODB.Connection
Dim sDBConnString As String
Dim moDBTblRecordSet As ADODB.Recordset
Const sDBTableLocPath As String _
= "C:\Users\kmass\AppData\Roaming\InvestManager\"
'
On Error GoTo ERROR_HANDLER
'
'Create Database connection
Set oDBConn = New ADODB.Connection
'Create Table Record-Set
Set moDBTblRecordSet = New ADODB.Recordset
'Build DB connection string
sDBConnString = _
"Provider=" & "Microsoft.ACE.OLEDB.12.0" & ";" _
& "Data Source='" _
& sDBTableLocPath _
& "tMeta_Table_Master.accdb" & "'"
'Open Database Table and Record-Set
oDBConn.Open sDBConnString '* <--CRASHES HERE
'
' Call ... the rest of the code to get record key
'
TestGetTblPrimKeyExit:
oDBConn.Close
'Release Table Objects
Set moDBTblRecordSet = Nothing
Set oDBConn = Nothing
Exit Sub
'
ERROR_HANDLER:
Debug.Print Err.Number & vbCrLf & Err.Description
Resume TestGetTblPrimKeyExit
'
End Sub
I expected the code to connect to the Access Database.
You can easily control Access from Excel, and do all kinds of things within the Access object, from Excel. Here are three simple demos of what you can do.
1)
'Open MS Access Form, from Excel
Global oApp As Object
Sub OpenAccess()
Dim LPath As String
Dim LCategoryID As Long
'Path to Access database
LPath = "C:\your_path_here\Northwind.mdb"
'Open Access and make visible
Set oApp = CreateObject("Access.Application")
oApp.Visible = True
'Open Access database as defined by LPath variable
oApp.OpenCurrentDatabase LPath
'Open form of interest
oApp.DoCmd.OpenForm "Form1"
End Sub
2)
'RUN MS ACCESS MACRO FROM EXCEL:
Sub AccessTest1()
Dim A As Object
Set A = CreateObject("Access.Application")
A.Visible = False
A.OpenCurrentDatabase ("C:\your_path_here\Northwind.mdb")
A.Application.Run "ExportToExcelTest"
End Sub
3)
' Run a delete query in MS Access, from Excel
Sub OpDaHus01()
Dim strDatabasePath As String
Dim appAccess As Access.Application
Dim strSQL As String
strDatabasePath = "C:\your_path_here\Northwind_2012.mdb"
strSQL = "DELETE tblTest.* FROM tblTest;"
Set appAccess = New Access.Application
With appAccess
.OpenCurrentDatabase strDatabasePath
.DoCmd.RunSQL strSQL
.Quit
End With
Set appAccess = Nothing
End Sub
If you need to run the code from a Macro, it needs to be a Public Function (rather than Sub), and it needs to be in a standard module (not a Form, Report or Class module).

How to export data from Excel to Access via Excel Macro when multiple instances of Excel are open

I am using an Excel macro to define a data range in Excel and then call "objAccess.DoCmd.TransferSpreadsheet" to import the data from this range into an Access table. This import does not work all of the time.
I have come to the conclusion that the macro works fine when there is only one instance of Excel open. However, the data import fails when another instance is already open. In the latter case the Access database opens up and the Excel file from which I run the macro is being reopened (in read-only mode) in the other Excel instance. There is no actual error but the desired import is not being carried out. Why does this happen?
Sub Excel_2_Access()
Dim strPath As String
Dim strwbPath As String
Dim strRange As String
Dim objAccess As Access.Application
Dim wbActive As Workbook
'get database path
strPath = Worksheets("error").Range("Access_DB_Path").Value & "\" & Worksheets("error").Range("Access_DB").Value
'open database
Set objAccess = New Access.Application
Call objAccess.OpenCurrentDatabase(strPath)
objAccess.Visible = True
'access import
Worksheets("error").Columns("P:P").Calculate
Set wbActive = ActiveWorkbook
strwbPath = Application.ActiveWorkbook.FullName
strRange = "error!M2:M" & (Worksheets("error").Range("WKN_count").Value + 2)
Call objAccess.DoCmd.TransferSpreadsheet(acImport, 8, "WKN_Mapping", strwbPath, True, strRange)
objAccess.Forms("MX_Import").Refresh
End Sub
As the macro is fairly short I have included the entire code for your reference. However, I don't think the way the range is specified or names are provided is really relevant to the question.
The desired outcome would be to have an Excel macro in place that carries out the transfer from Excel to Access no matter if there are other instances of Excel open or not.
Is there such a thing as the primary instance of Excel (the first one that was opened) which has a special status? Is there a way to provide the specific Excel instance the workbook is in when calling the Access function from Excel? Or is there a more reliable way to transfer the data which generally avoids this problem with multiple instances?
I tested approach that opens Access db and runs TransferSreadsheet. Don't need to set a workbook object (your code sets but then doesn't even utilize). It ran without error every time. I tried setting the Access object Visible but the database appears and immediately closes anyway, although the data import does happen. Set reference libarary: Microsoft Access x.x Object Library.
Sub test()
Dim ac As Access.Application, strRange As String
Set ac = New Access.Application
strRange = "Sheet1!A1:E3"
ac.OpenCurrentDatabase "C:\Users\June\LL\Umpires.accdb"
ac.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Rates", ThisWorkbook.FullName, True, strRange
End Sub
Example Excel VBA code that exports all rows of worksheet to existing table in Access without opening Access file. Setting an ADODB connection makes the Execute method available. This approach runs faster. Set reference library: Microsoft ActiveX Data Objects x.x Library.
Sub test()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=Excel 8.0"
cn.Execute "INSERT INTO Rates(RateLevel, Rate, Pos, EffDate) IN 'C:\Users\June\LL\Umpires.accdb' " & _
"SELECT RateLevel,Rate,Pos,EffDate FROM [Sheet1$];"
cn.Close
Set cn = Nothing
End Sub

From MS Excel VBA, create a Standard Module in MS Access

From VBA in Excel (2013), I need to create a standard module in a MS Access (2013) database. I am open to any and all ideas - Thanks!
OK, I have it working, but I am sure there are aspects that could/should be done differently. As a reminder, the code below is in MS Excel. Here is the code:
'REQUIRES: MS Access 12.0 Object Library
Dim ObjAccess As Access.Application
Dim strPath As String
strPath = "C:\Temp\MyDB_DEV_1.MDB"
Set ObjAccess = New Access.Application
With ObjAccess
.OpenCurrentDatabase strPath, True
.DoCmd.RunCommand acCmdNewObjectModule
.DoCmd.Save acModule, "Module1"
.DoCmd.Rename "MyCodedModule", acModule, "Module1"
.CloseCurrentDatabase
.Quit
End With
Set ObjAccess = Nothing`
The natural evolution of the previous solution is to want to add code to the new module. I decided to take an alternate route and "import" an existing standard module into the remote MS Access db. A final step in the evolution is to remotely run a procedure that is within the standard module that was imported into the remote db. Again, this code is located in Excel VBA. The code that accomplishes these two tasks are as follows:
'FROM EXCEL, REMOTELY INSTRUCT A MS ACCESS db
'IMPORTS .BAS FILE INTO db
'EXECUTE THE 'RoutineWithinModule1' THAT IS WITHIN MODULE1 OF THE REMOTE db
Dim appAccess As Access.Application
Set appAccess = New Access.Application
With appAccess
.OpenCurrentDatabase "C:\Temp\MyDB_DEV_1.mdb"
.VBE.ActiveVBProject.VBComponents.Import "C:\Module1.bas"
.DoCmd.Save acModule, "Module1"
.Run "RoutineWithinModule1"
.CloseCurrentDatabase
.Quit
End With
Set appAccess = Nothing

Import Excel Range into Access ISAM error

So I've been trying to put together this last piece of this VBA script and the connection to the database is giving me problems. The database opens but right after I get and error could not find installable isam I tried to register the DLL, no luck, I tried different versions in the connection, no luck.
At this point I am at a loss. All this is trying to do is import a range in an Excel sheet to the Access database. If anyone has a reason why it is giving me this error, please help.
Sub Open_Database()
Dim appAccess As Object
Dim pathToDatabase As String
pathToDatabase = Environ("USERPROFILE") + "\Desktop\FuturesDemandScheduling - Copy.mdb"
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase pathToDatabase, True, "password"
Windows("FuturesFIle-2014.xlsx").Activate
appAccess.DoCmd.TransferSpreadsheet acImport, _
acSpreadsheetTypeExcel3, _
"futuresData", _
Application.ActiveWorkbook.FullName, _
False, _
"A2:G491"
appAccess.CloseCurrentDatabase
appAccess.Quit
Set appAccess = Nothing
End Sub
I suspect the problem is the way you are importing the excel data into the table
Since you said the version of excel is 2010 and you are using .xlsx file extension in the code
Try
acSpreadsheetTypeExcel12Xml instead of acSpreadsheetTypeExcel3
acSpreadsheetTypeExcel3 supports Excel 3.0 Format
acSpreadsheetTypeExcel12Xml supports Excel 2010
Complete list here

Resources