Results 1 to 5 of 5
  1. #1
    QuantifyRisk is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    65

    Change Table name with Find/Replace

    I have a DB with 40 or so tables. They were linked to tblPrem and I renamed it to tblPremFinal and now the queries are all messed up. This name change needs to happen but there's too many queries to do manually. Does anyone know how to code it in VBA to do this:
    1. Open query as SQL
    2. Replace all instances of [tblPrem] with [tblPremFinal]
    3. Go to next query

    I know it can be done because I have seen it but I am not sure of the coding.

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Access should do it for you - Options>Current Database>Name AutoCorrect Options

  3. #3
    QuantifyRisk is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    65
    Yeah, I thought it would. This setting is checked but it's not doing it for some reason.

  4. #4
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Table, query and field names are not corrected with Name Auto Correct. Nor does it handle form, report or control name changes.
    Life might be easier if you just fell back to the original table name. You can always add notes/details to table properties if you need a reminder of what it's for instead of changing the name to something that does that for you.

    I guess if you search, you'll find code for such a task, but I'd need a good reason to not fall back to the original name. This is going to need you to reference the queries collection, loop through them all while (one at a time) creating a QueryDef and using the Replace function to over-write the table name, then saving the qdef. Repeat as necessary. One wrong character, missing space/comma/whatever, and you are no further ahead. You would be wise to back up the db before executing such a procedure.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try this on a copy of your dB.
    I say again: Try this on a COPY of your dB.

    Code:
    Public Sub ExportLookaheads()
        Const sTableOldName As String = "tblPrem"
        Const sTableNewName As String = "tblPremFinal"
    
        Dim qdf As QueryDef
        Dim tmpSQL As String
    
    
        'loop through Query Collection
        For Each qdf In CurrentDb.QueryDefs
            tmpSQL = vbNullString   'clear variable
            If Left(qdf.Name, 1) <> "~" Then   'ensure the query is not deleted
                tmpSQL = qdf.SQL   'get query SQL
    
                'check to see if the SQL contains sTableOldName but not sTableNewName
                If InStr(1, tmpSQL, sTableOldName) And Not InStr(1, tmpSQL, sTableNewName) Then
                    tmpSQL = Replace(tmpSQL, sTableOldName, sTableNewName)
                    qdf.SQL = tmpSQL  'save the modified SQL
                    Debug.Print qdf.Name & " -- " & tmpSQL    'print changed SQL to immediate window
                End If
    
            End If
        Next qdf
    
        Set qdf = Nothing
    
    End Sub
    I did say to try this on a copy of your dB.... didn't I??????

    Good luck

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Find and Replace
    By dweekley in forum Queries
    Replies: 3
    Last Post: 04-12-2013, 07:16 AM
  2. Replies: 1
    Last Post: 11-30-2012, 05:57 AM
  3. Replies: 10
    Last Post: 11-16-2012, 05:02 AM
  4. find and replace
    By rohini in forum Access
    Replies: 7
    Last Post: 05-17-2012, 05:23 AM
  5. Find and Replace query from a 2nd table
    By elightbox in forum Queries
    Replies: 1
    Last Post: 09-17-2010, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums