How to set a default value on a combo box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • msilva100
    New Member
    • Aug 2015
    • 25

    #1

    How to set a default value on a combo box

    I have a table called STATES.

    This table is displayed in a form as a combo box for the selection of a state (AL,AR,AZ....WV ,WY).

    I want the combo box to default to MA unless I need to select another state.

    Thank you, I greatly appreciate your help.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Open the form in design view
    Highlight the control
    Right click
    Properties
    [Data] tab
    [Default Value] = "MA"
    (with string values, you need to include the quotes as shown :) )
    Last edited by zmbd; Dec 31 '15, 03:29 AM. Reason: [z{clarified the needed quotes for string values - opps}]

    Comment

    • msilva100
      New Member
      • Aug 2015
      • 25

      #3
      I tried your suggestion but I still can not get the combo box
      to default a Value.

      No sure why??

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Go to the same data tab
        In the control source what do you have?
        In the row source what do you have?
        In the row source type what do you have?

        In the Event tab
        are there any entries for any of the events?

        Go to the format tab
        What do you have for column count?
        What do you have for column widths?
        What do you have for list width?

        In the property Sheet
        Drop down list
        Select "Form"
        Event tab
        Check the code behind any events that you currently have associated with this form, especially:
        On_Current
        On_Load
        On_Open

        You are looking for any command that deals with your combobox.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          Originally posted by MSilva
          MSilva:
          No sure why??
          I suspect it's because you've omitted the double-quotes ("). These are not part of the display of the answer as you possibly assumed. They are a fundamental requirement for that property as it's a string value property. You could also use ='MA', but not 'MA'. Only the double-quotes work as a natural and readable value in this property.

          Generally the simplest is to use "MA"

          Good luck :-)

          Comment

          • mbizup
            New Member
            • Jun 2015
            • 80

            #6
            Are you testing this on new or previously entered records?

            The Default Value property will only affect new records, not change blanks that you may have already entered in existing records.

            Comment

            • hvsummer
              New Member
              • Aug 2015
              • 215

              #7
              This table is displayed in a form as a combo box for the selection of a state (AL,AR,AZ....WV ,WY).

              I want the combo box to default to MA unless I need to select another state.
              you can use vba to set default value for controls
              Code:
              .DefaultValue = Chr(34) & "MA" & Chr(34)
              'Chr(34) is double quote
              and can trap condition to make sure if controls.value = "" then controls.value = "MA" like this
              Code:
              if me.dirty then 
                if controls.value = "" or isMissing(Controls.value) then controls.value = "MA"
                me.dirty = false
              end if

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                vba maybe overkill here
                Let's see OP's answer to NeoPa, mbizup, and my posts to see what it is that OP actually requires.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  Originally posted by hvsummer
                  hvsummer:
                  you can use vba to set default value for controls
                  While this is true in itself, the code provided would not work as required. See my post #5 where the actual string value required is explained. Your code would leave the value as MA and not "MA" as would be necessary for it to work as required.

                  I believe this is another occasion where we need to give careful consideration to our advice. Certainly it's possible to use VBA for this, but the indications are that it's not the most sensible approach. My guess is that the OP's fallen into the most common trap when working with this property, of adding the value without the quotes. Let's see if that's how simple it is before trying to solve a more complicated puzzle that probably doesn't exist.

                  All attempts to help are appreciated, of course. With experience though, we need to consider more carefully what we offer.

                  Comment

                  • zmbd
                    Recognized Expert Moderator Expert
                    • Mar 2012
                    • 5501

                    #10
                    and it is certainly my fault for not being clearer by saying "including quotes" - my bad... I'll go fix that now

                    :blush:

                    Comment

                    • hvsummer
                      New Member
                      • Aug 2015
                      • 215

                      #11
                      me too, I'm going to edit my comment :3

                      and btw, NeoPa make me confusing about .value property = text string like "MA" in vba is valid,

                      because access know controls.value = "MA" equal value MA text data type, if we add "MA" mean value actually "MA" not MA, then
                      This table is displayed in a form as a combo box for the selection of a state (AL,AR,AZ....WV ,WY).
                      state will become ("AL", "AR", "AZ",...., "AM", ..., "WV", "WY") which does not fullfill his demand ?

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32669

                        #12
                        Originally posted by hvsummer
                        hvsummer:
                        and btw, NeoPa make me confusing about .value property = text string like "MA" in vba is valid,
                        Let me see if I can help you to understand what I'm trying to say. I include two images to illustrate the two different values. I'll refer to the first as Pic A & the second as Pic B for ease of use.
                        [IMGNOTHUMB]https://bytes.com/attachment.php? attachmentid=85 90&d=145157523 8[/IMGNOTHUMB][IMGNOTHUMB]https://bytes.com/attachment.php? attachmentid=85 91&d=145157523 8[/IMGNOTHUMB]
                        Pic A shows an attempt to add the value without the double-quotes and Pic B shows the same value with the double-quotes. Using the interface, Pic A is automatically updated to the value in Pic B.

                        In code, these two attempts are similar to :
                        Pic A
                        Code:
                        X.DefaultValue = "MA"
                        Pic B
                        Code:
                        X.DefaultValue = """MA"""
                        Unfortunately, with code, the value is added exactly as specified. So, the first line, similar to what was suggested in post #7 (Originally, prior to the update.), would not be valid and would leave a problem in the form.

                        You'll see that, what is valid in the user interface "MA", is not valid when used in VBA. In VBA the outside quotes of a string are a part of the language syntax and not part of the string value itself. This is why the second line of code (above) is required when setting the .DefaultValue property.

                        I hope that makes everything a lot clearer.

                        PS.
                        Originally posted by hvsummer
                        hvsummer:
                        state will become ("AL", "AR", "AZ",...., "AM", ..., "WV", "WY") which does not fullfill his demand ?
                        Actually, though the OP specified it as (AL,AR,AZ....WV ,WY), what they really meant was ('AL','AR','AZ' ...'WV','WY'). In Access term this is similar to ("AL","AR","AZ" ..."WV","WY") as they like to simplify things for beginners. This only works up to a point, beyond which it actually makes life more complicated, but by then you're hooked and people like us help you pick up the pieces. The point here is that the elements in the list are string values and not, as would be the case without any quotes, references to named items visible to your database. IE. There is nothing named [AL] which the database should be looking for to find the value being referred to. The value is the string "AL".
                        Attached Files
                        Last edited by NeoPa; Dec 31 '15, 03:30 PM.

                        Comment

                        • hvsummer
                          New Member
                          • Aug 2015
                          • 215

                          #13
                          @neoPa: that not what I mention, I understand your idea about .defaultvalue.
                          but controls.Value look like that too ? I mean that rule may be apply to .DefaultValue, but It won't apply to .Value, right ?
                          because manytime since I use .Value property with text, I don't have to " = chr(34) & "ABC" & chr(34)" ==

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32669

                            #14
                            Originally posted by hvsummer
                            hvsummer:
                            I mean that rule may be apply to .DefaultValue, but It won't apply to .Value, right ?
                            Absolutely right.

                            .DefaultValue does appear to behave differently from others. Certainly it's different from .Value in that respect.

                            Bear in mind though, that while .Value is specifically a value, .DefaultValue is actually a reference. A simple string value enclosed in double-quotes (") can be considered to be a reference to the value within the string. Similar to a value of ="MA". We've seen this with the value of ='MA'. It can also refer to other valid items such as Date(), Now(), etc.

                            Consider the following three scenarios :
                            1. You require the default value to be the string "=Date()". Quotes included.
                              The value for .DefaultValue would be, as specified in VBA and then displayed :
                              Code:
                              X.DefaultValue = """=Date()"""
                              Debug.Print X.DefaultValue
                              "=Date()"
                            2. You require the default value to be a reference to the current date.
                              The value for .DefaultValue would be, as specified in VBA and then displayed :
                              Code:
                              X.DefaultValue = "=Date()"
                              Debug.Print X.DefaultValue
                              =Date()
                            3. You require the default value to be the string of today's date. IE. "31/12/2015" when run today but "01/01/2016" when run tomorrow, etc.
                              The value for .DefaultValue would be, as specified in VBA and then displayed :
                              Code:
                              X.DefaultValue = """" & Date() & """"
                              Debug.Print X.DefaultValue
                              "31/12/2015"
                              NB. This will differ depending on the default format on your system for dates. I'm in the UK so mine display as shown. Tomorrow this will display the new value :
                              Code:
                              "01/01/2016"
                            Last edited by NeoPa; Dec 31 '15, 04:33 PM. Reason: Various minor errors.

                            Comment

                            • msilva100
                              New Member
                              • Aug 2015
                              • 25

                              #15
                              Thank you for all your great Feedback.
                              I have the Combo Box working now. Here is the behavior that I found. The Combo Box that I was using to set a default Value was created by "Lookup Wizard" from a Table and link to Table with all the States".
                              I create Query to list the states...all states will list properly.
                              But when I proceed to filter the Query with a state "MA" I got a message "Data Type Mismatch in Criteria expression".
                              Not sure if this behavior is intended functionality or bug" I am using MS Access 2013.
                              If it is the intended functionality it would have nice to see the numeric values in the simple Query listing (without state filter).
                              Here is what was have done:
                              1. I got rid of the Lookup Wizard that was created by the table.
                              2. Created Query with Query Wizard.
                              3. Tested in the Criteria filter out state "MA". And found no error message this time.
                              4. Proceeded to follow "zmbd" recommendation:
                              Open the form in design view
                              Highlight the control
                              Right click
                              Properties
                              [Data] tab
                              [Default Value] = "MA"
                              =============== ==========
                              My default state "MA" for that combo Box works as expected.

                              thanks to everyone!!!!!!! !!!!!!!

                              ============= Happy New Year to everyone ===============

                              Comment

                              Working...