picking up selections in list box

JahJr

Andy
Local time
Today, 05:28
Joined
Dec 3, 2008
Messages
93
I have 2 list boxes where the first one selects a category and the next one selects a product. Once the selections are made i need to be able to click place bet and get this information into a table "loggedbets". I am completly lost on this. Below is the code that links the list boxes.

Private Sub cboCategories_AfterUpdate()
Me.cboProducts.RowSource = "SELECT ProductName FROM" & _
" Products WHERE CategoryID = " & Me.cboCategories & _
" ORDER BY ProductName"

Me.cboProducts = Me.cboProducts.ItemData(0)
End Sub

Any help would be greatly appreciated
 
So you need two things.
1) Some event to trigger the system to execute the save operation.
2) The save operation.

For 1) you can use a click event of the list control you're using, looks like cboProducts (shown here with modal confirmation msgbox), or you can use a button. If you use a button you probably don't need to confirm the operation since clicking a button is a very explicit action on the part of the user.

Code:
Private Sub cboProducts_Click()
[COLOR="Green"]' handles the click event of cboProducts[/COLOR]
  if msgbox( _
    "save the " & me.cboproducts.column(1) & " product?", _
    vbyesno _
  ) = vbyes then
    DoSave
  end if
Ens Sub

For 2) you'll need SQL to open a recordset and add a record, or SQL to run an insert query. The insert query is faster but more cryptic. The recordset method (shown here) is slower but more verbose and readable. The recordset method also demonstates syntax you can use for table access that has very broad applications for storage and retrieval.

Code:
Private sub DoSave
[COLOR="Green"]' saves a record to table loggedbets, 
' uses a dao.recordset
' saves the user ID and the selected product in cboProducts
[/COLOR]  dim rst as dao.recordset
  set rst = currentdb.openrecordset( _
    "SELECT UserID, ProductID " & _
    "FROM LoggedBets")
  with rst
    .addnew
    [COLOR="Green"]'I'm guessing that who logged what is part of your system[/COLOR]
    !UserID = me.tbCurrentUserID
    !ProductID = me.cboProducts
    .update
    .close
  end with
end sub

Hope this gives some ideas about how to proceed.
 

Users who are viewing this thread

Back
Top Bottom