Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188

    Need help with cascading

    Hello,
    I have a lookup table with two fields (Location and Code). Every location has a corresponding code. In my form, Location is a combo box and when I select a location I would like the Code field to display the corresponding code from the table. How do I write the syntax for that?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    How is this a cascading issue? Can a Code have multiple locations?

    What exactly are you saving into record? Is that field also named Code? I would advise not using exact same field name in multiple tables.

    Possibly all you need is an expression in textbox that references combobox column that contains Code: =cbxLocation.Column(1)

    If you want to provide db for analysis, follow instructions at bottom of my post.
    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.

  3. #3
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Ok, that was way easier! LOL

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    An alternative is to include lookup table in form RecordSource, bind textbox to Code field and set its properties Locked Yes and TabStop No.
    But if there is no need to complicate RecordSource, don't.
    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.

  5. #5
    Join Date
    Apr 2017
    Posts
    1,792
    An alternative is to have combo's RowSource as query like (I assume you will have location code as ControlSource of combo):
    Code:
    SELECT LocationCode, Location & ": " & LocationCode FROM YourLookupTable ORDER BY 2 // in case LocationCode is a string
    or
    SELECT LocationCode, Location & ": " & CStr(LocationCode) FROM YourLookupTable ORDER BY 2 // in case LocationCode is an integer
    Your combo's value will be LocationCode, but the second column is what is displayed and contains both location and it's code

  6. #6
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Ok, I went with the easy route. I put =cbxLocation.Column(1) as the control source for my Code field. It's very close to working the way I need, but my Location field has duplicates. In other words, each location could have multiple codes. When I switch between these duplicate Locations the Code doesn't change. It always defaults to the first Code in the list. I hope that makes sense.

    How do I fix that?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    It should pull Code from whichever item is selected in combobox. Which column is Code in? Column(x) index begins with 0. If Code is in column 2 its index is 1.
    Maybe Avril's suggestion in post 5 would be better.

    Again, you could provide db for analysis.
    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
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by June7 View Post
    It should pull Code from whichever item is selected in combobox. Which column is Code in? Column(x) index begins with 0. If Code is in column 2 its index is 1.
    Maybe Avril's suggestion in post 5 would be better.

    Again, you could provide db for analysis.
    The code is in column 2 so the index is correct. The codes populate correctly. It just doesn't change when I toggle between the duplicate Locations in the list.

    For example, when I select "San Antonio" in the Location combo box, the Code fields updates to SA456. Perfect. However, if I switch the combo box to the second "San Antonio" row, the Code field should update to SA457 but it doesn't. It stays as SA456.

    Location Code
    San Antonio SA456
    San Antonio SA457
    San Antonio SA458

  9. #9
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,818
    Upload a copy of the Db so we can see

  10. #10
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by mike60smart View Post
    Upload a copy of the Db so we can see
    I really wish I could but there's too much proprietary data. :-(

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Cannot see that happening TBH if the codes actually are different.
    You could try setting it in the after update event of the combo?
    We do not need to see everything, just enough for the issue?

    Surely location and codes are not proprietary? :-(

    You can also use this on a COPY of your data.

    Here is a quick knockup with your data. That works as it should?
    Attached Files Attached Files
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Join Date
    Apr 2017
    Posts
    1,792
    Quote Originally Posted by matey56 View Post
    The code is in column 2 so the index is correct. The codes populate correctly. It just doesn't change when I toggle between the duplicate Locations in the list.

    For example, when I select "San Antonio" in the Location combo box, the Code fields updates to SA456. Perfect. However, if I switch the combo box to the second "San Antonio" row, the Code field should update to SA457 but it doesn't. It stays as SA456.

    Location Code
    San Antonio SA456
    San Antonio SA457
    San Antonio SA458
    When you do like I adviced, the combo displays selections
    ...
    "San Antonio: SA456"
    "San Antonio: SA457"
    "San Antonio: SA458"
    ...

    When you start typing into combo "San Antonio...", then at some moment (e.g. when you have typed something like "San Ant"), those selections will be at top of active list of them, and you can easily select the right one.

    But have location codes (e.g. SA456, or SA457, or SA458) as primary key, or indexed as an unique value, in your lookup table, and as foreign keys in any other tables you want it linked to - otherwise you will be in a world of hurt! Locations (e.g. San Antonio") must be present only in lookup table!

  13. #13
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by Welshgasman View Post
    Cannot see that happening TBH if the codes actually are different.
    You could try setting it in the after update event of the combo?
    We do not need to see everything, just enough for the issue?

    Surely location and codes are not proprietary? :-(

    You can also use this on a COPY of your data.

    Here is a quick knockup with your data. That works as it should?

    I think I'm getting close. Now, when I change the location it changes it for all records in the DB. How do I make it so it's only changing it for the current record?

  14. #14
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,818
    So your Combobox is not bound to a Control Source?

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Quote Originally Posted by matey56 View Post
    I think I'm getting close. Now, when I change the location it changes it for all records in the DB. How do I make it so it's only changing it for the current record?
    We have NO IDEA as to what you are doing!
    It sounds like a continuous forms issue, but that is just a wild guess.
    Where are you even updating the DB with that expression?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need Help With Cascading Combo Boxes
    By emassey0411 in forum Access
    Replies: 14
    Last Post: 05-25-2022, 06:06 PM
  2. Replies: 5
    Last Post: 12-20-2021, 07:50 PM
  3. Replies: 1
    Last Post: 02-27-2014, 03:43 PM
  4. Replies: 6
    Last Post: 02-01-2013, 10:02 AM
  5. Replies: 3
    Last Post: 09-28-2012, 03:05 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