Link to home
Create AccountLog in
Avatar of panterall
panterallFlag for United States of America

asked on

How to save a query's changes in MS Access 2007?

Hello,

I have been given a reporting tool built in MS Access by somebody else. This reporting tool was not performing as supposed to. Long story short, I found the issue in a query by looking at the SQL code in TOAD, and that actually would allow the Access reporting tool to perform as supposed to.

However, my big problem is that I cannot apply those changes to the Access SQL code.
So, this is what I am doing:

1) I press and hold "Shift" when I open the Access DB, so I can access all the objects in this reporting tool.

2) I locate the query that needs to be changed and double click on it.

3)After I see the results of the query, I press on "SQL View" to see the SQL code of that query.

4) I make changes and hit "Save".

5) I run again the query and I get the results I was hoping for. However, when I close the database and open it up again, my changes were not actually saved.

What can I do to save changes in the SQL View (mode)?
Help please, I have spent the last four days trying to figure this out and I just do not what to do.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of panterall

ASKER

Yes, I did. The thing is that I also encounter the same issue working on other databases through Access 2007. I even deleted the query and created a new one, but nothing. Any ideas?

Thanks
Please post the SQL of the query, before and after the changes.

There are some syntactical changes that the Jet SQL engine (MS Access SQL) simply cannot make.

In other cases Access will change your syntax back to what it "Thinks" you want.
The code is really really long, but the only part that I changed (in three instances) is this:

Previously the code was written this way:
 WHERE GYVMINF_ADDR_TYPE  =  'MA'

Open in new window


I changed it  to this (in order to include the second address type by using an OR)
 WHERE GYVMINF_ADDR_TYPE  IN ('MA','SE')

Open in new window


Again, when I run the code in TOAD it works perfectly, but I cannot save the changes in the Access reporting tool. Thanks for looking into that.
There is something else at work.  That change should persist without any trouble.

 Can you post a sample of your database with any sensitive data masked or removed?
