Results 1 to 10 of 10

Thread: [RESOLVED] MS Access query with unknown field

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Resolved [RESOLVED] MS Access query with unknown field

    Hi;

    Is it possible in MS Access to create an update query where the field being updated is determined at the time the query is run? For example...

    Code:
    update myTable set [someField] = 'ABC';
    In this example I would like to use a lookup table to set the field name (someField) used in the query.

    Is this possible?

    Thanks
    Lin

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MS Access query with unknown field

    I don't think so... and I can't think of a reason why you would want to, can you give a bit more detail about that?

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: MS Access query with unknown field

    Do you mean using a variable like:

    strSomeField = "MyColumn"

    update myTable set & " strSomeField" & " = 'ABC'";

  4. #4
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: MS Access query with unknown field

    It's possible using VBA
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: MS Access query with unknown field

    Hi Si;

    I have a table that looks something like this ...

    Code:
    ID      Category    Period_1    Period_2    Period_3   Period_4   Period_5   Period_n
    1        A               22          18         
    1        B               30          16
    1        C               12          34
    2        A               12          44
    2        B               88          20
    2        C               17          15
    As an example I would like to create a stock query that updates Period_? for ID-1, Category-A. At the moment I edit the query each time I run it which is a bit tedious.

    Hopefully my example is clear enough to show you what I'm attempting and any advice or suggestions are greatly appreciated.

    Thanks
    Lin

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: MS Access query with unknown field

    Maybe you could create a stored procedure that updates based on what you pass it...

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MS Access query with unknown field

    I had suspected it was something like that, and I'm afraid it is not a good table design - hence why this kind of thing is not easy.

    Based on the data you showed, the way I would have the table is 4 fields (ID, Category, PeriodNumber, and PeriodValue), with one row of data for each period.... that would then allow your query to specify a value (for PeriodNumber) rather than a field. If the period values are often blank (as implied by your example), it would also reduce the file size.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: MS Access query with unknown field

    Hi Si;

    Yes, I suspected that you'd suspect the table wasn't a great design. I'm still experimenting with this. The table is being used as the basis for a report that displays what activities took place in a given time period. As long as I'm willing to change the field name each time I do an update it will work but still not a great table design

    I will create a new table that matches what you've suggested and then try to figure out a query that displays the information as I've shown it in table.

    Thanks
    Lin

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MS Access query with unknown field

    The query you want would be something like this:
    Code:
    SELECT ID, 
           Category, 
           Sum(If(PeriodNumber=1,PeriodValue,0)) as Period_1,
           Sum(If(PeriodNumber=2,PeriodValue,0)) as Period_2,
           ...
    FROM myTable
    GROUP BY ID, Category

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: MS Access query with unknown field

    Thanks Si... Your suggestions worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width