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