Here is the piece of code, only for this query including the changes already made:

 WITH    
    MBR    
    AS  (  SELECT DISTINCT CASE     
                    WHEN SPOUSE_PIDM IS NULL    
                    THEN PRIMARY_PIDM    
                    WHEN (PM.MBR_PIDM IS NOT NULL AND    
                          SM.MBR_PIDM IS NULL)     
                    THEN PRIMARY_PIDM    
                    WHEN (PM.MBR_PIDM IS NULL AND    
                          SM.MBR_PIDM IS NOT NULL)      
                    THEN SPOUSE_PIDM    
                    WHEN (PM.MBR_PIDM IS NOT NULL AND    
                          SM.MBR_PIDM IS NOT NULL )     
                    THEN PRIMARY_PIDM    
                    WHEN (PM.MBR_PIDM IS NULL AND    
                          SM.MBR_PIDM IS NULL )      
                    THEN PRIMARY_PIDM    
                  END                               AS MBR_PIDM    
               , COUPLE_NAME   
               , NVL(PM.CLUB,SM.CLUB)               AS CLUB_CODE  
               , NVL(PM.CLUB_DESCR,SM.CLUB_DESCR)   AS CLUB_DESC   
               , NVL(PM.PC_Mbr,SM.PC_Mbr)           AS PC_MEMBER   
            FROM (SELECT gyvminf_pidm                                   AS PIDM   
                        , gyvminf_cm_mail_name                          as COUPLE_NAME   
                        ,CASE   
                            WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'P'  
                            THEN gyvcsps_pidm   
                            WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'S'  
                            THEN gyvcsps_spouse_pidm   
                            WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'NULL'  
                            THEN gyvminf_pidm END                       AS PRIMARY_PIDM   
                        ,CASE   
                            WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'P'  
                            THEN gyvcsps_spouse_pidm   
                            WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'S'  
                            THEN gyvcsps_pidm  END                      AS SPOUSE_PIDM   
                    FROM gyvminf   
                        LEFT JOIN gyvcsps ON gyvcsps_pidm = gyvminf_pidm   
                    WHERE gyvminf_addr_type IN ('MA','SE') 
                 )    
                LEFT JOIN (   select APRACTY_PIDM      as MBR_PIDM    
                                   , APRACTY_ACTC_CODE AS CLUB    
                                   , STVACTC_DESC      AS CLUB_DESCR    
                                   , MAX(APRACYR_YEAR) AS PC_Mbr    
                                FROM AYVXACT    
                               WHERE APRACTY_ACTC_CODE  IN ('FA_AZ_02') 

                                 AND APRACYR_YEAR >= 2013
                            GROUP BY APRACTY_PIDM     
                                   , APRACTY_ACTC_CODE    
                                   , STVACTC_DESC     
                        ) PM ON PRIMARY_PIDM = PM.MBR_PIDM    
                LEFT JOIN (   select APRACTY_PIDM      as MBR_PIDM    
                                   , APRACTY_ACTC_CODE AS CLUB    
                                   , STVACTC_DESC      AS CLUB_DESCR    
                                   , MAX(APRACYR_YEAR) AS PC_Mbr    
                                FROM AYVXACT    
                               WHERE APRACTY_ACTC_CODE  IN ('FA_AZ_02') 

                                 AND APRACYR_YEAR >= 2013
                            GROUP BY APRACTY_PIDM     
                                   , APRACTY_ACTC_CODE    
                                   , STVACTC_DESC     
                        ) SM ON SPOUSE_PIDM = SM.MBR_PIDM    
         ),    
    CHURCH    
    AS  (  SELECT DISTINCT ENTITY_UID                        AS CHRCH_PIDM    
             FROM AA_DONOR_CATEGORY    
                    LEFT JOIN (  SELECT AYVXMIE_PIDM   AS EXCL_PIDM     
                                      , MIN(CASE WHEN ayvxmie_incl_code = 'PCO' THEN ' '     
                                                 ELSE CASE WHEN ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')      
                                                           THEN 'Excl' END END)  AS EXCL     
                                   FROM AYVXMIE      
                                  WHERE ayvxmie_incl_code = 'PCO'     
                                     OR ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')     
                               GROUP BY AYVXMIE_PIDM     
                              ) EX ON ENTITY_UID = EX.EXCL_PIDM     
            WHERE DONOR_CATEGORY = 'CRCH'   
              AND EXCL IS NULL   
         ),    
    MAILABLE    
        AS (  SELECT CASE WHEN SPOUSE_PIDM IS NULL and  
                               PIDM = PRIMARY_PIDM AND  
                               NVL(PX_EXCL,'OK') = 'OK'  
                          THEN PRIMARY_PIDM  
                          WHEN SPOUSE_PIDM IS NOT NULL and  
                               PIDM = PRIMARY_PIDM AND  
                               (NVL(PX_EXCL,'OK') = 'OK' AND 
                                NVL(SX_EXCL,'OK') = 'OK') 
                          THEN PRIMARY_PIDM  
                          WHEN SPOUSE_PIDM IS NOT NULL and  
                               PIDM = PRIMARY_PIDM AND  
                               (NVL(PX_EXCL,'OK') = 'OK' AND 
                                NVL(SX_EXCL,'OK') <> 'OK') 
                          THEN PRIMARY_PIDM  
                          WHEN SPOUSE_PIDM IS NOT NULL and  
                               PIDM = SPOUSE_PIDM AND  
                               PX_EXCL = 'OK' AND 
                               SX_EXCL = 'OK' AND 
                               SP_ADDR IS NULL 
                          THEN SPOUSE_PIDM  
                          WHEN SPOUSE_PIDM IS NOT NULL and  
                               PIDM = PRIMARY_PIDM AND  
                               (NVL(PX_EXCL,'OK') <> 'OK' AND 
                                NVL(SX_EXCL,'OK') = 'OK') 
                          THEN SPOUSE_PIDM  
                          WHEN SPOUSE_PIDM IS NOT NULL AND  
                               PIDM = SPOUSE_PIDM AND  
                               (NVL(PX_EXCL,'OK') <> 'OK' AND 
                                NVL(SX_EXCL,'OK') = 'OK')  
                          THEN SPOUSE_PIDM END                     AS MAIL_PIDM 
                   , CASE WHEN SP_ADDR IS NOT NULL AND  
                               NVL(SX_EXCL,'OK') = 'OK'  
                          THEN 'Y' ELSE NULL END                    AS SP_MAILABLE   
                FROM ( SELECT gyvminf_pidm           AS pidm   
                            , GYVMINF_SP_ADDR_TYPE   AS SP_ADDR   
                             ,CASE   
                                WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'P' THEN gyvcsps_pidm   
                                WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'S' THEN gyvcsps_spouse_pidm   
                                WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'NULL' THEN gyvminf_pidm   
                              END   
                                AS primary_pidm   
                             ,CASE   
                                WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'P' THEN gyvcsps_spouse_pidm   
                                WHEN NVL (gyvcsps_primary_ind, 'NULL') = 'S' THEN gyvcsps_pidm   
                             END   
                                AS spouse_pidm   
                         FROM gyvminf    
                            LEFT JOIN gyvcsps ON gyvcsps_pidm = gyvminf_pidm   
                        where gyvminf_addr_type  IN ('MA','SE')                       
                    )    
                    LEFT JOIN (  SELECT AYVXMIE_PIDM   AS PX_EXCL_PIDM    
                                      , MAX(CASE WHEN ayvxmie_incl_code = 'PCO' THEN 'OK'    
                                                 ELSE CASE WHEN ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')     
                                                           THEN 'Excl' END END)  AS PX_EXCL   
                                   FROM AYVXMIE     
                                  WHERE ayvxmie_incl_code = 'PCO'    
                                     OR ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')   
                               GROUP BY AYVXMIE_PIDM    
                              ) PX ON PRIMARY_PIDM = PX_EXCL_PIDM    
                    LEFT JOIN (  SELECT AYVXMIE_PIDM   AS SX_EXCL_PIDM    
                                      , MAX(CASE WHEN ayvxmie_incl_code = 'PCO' THEN 'OK'    
                                                 ELSE CASE WHEN ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')     
                                                           THEN 'Excl' END END)  AS SX_EXCL    
                                   FROM AYVXMIE     
                                  WHERE ayvxmie_incl_code = 'PCO'    
                                     OR ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')    
                               GROUP BY AYVXMIE_PIDM    
                              ) SX ON SPOUSE_PIDM = SX_EXCL_PIDM 
                    JOIN GYVGEOR ON GYVGEOR_PIDM = PIDM  
                                AND GYVGEOR_GEOR_CODE  IN ('FA_AZ_02') 

               WHERE (PIDM = PRIMARY_PIDM AND NVL(PX_EXCL,'OK') = 'OK')  
                  OR (PIDM = SPOUSE_PIDM AND NVL(SX_EXCL,'OK') = 'OK')   
         )   
  SELECT DISTINCT GYVMINF_PIDM                                                                     AS PIDM   
       , GYVNAME_ID                                                                       AS ID   
       , SUBSTR(GYVMINF_PIDM,2,3) ||    
         SUBSTR(GYVNAME_LAST_NAME,1,3) ||    
         SUBSTR(GYVMINF_PIDM,-3)                                                          AS Mail_ID   
       , GYVGEOR_GEOR_CODE                                                                AS Club   
       , GYVGEOR_GEOR_DESC                                                                AS Club_Desc   
       , GYVNAME_LAST_NAME                                                                AS Last_Name   
       , CASE WHEN GYVNAME_CM_NAME IS NOT NULL AND    
                   SP_MAILABLE = 'Y'    
              THEN GYVNAME_CM_NAME ELSE GYVNAME_NAME_FULL END                             AS Mail_Name   
       , GYVHADR_ADD1                                                                     AS Street_1   
       , GYVHADR_ADD2                                                                     AS Street_2   
       , GYVHADR_ADD3                                                                     AS Street_3   
       , GYVHADR_CITY                                                                     AS City   
       , GYVHADR_STAT                                                                     AS State   
       , GYVHADR_ZIP                                                                      AS ZIP   
       , GYVHADR_NATN                                                                     AS Nation   
       , GYVHADR_DELPT                                                                    AS DelPt   
       , GYVHADR_CORR_DIG                                                                 AS Corr_Dig   
       , GYVHADR_CARR_RT                                                                  AS Carr_Rt    
       , CASE WHEN GYVMINF_PREF_US_SCHOOL = 'US'   
              THEN GYVMINF_PREF_US_YEAR END                                               AS US_ALUM    
       , CASE WHEN GYVMINF_PREF_CO_SCHOOL = 'JC'   
              THEN GYVMINF_PREF_CO_YEAR || ' (JC)'     
              WHEN GYVMINF_PREF_CO_SCHOOL = 'CO'    
              THEN GYVMINF_PREF_CO_YEAR END                                               AS CO_ALUM    
       , CASE WHEN GYVMINF_CUR_PAR_IND = 'C'   
              THEN 'Y' END                                                                AS CURR_PAR    
       , PC_Member   
    FROM GYVMINF    
         JOIN GYVGEOR ON GYVMINF_PIDM = GYVGEOR_PIDM    
         JOIN GYVHADR ON GYVGEOR_PIDM = GYVHADR_PIDM    
                     AND GYVGEOR_ATYP_CODE = GYVHADR_ATYP    
         JOIN GYVNAME ON GYVMINF_PIDM = GYVNAME_PIDM    
         JOIN MAILABLE ON GYVMINF_PIDM = MAIL_PIDM    
         LEFT JOIN MBR ON GYVMINF_PIDM = MBR_PIDM    
         LEFT JOIN CHURCH ON GYVMINF_PIDM = CHRCH_PIDM    
   WHERE MAIL_PIDM IS NOT NULL   
     AND GYVGEOR_GEOR_CODE  IN ('FA_AZ_02') 

     AND NVL(GYVMINF_HS_GRAD_YR,'1950') <= '2008' 
     AND NVL(GYVHADR_NATN,'USA') LIKE 'USA'      AND (GYVNAME_FIRST_NAME IS NOT NULL OR CHRCH_PIDM IS NOT NULL) 
