VB to update an Access Query

brendanolear

Active Member
Joined
Apr 26, 2008
Messages
366
I have a current Access Query which produces a table of 500 different companies, and thereafter has columns Week 01 to Week 53 with a Sum(IIF( formula that captures all "Sales" in any given week for each.

I wish to copy the query and replace "Sales" in all 53 columns with "Contacts" but only way I know how to do is manually across all 53 columns.

Would a VB script allow, or any other process to quickly change.

Thank You
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You can use a querydef to do this. Let's say you save your SQL strings into a memo field in a table along with an ID field. Let's say the table name is tblSQLStrings and the fields are SQLID (autonumber PK) and sSQL (Memo), this is something you could do (you would put this function into a STANDARD module and not a form, report or class module and you would name the module something other than the function name):

Code:
Function SwapSQL(strQueryName As String, lngID As Long)
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
 
strSQL = Nz(DLookup("sSQL", "tblSQLStrings", "SQLID = " & lngID), vbNullString)
 
If strSQL <> vbNullString Then
   Set db = Currentdb
   Set qdf = Currentdb.QueryDefs(strQueryName)
 
   qdf.SQL = strSQL
   qdf.Close
   Set qdf = Nothing
Else
   MsgBox "There is no SQL String available from ID: " & lngID & vbCrLF & _
              "Please contact your administrator for assistance", vbInformation, "Missing ID"
End If
 
End Function

Then to call this you just use

SwapSQL "qryMyBaseQuery", 4

and so you would have a base query and then you could use this to modify the SQL any time.

Hope that helps.
 
Last edited:
Upvote 0
I'm thinking maybe you just need a quick and dirty way to change this.

How about converting it to SQL view, copying it to Word and using the Find & Replace utility there?
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,818
Members
452,946
Latest member
JoseDavid

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