Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2011
    Posts
    48

    Unanswered: Setting A default value in a combo box

    Ok. I have tried many things and I just can't get it to work! I have a combo box that has many options and looks like the attachment.

    I want it to use one of the selections as a default value. I am able to do it just on the table but I would rather have it done on the form, and I can't figure out the code at all!
    Attached Thumbnails Attached Thumbnails combobox.JPG  

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Where ComboBoxName is the actual name of your cbo:

    To Default to the First selection in the cbo you'd use

    Me.ComboBoxName = Me.ComboBoxName.ItemData(0)

    Notice that it's Zero-based, so the 1st selection is 0, the 2nd selection is 1, etc.

    To Default to a given Bound Value

    Me.ComboBoxName = "DesiredDefaultValue"

    replacing DesiredDefaultValue with the value you actually want.Where you place the code depends on when you want it to Default to a given selection.

    When the Form first opens, use it in Form_Load event
    Code:
    Private Sub Form_Load()
      Me.ComboBoxName = "DesiredDefaultValue"
    End Sub
    To Default it whenever moving to another Record, use the Form_Current event.
    Code:
    Private Sub Form_Current()
      Me.ComboBoxName = "DesiredDefaultValue"
    End Sub
    To use it only when entering a New Record, in the Form_Current event
    Code:
    Private Sub Form_Current()
       If Me.NewRecord Then
         Me.ComboBoxName = "DesiredDefaultValue"
       End If
      End Sub
    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    what I've done in the past is to declare a default value in the table and then force that value as the default by code

    eg:-
    table
    ID
    Description
    IsDefault

    the row source for the list / combo box is
    SELECT ID, Description FROM mytable
    WHERE... if required
    ORDER BY IsDefault, Description

    you may need to play with the ORDER BY clause if the default value isn't comong out on top
    ORDER BY IsDefault DESC, Description

    that populated the list / combo box

    then when a new record is addedset the current value of the list box the the first vlaue in the list

    another wrinkle is to retrieve the default value from the table and use that if the list / combo box is not set ie is NULL
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Sep 2011
    Posts
    48
    thanks guys!! using this I was able to figure it out!

  5. #5
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Glad we could help!

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

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