UNION ALL    
  SELECT DISTINCT MBR_PIDM   
       , GYVNAME_ID                                                                       AS ID   
       , SUBSTR(GYVMINF_PIDM,2,3) ||    
         SUBSTR(GYVNAME_LAST_NAME,1,3) ||    
         SUBSTR(GYVMINF_PIDM,-3)                                                          AS Mail_ID   
       , NVL(GYVGEOR_GEOR_CODE,'N/A')                                                     AS Club   
       , NVL(GYVGEOR_GEOR_DESC,'(out-of-region member)')                                  AS Club_Desc   
       , GYVNAME_LAST_NAME                                                                AS Last_Name   
       , CASE WHEN SX_EXCL_PIDM IS NULL AND   
                   GYVMINF_CM_MAIL_NAME IS NOT NULL   
              THEN GYVMINF_CM_MAIL_NAME   
              ELSE GYVNAME_ADDR_NAME_PFLS END                                             AS Mail_Name   
       , GYVHADR_ADD1                                                                     AS Street_1   
       , GYVHADR_ADD2                                                                     AS Street_2   
       , GYVHADR_ADD3                                                                     AS Street_3   
       , GYVHADR_CITY                                                                     AS City   
       , GYVHADR_STAT                                                                     AS State   
       , GYVHADR_ZIP                                                                      AS ZIP   
       , GYVHADR_NATN                                                                     AS Nation   
       , GYVHADR_DELPT                                                                    AS DelPt   
       , GYVHADR_CORR_DIG                                                                 AS Corr_Dig   
       , GYVHADR_CARR_RT                                                                  AS Carr_Rt    
       , CASE WHEN GYVMINF_PREF_US_SCHOOL = 'US'   
              THEN GYVMINF_PREF_US_YEAR END                                               AS US_ALUM    
       , CASE WHEN GYVMINF_PREF_CO_SCHOOL = 'JC'   
              THEN GYVMINF_PREF_CO_YEAR || ' (JC)'     
              WHEN GYVMINF_PREF_CO_SCHOOL = 'CO'    
              THEN GYVMINF_PREF_CO_YEAR END                                               AS CO_ALUM    
       , CASE WHEN GYVMINF_CUR_PAR_IND = 'C'   
              THEN 'Y' END                                                                AS CURR_PAR    
       , PC_MEMBER   
    FROM GYVMINF    
         JOIN MBR ON GYVMINF_PIDM = MBR_PIDM    
         JOIN GYVHADR ON MBR_PIDM = GYVHADR_PIDM    
         JOIN GYVNAME ON MBR_PIDM = GYVNAME_PIDM    
         LEFT JOIN (  SELECT AYVXMIE_PIDM   AS PX_EXCL_PIDM    
                           , MAX(CASE WHEN ayvxmie_incl_code = 'PCO' THEN 'OK'    
                                      ELSE CASE WHEN ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')     
                                                THEN 'Excl' END END)  AS PX_EXCL    
                        FROM AYVXMIE     
                       WHERE ayvxmie_incl_code = 'PCO'    
                          OR ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')   
                    GROUP BY AYVXMIE_PIDM    
                   ) PX ON MBR_PIDM = PX_EXCL_PIDM    
         LEFT JOIN (  SELECT AYVXMIE_PIDM   AS SX_EXCL_PIDM    
                           , MAX(CASE WHEN ayvxmie_incl_code = 'PCO' THEN 'OK'    
                                      ELSE CASE WHEN ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')     
                                                THEN 'Excl' END END)  AS SX_EXCL    
                        FROM AYVXMIE     
                       WHERE ayvxmie_incl_code = 'PCO'    
                          OR ayvxmie_excl_code IN ('NML','NDO','NPC','PPC')    
                    GROUP BY AYVXMIE_PIDM    
                   ) SX ON GYVMINF_SP_PIDM = SX_EXCL_PIDM    
         LEFT JOIN GYVGEOR ON MBR_PIDM = GYVGEOR_PIDM     
                          AND CLUB_CODE = GYVGEOR_GEOR_CODE    
         LEFT JOIN CHURCH ON MBR_PIDM = CHRCH_PIDM    
   WHERE GYVMINF_ADDR_TYPE  IN ('MA','SE') 
