Issue with a Listbox query. (1 Viewer)

Duff-Man

New member
Local time
Today, 03:03
Joined
Jun 30, 2022
Messages
7
Hi all,

So i'm having an unexpected issue getting a listbox to query data from another table. It seems like it should be straightforward? Hopefully it's just a syntax issue?

So, in my main form ( PO_Creation_Form ), I have a listbox that I would like to show items from another table that are associated with the record displayed in the main form.

- In the main form the field of interest is "ContractNumber", and is stored in a control "ContractNumberTextbox".
- The secondary table is "GoodsReceived", and the field to match is called "AssociatedPO". The field to show, from GoodsReceived, is "GRN", which is also the primary key.

If I create an unbound textbox on the form, and manually enter a value for the "ContractNumber" then it works as expected:

Code:
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = TmpTextbox;



... but I can't seem to get it to work with the bound textbox control. I've tried (among many others):

Code:
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = ContractNumber;
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = Me.ContractNumberTextbox;
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = Me.ContractNumberTextbox.Text;
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = Forms![PO_Creation_Form]!ContractNumberTextbox;
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = Forms![PO_Creation_Form]!ContractNumberTextbox.Text;
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = '"Forms![PO_Creation_Form]!ContractNumberTextbox"'
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = ' " & Forms![PO_Creation_Form]!ContractNumberTextbox & " ';
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] Like '" & Forms![PO_Creation_Form]!ContractNumberTextbox & "*';

... among many others. I also tried updating the rowsource for the listbox in the form's "before update" event, e.g.:

Code:
Dim sql As String
    sql = "SELECT GRN, AssociatedPO FROM GoodsReceived WHERE AssociatedPO Like '" & Me.ContractNumberTextbox.Text & "*' ORDER BY AssociatedPO"
    Me.TestListbox.RowSource = sql


Any help would be greatly appreciated. This seems like it should be fairly straightforward?

Thanks in advance!
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 03:03
Joined
Jul 21, 2014
Messages
2,276
Try setting the RowSource of TestListbox to:
Code:
SELECT [GoodsReceived].[GRN] FROM GoodsReceived WHERE [GoodsReceived].[AssociatedPO] = [ContractNumberTextbox];
 

Duff-Man

New member
Local time
Today, 03:03
Joined
Jun 30, 2022
Messages
7
Hi all,

I solved the issue, at least partially - it wasn't actually to do with the query, it was the listbox itself not updating upon switching records.

For now I've added a "TestListbox.Requery" on every command button that can change a record.

... is there a more direct way to add commands when a form moves from one record to the next? Which of the Form events are reliably performed when the form moves to a new record? I tried a few that seemed promising, but no update for the textbox.

Alternatively, is there a way to bind the listbox in such a way that it automatically changes focus when the records in the form are updated?


Thanks to everyone who read this, and I hope this can save someone else some frustration in the future!
 

Gasman

Enthusiastic Amateur
Local time
Today, 03:03
Joined
Sep 21, 2011
Messages
14,265
Form Current event runs when you change record?
If you set the rowsource to a new value, no need to requery, see what I posted in another thread here in this site.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:03
Joined
Feb 19, 2002
Messages
43,261
Do NOT use a bound control for searching. When you modify a bound control, you are UPDATING the underlying data and Access is going to save this change.

All searching should be done using unbound controls.
 

Users who are viewing this thread

Top Bottom