Results 1 to 9 of 9
  1. #1
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142

    Post Breaking One Query into Two, renaming, deleting, etc.. without causing problems?

    I need some advice on the appropriate technique for renaming a query without destroying existing linkages, losing joins, causing circular references or creating other problems.

    Here's the latest example.

    I had a very large query that was using 7 tables and queries as input. It was drawing fields from each, doing a bunch of calculations, and then passing all the relevant information onto other queries. I had to add another table to it. I decided it was getting too large and cumbersome. So I decided to break it into 2 queries. One would gather all the information and the other do all the calculations.

    Let's call the original large one "A".



    I made 2 copies of "A". Call them "B" and "C". B would gather information. C would use B, do calculations, and pass on results and other pertinent fields

    I worked inside each of B and C until they are were doing what I wanted. I compared the results. They were perfect.

    THis is what I wanted.

    1. The new C query to be called A (so all the queries beneath it didn't have to change their input table name or anything else)

    2. The new B query to be called something brand new as the first step in the sequence.

    3. Get rid of the original A (preferably renaming it to keep as backup...just in case).


    The problem was that if I renamed the original "A" to "Backup", all the queries beneath it that used A simply starting referring to "Backup". I couldn't bring teh new ones in.

    So what I decided to do was delete the original A, rename the new C to A, and rename B to something else.

    The end result was that the deletion essentially broke all the linkages from A to downstream and also caused some kind circular reference and joining problem that messed up my ability to work within my new calculation query. To make a long story short, I had a total disaster on my hands. Nothing was working and I didn't have a good way to change it.

    Fortunately, I had good specifications and was able to recover eventually by deleting and rebuilding the calculation query and making some changes to everything that used it downstream. (no small task)

    So this gets me to my question.

    Given what I was trying to do, what is the correct approach for changing, renaming, or deleting queries so that you don't wind up destroying linkages, losing joins, creating circular references etc... This is not the first time I wanted to change a name or break something up and it caused me major grief.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    You dont.
    once you names queries/tables/macros, etc, renaming destroys the links.

    NOW, Ive had to do what you need also. So to help in my edit....
    i wrote this to help change all my queiries if I renamed a table, without breaking my queries.

    !!!!!BACKUP YOUR DB BEFORE YOU RUN THIS!!!!!

    The code below will find and replace a word in every query, with out the need to
    find and edit every query in the database.
    You can run this in the DBUG WINDOW. ( CTL-G )
    usage:
    ReplaceInSql "findTxt", "replaceWithTxt" 'updates ALL queries in db

    IF YOU WANT TO REPLACE ONLY certain types of queries use these code letters in the command:
    "A" 'append query
    "U" 'update query
    "S" 'select
    "D" 'delete
    "M" 'make
    "N" 'UNION
    "X" 'crosstab

    usage:
    ReplaceInSql "findTxt", "replaceWithTxt", "U" 'only update queries
    ReplaceInSql "findTxt", "replaceWithTxt", "M" 'only MAKE TABLE queries

    Code:
    Public Sub ReplaceInSql(pvFind, ByVal pvReplaceWith, Optional ByVal pvQType)
    Dim db As Database
    Dim qdf As QueryDef
    Dim vFind, vQnum
    Dim sSql As String
    Const kQS = 0
    Const kQD = 32
    Const kQU = 48
    Const kQA = 64
    Const kQM = 80
    Const kQN = 128
    Const kQX = 16
    
    
    If IsMissing(pvQType) Then pvQType = ""
    Select Case pvQType
       Case "A"  'append
         vQnum = kQA
       Case "S"  'select
         vQnum = kQS
       Case "D"  'delete
         vQnum = kQD
       Case "U"  'update
         vQnum = kQU
       Case "M"  'make
         vQnum = kQM
       Case "N"  'UNION
         vQnum = kQN
       Case "X"  'crosstab
         vQnum = kQX
       Case Else
         vQnum = ""
    End Select
    
    vFind = pvFind   '"tRptDefectsBlocked"
    
    Set db = CurrentDb
    Debug.Print "--Start qry replace on:" & vFind
    
    For Each qdf In db.QueryDefs
        sSql = qdf.SQL
        If Left(qdf.Name, 1) <> "z" And Left(qdf.Name, 1) <> "~" Then
            If InStr(sSql, vFind) > 0 Then
            
               If pvQType = "" Then  'replace ALL queries
                      GoSub REPLACEIT
               Else
                    If qdf.Type = vQnum Then
                      GoSub REPLACEIT
                    End If
               End If
            End If
        End If
    Next
    Debug.Print "--End qry replace"
    Set qdf = Nothing
    Set db = Nothing
    Exit Sub
    
    REPLACEIT:
        Replace2 sSql, pvFind, pvReplaceWith
        qdf.SQL = sSql
         Debug.Print getQryTypeNam(qdf.Type); " "; qdf.Name
        qdf.Close
    Return
    End Sub

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I use Rick Fisher's Find and Replace add-in when I have to do renaming. Costs about $50. Saved my sanity more than once.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142
    Do you have a link to that add-in?

    Any issues with the install?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    No, but a quick web search should find it. I have used it since Access 2003 without issues. There are other products, even a freebie. https://www.accessforums.net/databas...ign-41421.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142
    Thanks for the insights. THis forum has been so helpful. Is there a donation area or something?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Not that I know of. You could try the Contact Us link at bottom of page and ask there.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Another option.....

    If the column names for qryC are EXACTLY the same as the column names in qryA (so you don't break links), you could open both queries in SQL view,
    copy the qryC SQL and paste the SQL into qryA. Immediately save qryA.

    No renaming necessary. You made a back up of qryA first....right??

  9. #9
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142
    Quote Originally Posted by ssanfu View Post
    Another option.....

    If the column names for qryC are EXACTLY the same as the column names in qryA (so you don't break links), you could open both queries in SQL view,
    copy the qryC SQL and paste the SQL into qryA. Immediately save qryA.

    No renaming necessary. You made a back up of qryA first....right??
    I'll keep that in mind.

    Yes, I did have a backup initially.

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

Similar Threads

  1. Referential Integrity Causing Me Problems
    By Dave D in forum Database Design
    Replies: 3
    Last Post: 06-21-2014, 05:04 PM
  2. Multi Value Field Causing Problems in Reports
    By LordPanzer in forum Reports
    Replies: 3
    Last Post: 10-15-2013, 11:46 AM
  3. Replies: 1
    Last Post: 01-26-2012, 03:58 PM
  4. Replies: 11
    Last Post: 12-14-2010, 01:25 PM
  5. Renaming Fields on Table or Query
    By jo15765 in forum Access
    Replies: 4
    Last Post: 12-09-2010, 01:47 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