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!
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!