Hi I'm having a problem with creating a querytable via VBA, everything seems to work fine but I always get the initialization details window popup when the connection is actuall made i.e. when the querytable is refreshed.
The OLE DB initialization window has the correct data source but the other parameters are default or blank, if I click OK it works, or if I edit in the correct details it works... but why wont it just work silently without the popup window? I'm guessing there is something wrong or missing from my connection string.
This is the code snippet:
By the way this is coded in Access (hence the appXL Excel object), access creates a spreadsheet then creates the querytable.
Any help appreciated!sConnection = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";" & _ "Data Source=" & sDatabase & ";Mode=Share Deny Write;" Set rngDestination = appXL.Sheets("Template").Range("D8") 'Create QueryTable Set qryTable = appXL.Sheets("Template").QueryTables.Add(Connection:=sConnection, Destination:=rngDestination, SQL:=sSQL) 'Populate the querytable appXL.Application.ScreenUpdating = False qryTable.Refresh
Also further on I attpemt to delete the connection before closing the workbook but it doesnt seem to work, I havent debuged that yet but any thoughts welcome!
Edit: I forgot to mention, the data source is the database that this code module is in, so sDatabase contains the value of Application.CurrentDb.Name probably not important...With appXL.ActiveWorkbook .Connections(1).Delete .SaveAs FileName:=sServerPath & "\" & sReportName & " " & sMonth & " " & sYear & ".xls" .Close End With
Last edited by mrcoffee; 07-25-2011 at 09:27 AM.
OK, I've given up on the OLEDB connection string and am now using ODBC which seems to work fine:
THough i'd still appreciate if anyone can explain the OLEDB problem.sConnection = "ODBC;Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" & sDatabase & ";Uid=Admin;Pwd=;"
Which just leaves the issue of closing the connection, when I try to delete the connection with the code above there is a message on opening the resultant workbook saying that the query table has been discarded due to integrity issues, the workbook also opens up as "Reparied".
Any ideas what thats about and how to avoid it? The message doesnt come up if I leave the connection in place.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks