Listbox SQL (1 Viewer)

todavy

Registered User.
Local time
Today, 03:28
Joined
Dec 1, 2009
Messages
17
How do I make a listbox on a form to list all recommended customers?

The form, named "Customer Form", is based on a simple table named "Customers" with the fields ID,Name,Recommended_by. The field "Recommended_by" has the datatype Long Integer and just stores another custromers ID number.

On the Form is a listbox in which I want to display all customers (name only) that has been recommended by the customer that is presently beeing displayed in the form.

I set the listbox's rowsource to:
SELECT Customer.ID, Customer.Name, Customer.Recommended_by
FROM Customers WHERE Customers.Recommended_by=[Forms]![Custumer Form]![ID];

But this doesent work! The listbox get empty!

If I put a textbox in the form and just write =[Forms]![Custumer Form]![ID]; it shows the ID so theres nothing wrong with anything else it seems. Also if I change the "[Forms]![Custumer Form]![ID]" to an ID number, lets say 1141, it shows all customers with ths ID numer in the field Recommended_by.

Whats wrong?
 

bob fitz

AWF VIP
Local time
Today, 03:28
Joined
May 23, 2011
Messages
4,727
Code:
SELECT Customer.ID, Customer.Name, Customer.Recommended_by
FROM Customers WHERE Customers.Recommended_by=[Forms]![Cust[B][COLOR=red]u[/COLOR][/B]mer Form]![ID];
Could this be the cause, or is that just a typo in the posting.
Also, I think you will need to requery the combo box in the form's On Current event.
 

todavy

Registered User.
Local time
Today, 03:28
Joined
Dec 1, 2009
Messages
17
Just a typo! What is "requery the combobox" ?
 

missinglinq

AWF VIP
Local time
Yesterday, 22:28
Joined
Jun 20, 2003
Messages
6,423
Well, that 'just a typo' will keep the code from functioning correctly! Reference my signature, in red, below!

As for the 'requery the combobox,' the code would be
Code:
Private Sub Form_Current()
 Me.YourComboboxName.Requery
End Sub
Just replace YourComboboxName with the actual name of your Combobox. This causes the Combobox/Listbox to be updated with the Records related to the current CustomerID.

Linq ;0)>
 

todavy

Registered User.
Local time
Today, 03:28
Joined
Dec 1, 2009
Messages
17
Thanks for the help!

The typo is not in the database it just above (didn't copy and paste the code). As I said the =[Forms]![Customer Form]![ID]; worked fine in a textbox and its exactly the same in the Listbox.

Do I have to call the sub from somewhere or does it autorun when put in the standard module along with my other functions?
 

todavy

Registered User.
Local time
Today, 03:28
Joined
Dec 1, 2009
Messages
17
Case closed!

I put the function in the forms module (of course):

That solved it! Wonder why...

Thanks!!! :)
 

Users who are viewing this thread

Top Bottom