Results 1 to 9 of 9
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hopefully an easy question about listbox.... (Access 97)

    I have a listbox on a form that is unbound and is being filled by a query in my database. I also have a command button on this form that when you select a record our of the listbox and hit this command button, it will return another set of records into another listbox. This seems to work fine, however they want to be able to select from the listbox and return the data to the second listbox, in essence get rid of the command button. Can I use the same code? I am not sure where the code then needs to be run from...what event?

  2. #2
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    Just put it in the Click or DoubleClick event of your first listbox, depending on which action you want to use.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  3. #3
    Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    I have tried that once and it didn't work. I will try again, perhaps something needs to change in my code for this to work??

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    I often use both: users can click a command button or double click a list box, whatever they prefer.

    Private Sub lbxSelectName_DblClick(Cancel As Integer)
    ' Double clicking a name is the same as clicking the cmdSelect button
    cmdSelect_Click
    End Sub

  5. #5
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    Use the After Update Event on the listbox to launch the same code that the command button did.
    Regards,

    Gary
    (It's been a while!)

  6. #6
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    I'd have to see your code, because any of the suggestions made here (including those by Hans and Gary) should work. One more thing, is this a multiselect listbox?
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  7. #7
    Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    This is the code that I am using on the click event:


    Private Sub lstSRC_Click()

    Set rs = db.OpenRecordset("HoldSRCName")

    For Each varItem In lstSRC.ItemsSelected
    strItem = lstSRC.ItemData(varItem)
    txtSRC = strItem

    rs.MoveFirst
    Do While rs.EOF = False

    If rs!SRC = strItem Then
    txtName = rs!NAME
    Exit Do
    Else
    rs.MoveNext
    End If

    Loop
    Next



    End Sub


    It will go to the actual code but no variable is being found. I thought getting the variable would be the same as if I ran this code in a button which works. This is not a multiselect listbox. I don't know if there is some other property that needs to be set or if I can't do this with a bound field which I don't think is a true statement but obviously don't know. Any other thoughts. Thanks

  8. #8
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    I truly have no idea what you are trying to do. First of all, this is not a multiselect listbox, so why are you even fooling with the itemselected collection? If the listbox has a bound column, then the value of the listbox is the value of this column in the selected row! And why are you cycling through the recordset? My guess is that the value for rs!NAME you are getting is already being displayed in the listbox, and you can get it by using the .column property. Tell me what the columns are in the listbox, and I'll show you how easy this is.

    I think all of what you are trying to do should take exactly 1 line of code!
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hopefully an easy question about listbox.... (Access 97)

    As Mark Liquorman remarks, there is no need to use ItemsSelected if your list box is not MultiSelect.

    My guess would be that the situation is as follows:

    The first column in the list box is hidden. It contains the SRC field.
    The second column is the column the user sees. It contains the Name field.

    If this is correct, you can simplify the code to

    Private Sub lstSRC_Click()
    txtSRC = lstSRC.Column(0)
    txtName = lstSRC.Column(1)
    End Sub

    Notes:
    In VBA, column numbering is zero-based, so the first column is Column(0), the second column is Column(1), etc.
    If the first column is the bound column, you can replace lstSC.Column(0) by just lstSRC.

Posting Permissions

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