Log in

View Full Version : Running Access Query from Excel - Password Prompt



telmacro
08-13-2008, 08:32 PM
Hi guys,

I am using VBA to run a few queries in Excel. The problem is that when the query refreshes, I am asked to enter the password to the database. I have selected "Remeber Password" in Microsoft Query, but it does not work. Can I use VBA to enter the password?



'When the file is opened:
Private Sub Workbook_Open()
Call Refresh
End Sub
Sub Refresh()

Sheets("Data1_Query").Activate
Range("a1").Select
Selection.QueryTable.Refresh BackgroundQuery:=True

Sheets("Data2_Query").Activate
Range("a1").Select
Selection.QueryTable.Refresh BackgroundQuery:=True
Sheets("Lookup").Activate
Range("J1").Select
Selection.QueryTable.Refresh BackgroundQuery:=True
'sort lookup table:
Columns("J:P").Select
Selection.Sort Key1:=Range("J2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

'now refresh all pivot tables in workbook
Dim pt As PivotTable
Dim ws As Worksheet
For Each ws In Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws


End Sub


Maybe there is a better way to do this?

Thank you!

telmacro
08-13-2008, 09:16 PM
Nevermind...I've been trying to figure this out for two days, but I took another look at it, after I put up this post. It has to do with the way I set up the tables in the original Access file. When I originally loaded the linked tables, I was given an option to "Remeber Password". I needed to go back and delete the tables, and reload them, this time selecting "Remeber Password". Even though I checked that off in Micrsoft Query, it did not work because it needs to be done in the original database file. I hope this helps someone out. (= If you have any questions regarding this, or if you can point out how to make my code more efficient, let me know.
Thanks everyone.