Run-Time Error 7866 with OpenCurrentDatabase - excel

When I run the code below I get 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.
The error is thrown on this line
db.OpenCurrentDatabase "C:\Users\QE9142\Desktop\VES Mgmt Reports ONLY.mdb"
I am trying to open an Access database that is saved on my desktop through Excel.
Sub Task()
Dim db As Access.Application
Dim strDB As String
Set db = New Access.Application
db.OpenCurrentDatabase "C:\Users\QE9142\Desktop\VES Mgmt Reports ONLY.mdb"
db.Application.Visible = True
End Sub

I can't replicate this error, but when the code exits, so does Access.
Insert a STOP or other event that will hold the code:
Sub Task()
Dim db As Access.Application
Dim strDB As String
Set db = New Access.Application
db.OpenCurrentDatabase "C:\Users\QE9142\Desktop\VES Mgmt Reports ONLY.mdb"
db.Application.Visible = True
Stop
End Sub
As for the error, try opening the mdb in Access and save it as an accdb file (Access 2007 format).

If user should take control of database once opened, don't see need for connection object. Use Shell command to simply open database file. Then user can do whatever they want with it.
Shell "cmd /c " & Chr(34) & "C:\Users\QE9142\Desktop\VES Mgmt Reports ONLY.mdb" & Chr(34)

Related

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).

VBA runtime error: could not find file

Good day all. I have a problem that I simply cannot seem to solve. I am working on an excel program that retrieve data from and update an MS Access 2010 db. I am connecting with the db using ADODB.Connection and the file (access.accdb) resides in a file server. When the application first launches it uses form1 to get logon details from the user and checks if the user id exists within the db. This function resides within a module and works fine. When I attempt to open the same file from a sub within another user form (form2) I keep getting an error that the file could not be found. I am using the exact same path string as what is used within the module only that this time it is used from within a user form. Below is the code within Module2:
Dim con As Object
Dim rs As Object
Dim path As String
path = "path.accdb"
Set con = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
with con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source = " & path & "; Jet OLEDB:Database Password = pwd"
.Open
This works fine.
I use the same only from a user form and without the recordset object. I am not retrieving data from the form but rather updating the db. The form is used for data capturing only.
Any help will be appreciated. Thanks.
This is the code that I am using within form1:
Dim conn As Object
Dim strPath As String
strPath = "path.accdb" 'same as the path I used from within Module2
Set conn = CreateObject("ADODB.Connection")
with conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source = " & strPath & "; Jet OLEDB:Database Password = pwd"
.Open 'this is where I get the error.
'Rest of code
Error message:
Run-time-error '-2147467259 (80004005)':
Could not find file 'path.accdb'
I have checked the path and it is correct. I have checked for missing references. I must also mention that the database file is located on a folder on my company folder to which I do have access.
Simple syntax problem:
You declare strPath:
Dim strPath As String
strPath = "path.accdb" 'same as the path I used from within Module2
But then you call Path instead:
.ConnectionString = "Data Source = " & path & "; Jet OLEDB:Database Password = pwd"
It happens to all of us when we switch around the spelling of variables :)
** EDIT **
Ok... Let's try something different...
Why don't you open your database only once into a global variable?
Maybe opening it a second time in a subform is somehow clashing with your initial connection to the database.
Especially since you don't show us code where you close your connection.
A lot of clues are in your real code that you are not showing us. If making a global variable doesn't work, you may need to show us your real code with only sensitive info changed.

Transfer Excel data to Access

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.

Keep Excel sub runing after calling access sub procedure

within my Excel sub I am calling an Access sub procedure that runs in the background (DB does not open) and updates a table in my DB. Everything works perfect, except that my Excel Sub will not proceed to the next line of code until Access is done running it's sub I called from Excel. So, my question is this...is there any way to call/run an Access Macro/Sub procedure from within an Excel Sub and have Excel proceed through the rest of the code and not have to wait for the Access Macro/sub to finish in order to proceed? Code: below:
Set acObj = CreateObject("Access.Application")
acObj.Application.Visible = False
acObj.OpenCurrentDatabase "C:\Intraday Data\Intraday.accdb"
acObj.Application.Run "RunData"
MsgBox "Done!"
So Basically I want to get the done prompt right away w/o having to wait 30 seconds for the access procedure to finish...anyone have any insight on this they can share w/ me please?
Thanks!
Here's some simple code to demonstrate an asynchronous call to an Access procedure:
Sub ExecuteAccessActionQuery()
' Sample demonstrating how to execute an action query in an Access accdb asynchronously
' Requires a reference to a Microsoft ActiveX Data Objects library
Dim cn As ADODB.Connection
Dim strQuery As String
Dim strPathToDB As String
Dim dTimer As Double
' Change path and query name as necessary
strPathToDB = "C:\some path\database name.accdb"
strQuery = "qmtTempTable"
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & strPathToDB & ";"
.Open
End With
dTimer = Timer
cn.Execute strQuery, , adCmdStoredProc + adAsyncExecute
MsgBox Timer - dTimer
Set cn = Nothing
End Sub
You can emulate an asynchronous call in VBA by creating a trigger in the client application which will then execute the required subroutine. This technique is described well here (note the 1 second time delay required a few posts down):
http://social.msdn.microsoft.com/Forums/en-US/0546f8eb-d786-4037-906e-1ee5d42e7484/asynchronous-applicationrun-call?forum=isvvba
Currently, you are manipulating data in the Access tables without initiating the application, so you can either use a new instance of Excel in which to create the trigger, or you can open the Access application and do it there (http://msdn.microsoft.com/en-us/library/office/aa213969%28v=office.11%29.aspx). Either way, you must have another independent application (process) within which to execute your 'asynchronous' routine.

connecting to a read-only Access db from Excel VBA

I have been a long-time visitor to stackoverflow, but this is my first question.
I'm trying to query an Access 2010 database in a folder to which the user has only read-only access.
strDBPath = <full path of accdb>
Set myConn = New ADODB.Connection
myConn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Mode=Read; Data Source=" & strDBPath
Set myRS = New ADODB.Recordset
myRS.ActiveConnection = myConn
The myConn.Open line gives a "file currently in use" error. If I move the accdb to a folder that the user can write, the code runs fine.
Thanks in advance for any advice!
Wie have also a connection with an Access DB but im quite sure we have also write permissions on the folder.
The Code we use is here.
Dim DB As DAO.Database
Dim QRY As DAO.QueryDef
Dim Rs As DAO.Recordset
Public Ersteller As String
'Prüft die Verbindung zur DB
Public Function OpenDB() As Integer
'Informationen über Database'
Dim Database As Variant
Database = Worksheets("Anträge").Range("B3").Value
'Check Datenbank Verbindung
On Error Resume Next
Set DB = CreateObject("DAO.DBEngine.120").OpenDatabase(Database)
If Err.Number <> 0 Then
MsgBox "Keine Verbindung zur Datenbank möglich!"
End If
OpenDB = Err.Number
End Function
Mayebe it helps. :)
Access to database in a read only folder is possible (of course Mode=Read) but only if this database is not used by any other user (it means there is no .ldb file). But then, there is only one user able to have an access. So anyone calling database (even if it is done by excel) locking it for any others.
:-(

Resources