views:

475

answers:

2

I have an access 2003 database file with hundreds of queries. I would like to rename all tables referenced from within my queries based on a condition

If tableNameInQuery = "tableName" Then

    tableNameInQuery = "newTableName"

End If

Any suggestions or articles would be great.

Thanks

PS an example in either C# or VB.NET would be appreciated

A: 

You could check out Black Moshannon's Speed Ferret. This tool is not free but I think it is relatively inexpensive and will give you VERY reliable table renaming. If your time is worth something check it out.

One thing to remember if you use Speed Ferret or some other like tool...and that is the ORDER you rename the tables is pretty important. Be sure to put some thought into it.

Seth Spearman

Seth Spearman
I dont think i made myself clear, I want to rename the reference to the table inside the query. Not the table itself?Will check the link out now.ThanksBen
Ben
In other words you want to change WHICH table is referenced inside of a query. Not the table name. You can still do that with Speed Ferret.
Seth Spearman
yes that is correct. The lastest version of Speed Ferret only supports up to access 2002.
Ben
David-W-Fenton
+3  A: 

I have a quick & dirty VBA sub which does what I think you want. (Can you translate it to one of your preferred languages?) You could try it with a copy of your database. (DO NOT try it with the only copy of a database you want to keep!)

To substitute "tblBar" for "tblFoo" in your queries, you can run it from the VBE Immediate Window (from Access, Ctrl+g will get you there) like this:

call swapTblNamesInQueryDefs("tblFoo", "tblBar")

To actually save the changed query definitions, call it like so:

call swapTblNamesInQueryDefs("tblFoo", "tblBar", "savechanges")

The code:

Public Sub swapTblNamesInQueryDefs(ByVal pstrFind As String, _
ByVal pstrReplace As String, _
Optional ByVal pstrMode As String = "DisplayOnly")

Dim qd As QueryDef
Dim re As Object
Dim strSql As String

Set re = CreateObject("vbscript.regexp")
re.Global = True
re.IgnoreCase = True

re.Pattern = "\b" & pstrFind & "\b"

For Each qd In CurrentDb.QueryDefs
    If Left$(qd.Name, 1) <> "~" Then
        Debug.Print qd.Name
        Debug.Print "Before: " & qd.SQL
        strSql = re.Replace(qd.SQL, pstrReplace)
        Debug.Print "After: " & strSql
        'only save the modified SQL statement if called
        'with SaveChanges parameter
        'If pstrMode = "SaveChanges" Then
        If StrComp(pstrMode, "SaveChanges", vbTextCompare) = 0 Then
            qd.SQL = strSql
        End If
        Debug.Print String(20, "-")
    End If
Next qd
Set re = Nothing
Set qd = Nothing
End Sub

Edit: Changed evaluation of pstrMode to guarantee case insensitive comparison (in case module has "Option Compare Binary").

HansUp
Nice code! I can't say I need it (as I have 3 search and replace add-ins to choose from), but I can see it being useful at some point to me, and is certainly helpful for the OP.
David-W-Fenton
Thanks, David. I never used it for real. There may be gotchas I haven't anticipated. But I thought it might be a useful starting point for Ben. And based on his message, I'm unsure whether a VBA solution is acceptable to him.
HansUp
Thanks Hans, VBA is also an option available to me. I will try the code in the next day or so and report back.What search and replace add-ins do you use David?
Ben
Tried the code yesterday and it worked great. Thanks.
Ben