I'm am trying to create a query table using VBA in an open password protected Excel workbook called RustysWorkbook.xls. This works fine as long as I have NOT launched any other excel workbooks (on the windows taskbar) prior to executing my code.
But if i launch any other workbooks before opening RustysWorkbook.xls, something strange happens. First, Excel switches the focus from RustysWorkbook.xls to the workbook preceding it (on the windows taskbar), then prompts me for the password to RustysWorkbook.xls, then and reopens RustysWorkbook.xls in a different window.
This behavior begins at the following line of code => .Refresh BackgroundQuery:=False
How to I get my query to refresh successfully even if there is already another workbook preceding it ?
Here's my code
Sub QueryData()
On Error GoTo ErrHandler:
Dim wks As Worksheet
Dim DestinationRange As Range
Dim qtQuery As QueryTable
Dim connString As String
Dim sqlString As String
Dim CurrentDirectory As String
Set DestinationRange = wkstmpCombined.Range("A2")
For Each qtQuery In wkstmpCombined.QueryTables
qtQuery.Delete
Next qtQuery
wkstmpCombined.Range(DestinationRange, "Z15000").Clear
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Build connection string path using current location of workbook.
If (Left(ThisWorkbook.Path, 1) = "\") Or (Left(ThisWorkbook.Path, 1) = "C") Then
CurrentDirectory = ThisWorkbook.Path & "\"
Else
CurrentDirectory = GetNetPath(Left(ThisWorkbook.Path, 1))
End If
'Assign connection string
connString = "ODBC;Driver={Microsoft Excel Driver (*.xls)};DriverId=790;" & "Dbq=" & CurrentDirectory & ActiveWorkbook.Name & ";" & "DefaultDir=" & CurrentDirectory & ";"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Build query SELECT statement
sqlString = " SELECT *"
'Build query FROM statement
sqlString = sqlString & " FROM {oj `wkstmpPrimary$` `wkstmpPrimary$`" & " LEFT OUTER JOIN `wkstmpAncillary$` `wkstmpAncillary$`" & " ON `wkstmpPrimary$`.PK = `wkstmpAncillary$`.PK} "
'Add querytable w/ dynamic SQL
Set qtQuery = wkstmpCombined.QueryTables.Add(connString, DestinationRange, sqlString)
With qtQuery
.Refresh BackgroundQuery:=False
.RefreshStyle = xlOverwriteCells
End With
ExitFinally:
Set DestinationRange = Nothing
Set qtQuery = Nothing
Exit Sub
ErrHandler:
If Err.Number = vbObjectError + 1001 Then
MsgBox "Your request returned too many results." & "Please refine your search.", vbInformation, "Result Error"
Err.Clear
Else
GoTo ExitFinally
End If
End Sub
But if i launch any other workbooks before opening RustysWorkbook.xls, something strange happens. First, Excel switches the focus from RustysWorkbook.xls to the workbook preceding it (on the windows taskbar), then prompts me for the password to RustysWorkbook.xls, then and reopens RustysWorkbook.xls in a different window.
This behavior begins at the following line of code => .Refresh BackgroundQuery:=False
How to I get my query to refresh successfully even if there is already another workbook preceding it ?
Here's my code
Sub QueryData()
On Error GoTo ErrHandler:
Dim wks As Worksheet
Dim DestinationRange As Range
Dim qtQuery As QueryTable
Dim connString As String
Dim sqlString As String
Dim CurrentDirectory As String
Set DestinationRange = wkstmpCombined.Range("A2")
For Each qtQuery In wkstmpCombined.QueryTables
qtQuery.Delete
Next qtQuery
wkstmpCombined.Range(DestinationRange, "Z15000").Clear
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Build connection string path using current location of workbook.
If (Left(ThisWorkbook.Path, 1) = "\") Or (Left(ThisWorkbook.Path, 1) = "C") Then
CurrentDirectory = ThisWorkbook.Path & "\"
Else
CurrentDirectory = GetNetPath(Left(ThisWorkbook.Path, 1))
End If
'Assign connection string
connString = "ODBC;Driver={Microsoft Excel Driver (*.xls)};DriverId=790;" & "Dbq=" & CurrentDirectory & ActiveWorkbook.Name & ";" & "DefaultDir=" & CurrentDirectory & ";"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Build query SELECT statement
sqlString = " SELECT *"
'Build query FROM statement
sqlString = sqlString & " FROM {oj `wkstmpPrimary$` `wkstmpPrimary$`" & " LEFT OUTER JOIN `wkstmpAncillary$` `wkstmpAncillary$`" & " ON `wkstmpPrimary$`.PK = `wkstmpAncillary$`.PK} "
'Add querytable w/ dynamic SQL
Set qtQuery = wkstmpCombined.QueryTables.Add(connString, DestinationRange, sqlString)
With qtQuery
.Refresh BackgroundQuery:=False
.RefreshStyle = xlOverwriteCells
End With
ExitFinally:
Set DestinationRange = Nothing
Set qtQuery = Nothing
Exit Sub
ErrHandler:
If Err.Number = vbObjectError + 1001 Then
MsgBox "Your request returned too many results." & "Please refine your search.", vbInformation, "Result Error"
Err.Clear
Else
GoTo ExitFinally
End If
End Sub