--     AND GYVMINF_DRQ_IND IS NULL   
     AND MBR.PC_MEMBER >= 2013
     AND ((MBR_PIDM = GYVMINF_PIDM AND PX_EXCL IS NULL) 
        OR (MBR_PIDM = GYVMINF_SP_PIDM AND SX_EXCL IS NULL))  
     AND NVL(GYVMINF_HS_GRAD_YR,'1950') <= '2008' 
     AND NVL(GYVHADR_NATN,'USA') LIKE 'USA'      AND GYVGEOR_PIDM IS NULL 
     AND CLUB_CODE  IN ('FA_AZ_02') 

     AND (GYVNAME_FIRST_NAME IS NOT NULL OR CHRCH_PIDM IS NOT NULL) 
     AND GYVGEOR_PIDM IS NULL  
  ORDER BY LAST_NAME

Open in new window

Just for laughs, what happens if you do this, then save the query:

WHERE GYVMINF_ADDR_TYPE  =  'MA' OR WHERE GYVMINF_ADDR_TYPE  =  'SE'
That is a very good question and I have tried it before, but it does not return the expected results.
What it does is that gives me all people and institutions with addresses in the database (all of them).

The difference with the query I am trying to update in Access is that gives me only the people (and not institutions) that are part of a region in the States.

