Results 1 to 13 of 13
  1. #1
    nmart1230 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    43

    Code based off combo box selection to dictate another controls info.

    I have a combobox that has different record types. I would like to have code run based off of selection to change data in one of the controls on the same form. So say someone selects OMPF in the carton # control it adds a 1 in front of the number or if someone selects FOIA in the carton # control it adds a 0 in front of the number. Here is the code I was trying to work with.


    Code:
    Private Sub BOX_NUMBER_AFTERUPDATE()
    If Me.RECORD_TYPE = OMPF Then
    Me.CARTON__ = "1" + Right([BOX NUMBER], 3)
    Else
    Me.CARTON__ = "1" + Right([BOX NUMBER], 3)
    End If
    Me.TEAM_NUMBER = Left([BOX NUMBER], 1)
    End Sub
    Thanks in advance!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Put OMPF within quote marks: "OMPF"

    Use & characters as concatenation operator, not +.

    Why are you concatenating "1" in both - should one of those be "0"?
    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
    nmart1230 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    43
    Okay, I tried your suggestions and still not working. Also, about the "1" and one of them should be "0". Well, really depending on the combo selection the carton # should be what I am concatenating or nothing at all. So, if the chosen option is "OMPF" then all the carton #'s have to begin with "1" and nothing else. If the chosen option is "STR" then all the carton #'s should begin with a "0" and so forth. I just didn't know how else to write it in code. I also moved the code to RECORD_TYPE_AFTERUPDATE because the user enters the box number before they make the combobox selection. So, I figured that was a better spot for the code that way if they make an incorrect selection and then change it the CARTON # should change accordingly. I did that and it still doesn't work.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Seems the second "1" should be "0".

    What does 'not working' mean - error message, wrong results, nothing?

    Are there other options than "OMPF" and "STR"?

    I do wonder about the __ at the end of Me.CARTON__.

    If you want to provide db for analysis, follow instructions at bottom of my post. Identify the objects involved.
    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
    nmart1230 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    43
    Sorry, not working means nothing is happening. Is their are four options in the dropdown. They are "CLAIM", "STR", "OMPF", & "FOIA". Well the control is named "CARTON #" but if I under the event tab on the property sheet and choose an event to put code in that's what Access calls it automatically. I will provide db tomorrow as it is almost the end of the workday for me.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Should not use spaces and special characters/punctuation (underscore is exception) in naming convention. Better would be CartonNum or Carton_Num for field and tbxCarton for textbox, otherwise must use [].

    Try:

    Me.[CARTON #]

    Have you run Debug > Compile on the code?
    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.

  7. #7
    nmart1230 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    43
    Alright, I figured it out. The dropdown was going off the ID of the table and not the actual field name. So, the code looks like this.
    Code:
    Private Sub RECORD_TYPE_AfterUpdate()
    If Me.RECORD_TYPE = "3" Then
    Me.CARTNUM = "1" + Right([BOX NUMBER], 3)
    End If
    If Me.RECORD_TYPE = "1" Then
    Me.CARTNUM = "0" + Right([BOX NUMBER], 3)
    End If
    If Me.RECORD_TYPE = "2" Then
    Me.CARTNUM = "0" + Right([BOX NUMBER], 3)
    End If
    If Me.RECORD_TYPE = "4" Then
    Me.CARTNUM = "1" + Right([BOX NUMBER], 3)
    End If
    End Sub

  8. #8
    nmart1230 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    43
    Oh, I also changed the name to CARTNUM per your suggestion and it works beautifully. Thanks for your help!

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Ooops, my first question should have been about lookups.

    Are you using lookups with alias in table? Advise not to.

    Record_Type is a text type field?
    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.

  10. #10
    nmart1230 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    43
    Yeah, I thought about shortly after we started messaging. There is a lookup on that table. Yet, another thing I have to fix from this wonderful inherited db.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Can leave it as long as you understand what you are dealing with. But might be considerate of your successor if it is eliminated so they don't suffer as you have.
    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.

  12. #12
    nmart1230 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    43
    Oh, I will most definitely correct it just so my successor won't have to go through the headache. I have one last question on this. Is there code to have automatically switch to the next consecutive number once it reaches the highest it can go before having to switch. i.e. once it reaches 999 it know to put a 1 for 1000 then when it reaches 1999 it changes to 2000 and so on? Or will someone have to go into the code every time to change it manually when it reaches each end of the number range?
    Last edited by June7; 11-14-2014 at 10:59 AM.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    I expect code will have to be modified. What is BOX NUMBER? Is it alpha/numeric? Why are you pulling the right 3 characters?

    Should build a string identifier to always have the same number of characters. If you think the sequence will reach into more than 3 digits then should build for that using placeholder zeros. Otherwise sort won't work properly.

    In my db, I have an identifier structured like: YYYYA-XXXX

    Example: 2014A-0015

    The XXXX part starts over every year. It's never exceeded 4500. Example code in https://www.accessforums.net/program...ple-46469.html

    An alternative is to put the 1/0 prefix in one field and the sequence in another number field. Then do calc to concatenate when needed: [Prefix] & Format([Sequence], "00000")
    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.

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

Similar Threads

  1. Replies: 3
    Last Post: 07-03-2013, 10:38 AM
  2. Replies: 3
    Last Post: 12-11-2012, 09:12 AM
  3. Replies: 1
    Last Post: 10-30-2012, 10:29 AM
  4. More Info" button based on Combo Box selection
    By kriskeven in forum Access
    Replies: 1
    Last Post: 05-21-2012, 02:23 PM
  5. Replies: 6
    Last Post: 03-10-2011, 11:31 AM

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