Results 1 to 9 of 9
  1. #1
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36

    List Box Update

    I have a form with a combo box, a list box and a button. The data in the combo box is from FieldA from Table1 and the list box is populated from a query that selects all the records in Table2 that have a null IDField. Table2 is related to Table1 by the index for FieldA (IDField)...

    On click, I want the selected record(s) from the list box to have their IDField updated using the contents of the combo box.

    I'm pretty inexperienced and can't imagine how to go about this. Hope I explained it well enough.

  2. #2
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    I'm assuming you want to update Table2 when you click on the button. You'll need to write some VBA code for the On Click event of the button.

    Basically, loop through the selected items in the ListBox and update using value from the combo box.

    Here's a skeleton code to get you started.
    Code:
     
    Dim oItem As Variant
     
    If Me!mylistbox.ItemsSelected.Count <> 0 Then
        For Each oItem In Me!mylistbox.ItemsSelected
            ' Use DAO recordset to update those selected records
            ' either with SQL or using RecordSet.Edit and RecordSet.Update methods
            ' .....
        Next oItem
    Else
        MsgBox "Nothing was selected from the list", vbInformation
        Exit Sub 'Nothing was selected
    End If

  3. #3
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Yes, that is what I want to do. I've done loops and recordsets before in SQL but how to do this in VBA? I've only recently figured out how to open the code modules.

  4. #4
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    What makes it hard for me to grasp is that the list box is populated with a query that holds two fields from Table2. So how do I reference just one of these fields in an update statement?

  5. #5
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Okay, I looked up the syntax elsewhere on the web. Here is what I have. When I test it, absolutely nothing happens, not even an error. I'm clearly missing something important!

    Dim SQLMasterUpdate As String
    Dim oItem As Variant

    If Me!RestaurantListBox.ItemsSelected.Count <> 0 Then
    For Each oItem In Me!RestaurantListBox.ItemsSelected

    SQLMasterUpdate = "UPDATE Restaurant SET " & _
    "Restaurant.AccountManagerID = (SELECT AccountManager.AccountManagerID " & _
    "WHERE AccountManagerName = " & AccountManagerName.Text & ")" & _
    "WHERE ((SELECT RestaurantInfo, RestaurantPhone FROM Restaurant) = " & oItem & ");"
    DoCmd.RunSQL SQLMasterUpdate
    Next oItem
    Else
    MsgBox "Nothing was selected from the list", vbInformation
    Exit Sub 'Nothing was selected
    End If
    End Sub

  6. #6
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Well, now obviously the oItem variable is simply an integer. I had overlooked that. Soo.... how do I pull the values out of the list box in order to run the update query on all records that match the list box?

  7. #7
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    Actually, oItem is referring to the index for the row. So, maybe you might to rename it accordingly to say iRow.

    To get the value, you can do the following:

    Code:
    Me.RestaurantListBox.ItemData(iRow)
    If you have multiple columns, then you need to specify the index for the column to get the specific value you want, for example:

    Code:
    Me.RestaurantListBox.Column(iColumn, iRow)

  8. #8
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Thanks a BUNCH! That's what I was looking for.

  9. #9
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    You're welcome.

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

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2011, 09:30 PM
  2. List box update from combo box choice
    By allykid in forum Forms
    Replies: 1
    Last Post: 03-08-2011, 10:06 PM
  3. Update table based on List Box selection
    By tpcervelo in forum Forms
    Replies: 0
    Last Post: 11-04-2010, 01:32 PM
  4. Update Field list in Table with Query
    By Scorpio11 in forum Queries
    Replies: 3
    Last Post: 07-16-2010, 01:57 PM
  5. Input data from one list box to another list box
    By KellyR in forum Programming
    Replies: 0
    Last Post: 06-04-2010, 11:24 AM

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