I am trying to import an MS Access query into excel without triggering the log-in prompt. I have attempted this operation a few different ways, but both methods haven't given me a complete solution.
Specifics:
My access query source is an unprotected access database file (database1.accdb) built in MS Access 2010. This database gets tables from different sources (by use of linked tables) and performs data processing. One of these sources requires a password, so when I run the query, a log-in prompt comes up asking me for credentials (which I have). I have no issues with the query itself.
My excel spreadsheet (built in excel 2010) contains VBA code that retrieves tables from other data sources and some of them require authentication as well, so I built a custom prompt that lets a user enter credentials for all the tables.
The problem here is that I have a prompt coming up in the excel spreadsheet that asks the user for log-in information, but then another prompt comes up when the access query is imported. Here's what I've tried to do to handle the problem:
Method 1: Using the Macro Recorder:
I used excel's built in macro recorder to follow my manual steps in importing the access query. When I'm recording the macro, the imports works and the query comes in with no errors as expected. However, when I try to run the macro, I get a runtime error:
"Run-time error '1004':
The query did not run, or the database could not be opened. Check the database
server or contact your database administrator. Make sure the external database
is available and has not been moved or reorganized, then try the operation
again."
Code from Macro Recorder:
Sub Macro2()
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;" _
, "Data Source=C:\Database1.accdb;Mode=Share Deny Write;" _
, "Extended Properties="""";Jet OLEDB:System database="""";" _
, "Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";" _
, "Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;" _
, "Jet OLEDB:Global Partial Bulk Ops=2;" _
, "Jet OLEDB:Global Bulk Transactions=1;" _
, "Jet OLEDB:New Database Password="""";" _
, "Jet OLEDB:Create System Database=False;" _
, "Jet OLEDB:Encrypt Database=False;" _
, "Jet OLEDB:Don't Copy Locale on Compact=False;" _
, "Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;" _
, "Jet OLEDB:Support Complex Data=False;" _
, "Jet OLEDB:Bypass UserInfo Validation=False"), _
Destination:=Range("$A$4")).QueryTable
.CommandType = xlCmdTable
.CommandText = Array("Query3")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Database1.accdb"
.ListObject.DisplayName = "Table_Database1"
.Refresh BackgroundQuery:=False
End With
Range("I3").Select
End Sub
My guess as to why this macro doesn't work (but the manual steps do) is because some parameters are ignored by the recorder. If I removed the quotes from some of the password fields, the code doesn't error out, but I get the log-in prompt again. I was hoping someone on here can see if there's a missing parameter or an incorrectly assigned parameter.
Method 2: Using the DAO Library:
For this method, I had to make a few changes. First I had to add a reference in my editor for "Microsoft DAO 3.6 Object Library". Then I had to covert my .accdb file to a .mdb file so I can use the DAO functions:
Code for DAO Method:
Sub Macro3()
Dim db1 As Database
Dim db2 As Database
Dim recSet As Recordset
Dim strConnect As String
Set db1 = OpenDatabase("C:\Database1.mdb")
strConnect = db1.QueryDefs("Query3").Connect _
& "DSN=myDsn;USERNAME=myID;PWD=myPassword"
Set db2 = OpenDatabase("", False, False, strConnect)
db2.Close
Set db2 = Nothing
Set recSet = db1.OpenRecordset("Query3")
With ActiveSheet.QueryTables.Add(Connection:=recSet, Destination:=Range("$A$4"))
.Name = "Connection"
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
recSet.Close
db1.Close
Set recSet = Nothing
Set db1 = Nothing
End Sub
This method works and I can bypass the database's log-in prompt... as long as my query doesn't return a large amount of records. When I was returning up to ~60,000 records, the code would not take more than 5-10 seconds to get a result. However, when I tried pulling more than ~100,000 records, excel would become unresponsive and hang (I let the code run for about 10 minutes before I stopped it). I'm thinking I've hit some limitation on the DAO, other than that I can't find documentation that addresses this.
Any assistance is appreciated.