Update Embedded Query using VBA

tonycuban

New Member
Joined
Oct 16, 2009
Messages
11
This was a request from my boss and i can't get it to work properly

The workbook have multiple worksheets. Most of the worksheets have embedded queries from A MS Access database. The intention is to update all the embedded queries by clicking on a button if the location of the database were to change.
I wrote the following

Private Sub cmd_Update_Qry_Click()
On Error GoTo cmd_Update_Qry_Err

Application.EnableCancelKey = xlDisabled

strDbPath = Range("DbPathFolder").Value
strOldDbPath = Range("DbOldPathFolder").Value

For Each oworkSheet In ThisWorkbook.Worksheets

If oworkSheet.QueryTables.Count <> 0 Then

strWorksheetName = oworkSheet.Name

For Each qt In oworkSheet.QueryTables
strQryName = qt.Name
strSQL = qt.CommandText
strConnection = qt.Connection

strConnection = Application.WorksheetFunction.Substitute(strConnection, strOldDbPath, strDbPath)

strSQL = Application.WorksheetFunction.Substitute(strSQL, strOldDbPath, strDbPath)

Set qtConnection = ThisWorkbook.Worksheets(strWorksheetName).QueryTables(strQryName)
qtConnection.Connection = strConnection
qtConnection.CommandText = strSQL
Next qt

End If
Next

Set qt = Nothing
Set qtConnection = Nothing

MsgBox "Done!"

Exit Sub

cmd_Update_Qry_Err:
Select Case Err.Number
Case 1004
Resume Next
Case Else
End Select

End Sub


Problem #1
Sometimes the SQL doesn't get updated on any of the worksheets. Sometimes some get updated and some don't.
It never, not even once, updated all the worksheets

Problem #2
If the SQL gets updated, when the data is refresh it works but:
when try to see the SQL by going Data - Import External Data - Edit Query I get the message "[Microsoft][ODBC Microsoft Access Driver] Record is deleted."

Thanks for your time
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Thanks for your reply Andrew.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I added line <o:p></o:p>
qtConnection.Refresh<o:p></o:p>
after<o:p></o:p>
Set qtConnection = ThisWorkbook.Worksheets(strWorksheetName).QueryTables(strQryName)
qtConnection.Connection = strConnection
qtConnection.CommandText = strSQL<o:p></o:p>
<o:p></o:p>
both problems remain.<o:p></o:p>
#1: The workbook has 15 worksheet. It updates the sql in the first worksheet, skips worksheet 2 to 4, and updates the rest.<o:p></o:p>
<o:p></o:p>
#2: The worksheets were the SQL is updated, when try to see the SQL by going Data - Import External Data - Edit Query I get the message "[Microsoft][ODBC Microsoft Access Driver] Record is deleted."<o:p></o:p>
 
Upvote 0
I don’t understand your question.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
The background query is strQryName<o:p></o:p>
<o:p> </o:p>
Set qtConnection = ThisWorkbook.Worksheets(strWorksheetName).QueryTables(strQryName)<o:p></o:p>
<o:p> </o:p>
And strQryName gets set in:
<o:p> </o:p>
If oworkSheet.QueryTables.Count <> 0 Then<o:p></o:p>
<o:p></o:p>
strWorksheetName = oworkSheet.Name<o:p></o:p>
<o:p></o:p>
For Each qt In oworkSheet.QueryTables<o:p></o:p>
strQryName = qt.Name
Thanks
 
Upvote 0
No, the BackgroundQuery property can be True or False. If True queries are performed asynchronously (in the background).
 
Upvote 0
i checked the external Data range Properties and the background refresh is enabled

i did try the code both way, with the same result
qtConnection.Refresh True
qtConnection.Refresh False

Is there is anything I should add to my original code beside qt.Connection.Refresh True

Thanks
 
Upvote 0
Is there is anything I should add to my original code beside qt.Connection.Refresh True

Not that I can think of. If BackgroundQuery is Dalse the macro should wait until it's finished before moving on.

Why are you doing this:

Code:
Set qtConnection = ThisWorkbook.Worksheets(strWorksheetName).QueryTables(strQryName)
qtConnection.Connection = strConnection
qtConnection.CommandText = strSQL

rather than?

Code:
qt.Connection = strConnection
qt.CommandText = strSQL
 
Upvote 0
Thanks.
I changed it as per your suggestion

We found the problems !!!!
Problem #1 - The Substitute function is case sensitive and the database old path in some of the SQLs where lower case, so, before running the substitute function i use the UCASE function to ensure that the string searched is found

Problem #2 - The new database needed to be "compacted and repaired"

For any one who may need the code here is again after been fixed:

Private Sub cmd_Update_Qry_Click()
On Error GoTo cmd_Update_Qry_Err
Application.EnableCancelKey = xlDisabled

strDbPath = Range("DbPathFolder").Value
strOldDbPath = UCase(Range("DbOldPathFolder").Value)

If strDbPath = "" Or strOldDbPath = "" Then Exit Sub

For Each oworkSheet In ThisWorkbook.Worksheets

If oworkSheet.QueryTables.Count <> 0 Then

strWorksheetName = oworkSheet.Name

For Each qt In oworkSheet.QueryTables
strQryName = qt.Name
strSQL = UCase(qt.CommandText)
strConnection = UCase(qt.Connection)

strConnection = Application.WorksheetFunction.Substitute(strConnection, strOldDbPath, strDbPath)

strSQL = Application.WorksheetFunction.Substitute(strSQL, strOldDbPath, strDbPath)

qt.Connection = strConnection
qt.CommandText = strSQL
qt.Refresh False
Next qt
End If
Next

Range("DbOldPathFolder").Value = strDbPath
Range("DbPathFolder").Select
Selection.ClearContents

Set qt = Nothing
Set qtConnection = Nothing

MsgBox "Done!"

cmd_Update_Qry_Exit:
Exit Sub
cmd_Update_Qry_Err:
MsgBox Err.Number & " " & Err.Description
GoTo cmd_Update_Qry_Exit
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top