Link to home
Start Free TrialLog in
Avatar of Dan89
Dan89

asked on

Listbox not displaying results from query

I have a form based on a table, 2 of the fields on this table are ContactID and ContactName, and I have another form which has no record source, when I press a button on the first form the 2nd form opens, values from ContactID and ContactName are then put into two textboxes on the 2nd form. There is a list box on this form which displays items related to this contact, I use a query as the row source which uses the value that has been put into the textbox as criteria. The code for putting in the two values into the second form from the first is:
Private Sub btnSupplier_Click()
Dim cp As String
Dim ct As String

cp = [ContactID].Value
ct = [Contact].Value

DoCmd.OpenForm "SelectSupplier", acNormal
Forms![SelectSupplier].[txtContactID] = cp
Forms![SelectSupplier].[txtContact] = ct
End Sub

The SQL statement for the listbox is:
SELECT qryContactSupplier.ItemID, qryContactSupplier.ProductName, qryContactSupplier.Description
FROM qryContactSupplier
ORDER BY qryContactSupplier.ProductName;

The problem is that when the 2nd form opens, the results from the query are not displayed, the list box is blank. If I press the refresh button the results are then displayed. I can also make a button that uses the code: Me.lstItems.requery that also works (I dont want to have to do this), but there seems to be no other way to get it to refresh, iv tired putting it in the load event for the form etc.... does not work. Any suggestions?

Thanks
Dan
Avatar of PreachDotNet
PreachDotNet

Try requerying the listbox after the form has finished updating.

Or try it on the txtContactID updated event.  it appears you are loading the form and then passing the variables so all the querying is being done prior to you passing the variables.

        Me.listbox.Requery
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dan89

ASKER

Cheers peter