Combobox Selection To Populate Listbox
I want to have a code in VBA where i can populate a list box based on the value of a combo box because i need to give the my user ability to select from list box i have also try to ask my friends and try to find it over internet but was not able to get it so i have posted here hope my query gets solved.
Combobox Selection To Populate Listbox
Try to follow the below steps:-
- Open the sample database Northwind.
- In the Database Window, click Forms, and then click New.
- In the New Form dialog box, click Design View, and then click OK.
- Add a combo box control on the form.
- Right-click the combo box, click Properties, and then set the following properties for the combo box
Code:
Name: cmbReports
RowSourceType: Table/Query
LimitToList: Yes
Left: 2"
Top: 0.5"
Width: 2"
- Now assign the following SQL statement to the RowSource property:
Code:
SELECT Name, Type FROM MSysObjects WHERE Type=-32764 ORDER BY Name;
- Now right-click on label, click Properties, and then set the following properties for the label:
Code:
Name: lblReports
Caption: Reports:
Left: 1"
Top: 0.5"
Width: 1"
FontWeight: Bold
TextAlign: Right
- On the File menu, click Save, type frmListReports in the Form Name box, and then click OK.
- On the View menu, click Form View. Note that the reports are listed in alphabetical order.
Re: Combobox Selection To Populate Listbox
I have try to do the following things but still facing same issue. Is there any other method then please post me.
Re: Combobox Selection To Populate Listbox
Try to follow this steps i am sure this will work for you.
- Open the sample database Northwind.
- In the Database window, click Forms, and then click New.
- In the New Form dialog box, click Design View, and then click OK.
- Add a combo box control to the form.
- Right-click the combo box, click Properties, and then set the following properties for the combo box:
Code:
Name: cmbReports
RowSourceType: fncGetReports
LimitToList: Yes
Left: 2"
Top: 0.5"
Width: 2"
- Right-click the label, click Properties, and then set the following properties for the label:
Code:
Name: lblReports
Caption: Reports:
Left: 1"
Top: 0.5"
Width: 1"
FontWeight: Bold
TextAlign: Right
- On the View menu, click Code, and then copy or paste the following code:
Code:
Function fncGetReports(Ctrl As Control, varID As Variant, _
varRow As Variant, varCol As Variant, varCode As Variant) _
As Variant
'Assign all report names to the combo box.
Dim obj As AccessObject
Dim proj As CurrentProject
Set proj = Application.CurrentProject
Select Case varCode
Case acLBInitialize
fncGetReports = True
Case acLBOpen
fncGetReports = Timer
Case acLBGetRowCount
fncGetReports = proj.AllReports.Count
Case acLBGetColumnCount
fncGetReports = 1
Case acLBGetColumnWidth
fncGetReports = -1
Case acLBGetValue
Set obj = proj.AllReports(varRow)
fncGetReports = obj.Name
End Select
End Function
- On the File menu, click Close and Return to Microsoft Access.
- On the File menu, click Save, type frmListReports in the Form Name box, and then click OK.
- On the View menu, click Form View. Note that the reports are not listed in alphabetical order. For example, the Employee Sales by Country report appears second from the bottom instead of immediately following the Customer Labels report.