Results 1 to 4 of 4
  1. #1
    Juan4412 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    200

    Populate List Box & Read Value From VBA

    I want to only do this with VBA as the values are going to be static and never change. I am going to add a listbox and name it listbox1 - I need to use vba to add a NULL value - default selection, and add the values Joe, John. And also in VBA read the selected value from the listbox. pseudocode would be


    Code:
    'populatelistbox
    listbox1.Values = Array(NULL, "Joe", "John")
    
    'read listbox values
    if listbox1.Value = NULL Then 
       MessageBox.Show("You must select a name")
    Else
       pickedvalue = listbox1.Selection
    endif
    
    'process depending
    if pickedvalue = "Joe" Then
        MessageBox.Show "You Picked Joe"
    Else if pickedvalue = "John" Then
        MessageBox.Show "You Picked John"
    End if

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    21,712
    If the values are static, why not set them in design view and be done with it? To do it in code you'd use AddItem I think, but I've never bothered.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Juan4412 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    200
    @pbadly - setting in design works perfectly. How would I read the value selected?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    21,712
    If the list box is single select and the code is on the same form:

    Msgbox Me.ListboxName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. How to populate list box from an array?
    By AAAndy in forum Forms
    Replies: 5
    Last Post: 08-19-2016, 08:23 AM
  2. Populate Text Box With Selected Value From List Box
    By chalupabatman in forum Forms
    Replies: 27
    Last Post: 08-17-2016, 06:24 AM
  3. Replies: 1
    Last Post: 11-23-2012, 10:26 PM
  4. Replies: 1
    Last Post: 05-11-2011, 02:51 AM
  5. List box to populate other list boxes
    By Nathan in forum Forms
    Replies: 0
    Last Post: 03-03-2009, 07:22 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 - Senior Forums