I think with the statement laid out like that evaluates all of the conditions.

Does that make sense?
Good question!
The WHERE conditions you have posted in http:#a39029888 look fine for an Access database.

However, the lengthy query that you have posted in http:#a39029976 looks SQL compatible, but not Access compatible -- particularly those SELECT CASE clauses,

Can you describe your database a bit more?

Are you saying that this has been working entirely in Access (front-end and back-end)?

Do you have the Access option for SQL compatibility checked?

Again, seeing an actual sample database might help.
OK, then I will leave you in mbizup's capable hands.

Jeff
Thanks Jeff.
I'm not feeling so capable at the moment.  :-)

I think this question could benefit from being in additional topic areas, but I'm not sure which one(s).
Thanks for your answer mbizup.
Well this database does a lot of things, but this particular option does the following:
- The user selects the query to run from a dropdown menu (the one from the code ).
- Then, the user selects a region(s). This regions contain active members (people) signed up to receive mailing from us.

The problem with the original code was that it was only pulling people with main addresses in  a specific region. What I did was to include a code for those who also have a seasonal address in that region.

How do I know if I have the Access option for SQL activated? As you can see I am not very familiar with MS Access.

I am attaching a screenshot of how the reporting tool looks like
03-28-2013-4-04-59-PM.png
<How do I know if I have the Access option for SQL activated?>
This is under Access Option in Access 2010 (not sure about 2007), but it probably doesn't matter... if I'm understanding your question correctly, the bulk of your query IS working without errors, but you just need to change the SQL of the query, and are unable to because the changes keep reverting themselves (let me know if I'm misunderstanding that).

Where is your query actually stored?

Is it actually in your Access database, or is it somehow linked?  Can you post a screenshot of your navigation pane with the query's name visible, and also a screenshot of the query's design/SQL and maybe something to show how/where you are opening it for edits?

<<
I found the issue in a query by looking at the SQL code in TOAD
>>
I haven't actually tried editing and saving your query in Access because you haven't posted the full SQL, and because I don't have the tables you are using.  However, my gut feeling is that Access would not allow me to save the syntax you have posted.

TOAD is a separate reporting tool correct?  Are you able to actually edit and save the SQL using TOAD?

If you can do that, perhaps the query can be imported/replaced in Access (?)

(Just making somewhat educated guesses here... this is unfamiliar territory for me)
Thanks, mbizup for your post and all the educated guesses. Brainstorming opens up new paths.

Yes, TOAD is a tool some of us use for Data Analysis. It is pretty good. At least I feel is more powerful than Access and lets me deal with the code right away. Besides, it does not contain all the thousands of visual confusing things (at least for me) that Access have.

I tried exporting it from TOAD to Access, but no luck.
Everything is stored in a huge Oracle DB. Access pretty much only serves as a corky reporting tool.
Well, well, well!!!

I was finally able to fix the issue. Hooray!

As mbizup said in the very first post, the issue was related to a VBA Module very hidden by the later Reporting Tool designer. Funny thing is that I had already looked over all the modules present, but for some reason, the former designer placed this module in a different place. Somehow this also replicated a different code result when the reporting tool was run.

Thanks a lot mbizup for sticking around and for the educational tips, and boag2000 for your contribution as well.
Thanks mbizup! you were very right since the very beginning. I guess I just needed to look deeper!
Lol!  Glad to help out.

The first thing I LOVED about learning database development was that it had so many different aspects to it... programming, SQL, UI...

They all work together to trip you up in unexpected ways!
LOL! that is so true.
I guess I should stick to the plain SQL code hahaha!!