How to assign values in combo box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • irslan rafique
    New Member
    • Oct 2014
    • 40

    How to assign values in combo box

    Hi,
    I am new to access so please bear my question.
    In a form, I have a combo box(combo1) that store values in field(remarks1) in a table.The values in combo box are:
    SL
    ST
    DI
    SL + ST
    SL + DI
    These all values are being saved in remarks1 quiet easily.
    I have more fields in the table:
    SL1
    ST1
    DT1
    SLST1
    SLDI1
    I want when I selet SL from combo1 it saves SL in remarks1 and save "1" as well in SL1 field.I want to have same result with all five combo1 values
    Please assist me.
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Combo boxes have an After Update event. In the After Update event of combo1 you can put some VBA code to update any other fields you want. You'll put something like
    Code:
    me.SL1 = 1
    if it's a numeric 1 or
    Code:
    me.SL1="1"
    if it's an alpha.
    Of course you'll have to put that in a select case or if/then mechanism to make sure you're setting the right value in SL1. Like this, for instance:
    Code:
    Select case me.combo1
       case "SL"
            me.SL1=1
       case "ST"
            me.SL1=???
       case "DI"
       case "SL+ST"
       case "SL+DI"
    end select
    I'm not exactly sure what you mean by "I want to have same result with all five combo1 values" but if you get the above I suspect you can expand on that easily.

    You might consider making combo1 a 2-column combo box. You could have the 2nd column invisible (columnwidths=1 ;0; assuming width=1 for column 1 is okay). In the second column you could have the value that matches the first column. In that case, your VBA code could be
    Code:
    me.SL1=me.combo1.column(1)
    Column(1) refers to the 2nd column, not the first.

    Hope that helps.

    Comment

    • irslan rafique
      New Member
      • Oct 2014
      • 40

      #3
      Thanks a lot for putting me on right track.
      I am using this code now:
      Code:
      Select Case Me.Combo1043
         Case "Staging"
              Me.Staging1 = "1"
         Case "Diversion"
              Me.Diversion1 = "1"
         
      End Select
      But one problem.
      If i select Staging and suppose the actual value is diversion so I will select diversion in the combo box so in result it passes "1" in both related fields (Staging1 & Diversion1)
      Need your more help please.

      Comment

      • jforbes
        Recognized Expert Top Contributor
        • Aug 2014
        • 1107

        #4
        I'm not exactly sure what you are attempting to do, but I'm guessing that you want all the fields set to nothing except the fields you are setting to one. If this is true, you can use the following:
        Code:
        Select Case Me.Combo1043
            Case "Staging"
                 Me.Staging1 = "1"
                 Me.Diversion1 = "" 
            Case "Diversion"
                 Me.Staging1 = ""
                 Me.Diversion1 = "1" 
         End Select
        If you are looking to put a "0" in that field instead, use:
        Code:
        Select Case Me.Combo1043
            Case "Staging"
                 Me.Staging1 = "1"
                 Me.Diversion1 = "0" 
            Case "Diversion"
                 Me.Staging1 = "0"
                 Me.Diversion1 = "1" 
         End Select

        Comment

        Working...