Results 1 to 9 of 9
  1. #1
    wardw is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    May 2013
    Location
    Hines, Oregon
    Posts
    41

    How to select a default item in a list box?

    My form has several list boxes. The first item in each of them is "(None)", because most of the time the list items won't apply to the record being added to the database. I'd like to have the "(None)" item highlighted so the user knows it's already selected and they can just skip the list box if they don't need to select some other item.

    How can I show the item as selected when the form opens, and if it's selected by default will it also be added to the database?

  2. #2
    Sibtain is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Karachi
    Posts
    15
    ListObject.Value = ListObject.ItemData("Index/Value of None")

  3. #3
    wardw is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    May 2013
    Location
    Hines, Oregon
    Posts
    41
    Thanks for your reply, Sibtain. I tried just pasting in your code exactly as is, and Access altered it to


    [ListObject].[Value]=
    [ListObject].[ItemData]("Index/Value of None")

    but the list item (None) still wasn't selected in the form. I tried modifying the code to


    [ListObject].[Value]=
    [ListObject].[ItemData]("(None)")

    (and for some reason Access prepended an equals sign to it so it changed to this:

    =
    [ListObject].[Value]=
    [ListObject].[ItemData]("(None)")

    but the list item (None) still wasn't selected. I've tried changing the list item to just None, with no parens, but I got the same result.

    I notice something interesting in the table the list items are derived from. That table has two fields: the primary key field called NamePrefixID, and the NamePrefix field, which the form's list box is linked to. in Datasheet view, the bottom row contains (New) in theprimary key field, which is normal, but instead of the usual blank in the NamePrefix field, it has ("None"). Not sure where that came from, but if I try to delete it a new empty row appears above the bottom row, which still contains the (New) and ("None") items.

  4. #4
    Sibtain is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Karachi
    Posts
    15
    Dear Wardw

    First of all i would like to clear some thing to you that
    ListObject means your List object name it my be "MyList"
    in that case the syntex would be
    Me.MyList.Value = Me.MyList.ItemData(0)
    If you know the index of None then pass it insted of 0
    Here Zero may be the index of your "None"
    furthermore if you dont know the index of Your "None" Element then
    you have to make some function and iterate all the elements and return the value of your desired element

    and the None is coming because you may setup the default value for the field remove it in table designe mode

    Thanks and Regards
    Last edited by Sibtain; 05-24-2013 at 01:06 PM. Reason: Something got missed

  5. #5
    wardw is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    May 2013
    Location
    Hines, Oregon
    Posts
    41
    Thank you, Sibtain. I changed the code to

    =[NamePrefixes].[Value]=[NamePrefixes].[ItemData](1)

    because the first value in the NamePrefixes table's NamePrefixID field is 1. So now, if no item is selected in the form's Prefix list box, in my main table's Datasheet view showing that record, the NamePrefix field is empty. If None is deliberately selected in the list box, a 1 appears in that record's NamePrefix field. I'm concerned that if the field is empty, searching for all records whose NamePrefix is None won't find those records.

    And, unfortunately, when the form is opened, None still appears unselected by default.

  6. #6
    Sibtain is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Karachi
    Posts
    15
    try this
    =[NamePrefixes].[Value]=[NamePrefixes].[ItemData](NamePrefixID)

  7. #7
    wardw is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    May 2013
    Location
    Hines, Oregon
    Posts
    41
    I tried your suggestion, Sibtain,
    =[NamePrefixes].[Value]=[NamePrefixes].[ItemData](NamePrefixID)

    but with exactly the same results as before.

    In case you might find it useful, I checked the Property Sheet for the form's NamePrefixID field, and it says the following:

    Control Source: NamePrefixID
    Row Source: SELECT [NamePrefixes].[NamePrefixID], [NamePrefixes].[NamePrefix] FROM NamePrefixes;
    Row Source Type: Table/Query
    Bound Column: 1
    . . . .
    Show Only Row Source Value: No
    Default Value: =[NamePrefixes].[Value]=[NamePrefixes].[ItemData]([NamePrefix])
    . . . .
    Enabled: Yes
    Locked: No

  8. #8
    wardw is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    May 2013
    Location
    Hines, Oregon
    Posts
    41
    Okay; I think I've found the solution.
    1. On the form, in Design view, right-click the list box and select Properties.
    2. On the Property Sheet, open the Data tab.
    3. For the Default Value, type 1 (or whatever number the list item you want selected is)
    4. Save the form.

    Don't know why a more complex formula isn't required, but just the 1 highlighted the list item I wanted as the default, and when the form was submitted, the default item was added to that record.

    Sometimes just figuring out the right search string for a problem can really help. Here's the video showing the procedure I used: https://www.youtube.com/watch?v=h-MWOENpH0I

  9. #9
    Sibtain is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Karachi
    Posts
    15
    No dear you are missing one thing here now i get clear view of your work so
    Click image for larger version. 

Name:	SelectComboListjpg.jpg 
Views:	11 
Size:	104.1 KB 
ID:	12496

    if you look at the image which i pasted here you can easily understand how it will get done
    please give me your email so i can email you one sample program which will solve your
    problem

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

Similar Threads

  1. Replies: 2
    Last Post: 04-05-2012, 08:39 PM
  2. Replies: 4
    Last Post: 07-27-2011, 09:52 AM
  3. Replies: 1
    Last Post: 10-22-2010, 10:11 AM
  4. Can't alays select an item from a list box
    By bomber in forum Access
    Replies: 0
    Last Post: 10-16-2009, 10:01 AM
  5. Replies: 8
    Last Post: 09-24-2009, 02:56 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