DColin
asked on
Refresh a list box data on an Access form
Hi Experts,
I have a form that contains a combobox containing a list of departments.
When a department is selected I would like a listbox to display info. from a db table that belongs to the dept selected.
Here is the Row Source for the listbox.
SELECT NRN FROM tblRegion WHERE Depts =[Forms]![Form1]![ComboDeb ts];
When the form first loads the list box contains the info. relating to the combobox selected Dept. What I would like is when a new dept. is selected in the combobox the list box refreshes the data.
At the moment nothing happens unless I refresh the form from the refresh button on the home ribbon.
I have a form that contains a combobox containing a list of departments.
When a department is selected I would like a listbox to display info. from a db table that belongs to the dept selected.
Here is the Row Source for the listbox.
SELECT NRN FROM tblRegion WHERE Depts =[Forms]![Form1]![ComboDeb
When the form first loads the list box contains the info. relating to the combobox selected Dept. What I would like is when a new dept. is selected in the combobox the list box refreshes the data.
At the moment nothing happens unless I refresh the form from the refresh button on the home ribbon.
ASKER
hnasr,
This has not worked. The listbox always seems to be one update behind. As I add another entry with the combobox the previous one appears.
I am wondering is the selected combobox entry is not getting added to the database quickly enough for the listbox requery to see it.
I am using the below code to add the combobox entry to the db table.
Private sub cboDepts_Change()
Dim dbs as Database
Set dbs = openDatabase("Manpower.acc db")
dbs.Execute "INSERT INTO tblRegion (Depts, NRN) VALUES ("Me.txtDeptID", '" & Me.cboDepts.text & "');"
dbs.close
End Sub
This has not worked. The listbox always seems to be one update behind. As I add another entry with the combobox the previous one appears.
I am wondering is the selected combobox entry is not getting added to the database quickly enough for the listbox requery to see it.
I am using the below code to add the combobox entry to the db table.
Private sub cboDepts_Change()
Dim dbs as Database
Set dbs = openDatabase("Manpower.acc
dbs.Execute "INSERT INTO tblRegion (Depts, NRN) VALUES ("Me.txtDeptID", '" & Me.cboDepts.text & "');"
dbs.close
End Sub
Try to upload a simple demo database demonstrating the issue.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi DColin.
I have a question for you, the source table for the listbox, is a local /linked table in the same database file or is a table in an external database?.
Thanks.
Best regards.
Antonio (Barcelona, Spain).
I have a question for you, the source table for the listbox, is a local /linked table in the same database file or is a table in an external database?.
Thanks.
Best regards.
Antonio (Barcelona, Spain).
ASKER
Same database
That's what I guessed.
/gustav
/gustav
DColin, can you attach a copy of your database?. Really the solution is very easy.
If you post the database, I can see the form, the involved tables, the fields, relations, queries... and then help you better. Think that I don't know the table names or the field names, and is very difficult to give help.
I can prepare a sample database, with a solution, but you must modify my sample to adapt it to your database, and I know nothing about your VBA programming knowledge.
Best regards.
Antonio (Barcelona, Spain).
If you post the database, I can see the form, the involved tables, the fields, relations, queries... and then help you better. Think that I don't know the table names or the field names, and is very difficult to give help.
I can prepare a sample database, with a solution, but you must modify my sample to adapt it to your database, and I know nothing about your VBA programming knowledge.
Best regards.
Antonio (Barcelona, Spain).
Hi DColin.
I prepared a very small sample. It has two forms, one uses relationed tables and uses only one vba instruction in the click event of the combo.
The other form uses full VBA code for filling the combo and the listbox in runtime.
I hope this sample be useful for you.
Best regards.
Antonio (Barcelona, Spain).
P.D. I'm sorry, the tables and fields names are in Spanish because they are used in my DataBase applications.
codigos_postales.accdb
I prepared a very small sample. It has two forms, one uses relationed tables and uses only one vba instruction in the click event of the combo.
The other form uses full VBA code for filling the combo and the listbox in runtime.
I hope this sample be useful for you.
Best regards.
Antonio (Barcelona, Spain).
P.D. I'm sorry, the tables and fields names are in Spanish because they are used in my DataBase applications.
codigos_postales.accdb
In combo box after update event:
Open in new window