Change Access List Box Column Widths with VBA
Question details
Change a Microsoft Access list box's column count and widths when an option group selects a different row source.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A form uses an option group to switch the list box between row sources with different columns.
- Observed behavior
- The list box does not consistently display the expected columns and widths after the row source changes.
Open the form in Design View and check the names of the option group and list box in their property sheets. Identify the option values and the fields returned by each row source.
Update the list box in the option group's After Update event
Use this approach to apply the correct row source, column count, and widths whenever the selection changes.
Set ColumnCount to the number of fields returned by the selected row source. Provide a semicolon-separated width for each column; a width of 0 hides that column.
The following example assumes an option group named grpView, a list box named lstResults, and option values of 1 and 2. Replace the control names, table names, fields, and widths with those used in your form.
In Form Design View, select the option group, open its Property Sheet, and choose Event > After Update > [Event Procedure]. Click the ellipsis button to open the VBA editor.
Add this code inside the event procedure, using your own field and table names: Select Case Me!grpView.Value Case 1 Me!lstResults.ColumnCount = 3 Me!lstResults.ColumnWidths = "0 in;1.5 in;2 in" Me!lstResults.RowSource = "SELECT ClientID, ClientName, Email FROM Clients" Case 2 Me!lstResults.ColumnCount = 2 Me!lstResults.ColumnWidths = "0 in;2 in" Me!lstResults.RowSource = "SELECT ProductID, ProductName FROM Products" End Select Me!lstResults.Requery
Return to Form View and select each option in grpView. Check that the visible columns and their widths match the fields returned by each row source.

Work with exported Access data in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar interface for viewing and editing spreadsheet data. Export Access data to Excel to move it into WPS Spreadsheets; WPS does not run Access forms or fix their VBA.

Frequently Asked Questions
Where should I put the list box update code?
Place it in the option group's After Update event. In Form Design View, select the option group and open Property Sheet > Event > After Update.
Why is a list box column not visible?
Check the list box's Column Widths property or the ColumnWidths value in VBA. A width of 0 hides a column, and the widths must follow the order of the fields in the row source.
How many widths should I specify?
Specify one semicolon-separated width for each column returned by the row source, and set ColumnCount to that number. For example, three columns could use "0 in;1.5 in;2 in".
Will the After Update event set the list box when the form first opens?
Not by itself. After Update runs when the option group's value changes. If the list box also needs initial settings, call shared update code from the form's Load event.




