Can I programatically replace things inside my queries?

Access9001

Registered User.
Local time
Today, 13:03
Joined
Feb 18, 2010
Messages
268
For instance, say I have a bunch of queries like so:

select a, b from [pathway to db.mdb].RandomQuery


and I want to programatically change them to this:

select a, b from [a new pathway.mdb].RandomQuery
 
currentdb.querydefs("YourQueryName").sql will retrieve your sql and you can replace stuff in there and write it back into your sql

Lookup querydefs in the VBA help and be a little creative...
 
Is there a way to cycle through all queries in my db without needing the name?
 
Is there a way to cycle through all queries in my db without needing the name?
This would:
Code:
Function ReplaceQueryPart(strTextToReplace As String, strTextToUse As String)
 
Dim qdf As DAO.QueryDefs
Dim strSQL As String
 
For Each qdf in Currentdb.QueryDefs
     strSQL = qdf.SQL
        If Instr(1, strTextToReplace, strSQL) > 0 Then
           strSQL = Replace(strSQL, strTextToReplace, strTextToUse)
           qdf.SQL = strSQL
           qdf.Close
        End If
Next

(untested air code)

But you might not want to change all of them so then you would need to do some sort of checking.
 
I decided the other day, I needed a search form, to locate where certain fields were used within queries.

I was going to extend this to forms and reports, but haven't done it yet.

You may find it useful.


View attachment DBS Search.zip
 
For large queries, that routine takes forever. Is there any way to just edit the SQL without needing to "execute" it on the backend to get all its properties when I run code? ALL I want to do is be able to mass-replace things in the SQL in a flash.
 
For large queries, that routine takes forever. Is there any way to just edit the SQL without needing to "execute" it on the backend to get all its properties when I run code? ALL I want to do is be able to mass-replace things in the SQL in a flash.


1. The code I gave doesn't execute anything. It just replaces the SQL within the query.

2. If you want a faster way of doing it (don't know how much faster it will be or if at all) you can try using the free V-Tools Total Deep Search and see how long it takes it. But I don't think you can programmatically use that.
 
The code works but it always seems to hang on the following

For Each qdf in Currentdb.QueryDefs
strSQL = qdf.SQL

for large queries


Basically I am moving a bunch of Access DBs to a new server and I need a way to update any hardlinks that may exist (relink tables, any links in-queries, any VBA linkages, etc)
 
The code works but it always seems to hang on the following

For Each qdf in Currentdb.QueryDefs
strSQL = qdf.SQL

for large queries


Basically I am moving a bunch of Access DBs to a new server and I need a way to update any hardlinks that may exist (relink tables, any links in-queries, any VBA linkages, etc)

Well, the part you point out is needed.

Second, if I were you I would NOT hard code anything in any queries or anything. Use tables to store data/properties that may change. It is much faster to change one entry in a table than it is to change hundreds of instances where the database has to search to replace.
 
Is there an easy way to do mass-replacements within tables in the same way as queries?
 
Is there an easy way to do mass-replacements within tables in the same way as queries?

What do you mean "mass replacements?" It would only be ONE record in a preferences table for the pathway for your mdb. You would need to modify your queries so that they used a lookup to the table instead of the direct path. After you have that done the first time, then you would only need to change the path in the one table in the one record in the one field and all of it is done. I don't understand why you think there would be mass replacements needed in tables.
 
The current network of databases is very automated and has many interlinkages -- exporting systems and so forth that have export paths listed as elements within tables and so forth.

But since this will need to go to a new server, all sorts of links will need to be updated. It's not a system I made.
 
You can rename Linked table sources much like the SQL above.

If you want to addapt values in queries it is update queries you are looking for.
 

Users who are viewing this thread

Back
Top Bottom