Access choose function in query field expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Coastie
    New Member
    • Apr 2007
    • 14

    #1

    Access choose function in query field expression

    Good afternoon-

    There is a table with the following fields:
    PK*, Equipment_ID, Battery_Serial
    *= arbitrary primary key

    Each piece of equipment has between 0 and 3 batteries, and each battery has a non-sequential serial number. Unfortunately, this creates multiple records for each piece of equipment. I want a query that consolidates the information onto a single line for each piece of equipment. The fields I need are:
    Equipment_ID, Battery_1_Seria l, Battery_2_Seria l, Battery_3_Seria l

    I don't care if the serial numbers are null for non-existent batteries.

    I thought I was on the right track by using the Choose() function in the expression builder, but I can't develop the proper choices variable. I tried comparing the Equipment_ID from the table to the one in the query, but that was circular logic.

    This is a medium priority item, as I don't need it until April 9th. Thank you.

    Matt
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Originally posted by Coastie
    Good afternoon-

    There is a table with the following fields:
    PK*, Equipment_ID, Battery_Serial
    *= arbitrary primary key

    Each piece of equipment has between 0 and 3 batteries, and each battery has a non-sequential serial number. Unfortunately, this creates multiple records for each piece of equipment. I want a query that consolidates the information onto a single line for each piece of equipment. The fields I need are:
    Equipment_ID, Battery_1_Seria l, Battery_2_Seria l, Battery_3_Seria l

    I don't care if the serial numbers are null for non-existent batteries.

    I thought I was on the right track by using the Choose() function in the expression builder, but I can't develop the proper choices variable. I tried comparing the Equipment_ID from the table to the one in the query, but that was circular logic.

    This is a medium priority item, as I don't need it until April 9th. Thank you.

    Matt
    There's no field that tells us whether the serial is for battery 1, 2, or 3?

    Comment

    • Coastie
      New Member
      • Apr 2007
      • 14

      #3
      Originally posted by Rabbit
      There's no field that tells us whether the serial is for battery 1, 2, or 3?
      Unfortunately no. I have no control over the structure/relationships of the database, so I am at the mercy of the architects.

      I am guessing it has something to do with the fact that the number (1,2,3) of the battery doesn't usually matter.

      Thank you for the quick response.

      Matt

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Well to get it that way I was thinking of a crosstab query but without a field to specify which battery it is, we'll have to create it. Is it safe to assume that battery serials are unique and that they won't be switched around to different equipment and that all the equipment will have at least 2 batteries?

        Comment

        • Coastie
          New Member
          • Apr 2007
          • 14

          #5
          Originally posted by Rabbit
          Well to get it that way I was thinking of a crosstab query but without a field to specify which battery it is, we'll have to create it. Is it safe to assume that battery serials are unique and that they won't be switched around to different equipment and that all the equipment will have at least 2 batteries?
          Battery serials should be unique, however, I am not sure if the developers prevented duplicate values for the battery serial. Also, we can't guarantee that all equipment will have at least 2 batteries. Some have 1, most two and few 3.

          Matt

          Comment

          • Coastie
            New Member
            • Apr 2007
            • 14

            #6
            I made a query today to which I added a subdatasheet, and it displays a list of equipment, then the subdatasheet shows the list of battery serials for each piece of equipment. How does Access make that table, and can I get to its data?

            Matt

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Originally posted by Coastie
              I made a query today to which I added a subdatasheet, and it displays a list of equipment, then the subdatasheet shows the list of battery serials for each piece of equipment. How does Access make that table, and can I get to its data?

              Matt
              As far as I can tell, the subdatasheets are just mini queries. They won't, however, create new columns for you. For that you'll need a crosstab query. But a crosstab query will need a row heading (Equipment), a value (Battery Serial), and a column heading. The column heading will be a way to distinguish between battery serial 1, 2, and 3. And it's what you're missing. I was hoping to use a sub query to accomplish that but the requirements weren't there. So I'm thinking you might have to do it through code.

              Comment

              • Coastie
                New Member
                • Apr 2007
                • 14

                #8
                I am continuing to dig on this one-

                I found a field (in another table) that documents the total number of batteries for each Equipment_ID.

                Comment

                • Coastie
                  New Member
                  • Apr 2007
                  • 14

                  #9
                  So... have to do it through code... any hints to start me in the right direction?

                  I mean, all I really want to do it transpose the column of battery serials from the subdatasheet into a row. That's it! Oh well.

                  Matt

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    What you'll want to do first is to sort your table by equipment. And create a new field. Then you'll want to do something along the lines of the following: Open the recordset and store the equipment in a variable, check it against the prior equipment, if it's the same equipment, give the new field a value of i where i is the number of times it's come across the same equipment. Then you repeat till you loop through the whole recordset.

                    Just remember to reset i each time you see a new equipment.

                    Then afterwards you can use a crosstab query.

                    Comment

                    • Coastie
                      New Member
                      • Apr 2007
                      • 14

                      #11
                      I talked with my "customer," and they don't need the data stored in the horizontal format. They just would like it displayed horizontally in the report. Does that make my life easier or harder?

                      Matt

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        You can do:
                        Code:
                        Equipment
                           - Battery Serial
                           - Battery Serial
                        
                        Equipment
                          - Battery Serial
                          - Battery Serial
                        But you won't be able to do:
                        Code:
                        Equipment - Battery Serial, Battery Serial, Battery Serial
                        Equipment - Battery Serial, Battery Serial, Battery Serial
                        Not unless you crosstab it.

                        Comment

                        • Coastie
                          New Member
                          • Apr 2007
                          • 14

                          #13
                          Originally posted by Rabbit
                          You can do:
                          Code:
                          Equipment
                             - Battery Serial
                             - Battery Serial
                          
                          Equipment
                            - Battery Serial
                            - Battery Serial
                          But you won't be able to do:
                          Code:
                          Equipment - Battery Serial, Battery Serial, Battery Serial
                          Equipment - Battery Serial, Battery Serial, Battery Serial
                          Not unless you crosstab it.
                          Fair enough. Now, though, I am severely out of my element. Is that to be done in VBA? The table in question is an Oracle table that I have linked via ODBC, and my reports all reference queries to ensure the most up to date data. How do I incorporate my creation of a battery index into this scheme?

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            Originally posted by Coastie
                            Fair enough. Now, though, I am severely out of my element. Is that to be done in VBA? The table in question is an Oracle table that I have linked via ODBC, and my reports all reference queries to ensure the most up to date data. How do I incorporate my creation of a battery index into this scheme?
                            Someone else will have to step in here. While I've used Oracle for simple queries, I've never linked it with Access so I don't know how that works. If you can interact with it the same way you can an Access table then I can help there.

                            My solution requires adding an extra column to that table. A one-time thing that you don't have to use code for.

                            Comment

                            • Coastie
                              New Member
                              • Apr 2007
                              • 14

                              #15
                              Originally posted by Rabbit
                              Someone else will have to step in here. While I've used Oracle for simple queries, I've never linked it with Access so I don't know how that works. If you can interact with it the same way you can an Access table then I can help there.

                              My solution requires adding an extra column to that table. A one-time thing that you don't have to use code for.
                              I don't have any local copies of any of the tables I am linked to. I can't modify the tables, but could create my own.

                              But, that is beside the point. I can have a local copy of the table in question, and can add the column for the battery index.

                              Comment

                              Working...