We help IT Professionals succeed at work.

Access VBA -- Form Load Error

Mark01 asked
This question involves the list box control. The attached file contains the code. The list box  control in my database (EE_1140419.mdb) is lstTimes, which is on the frmMainMenu. An image of the row source for the list box control (lstTimes) is Query1.

The form load event code for the frmMainMenu form is: Me!lstTimes.Selected(0) = True. The  problem is that only the primary key field is displayed on the frmMainMenu form when the form loads (see the OnlyPrimaryKey screen shot.). How do I correct the error?

I am using with the Contacts database from the "Microsoft Access 2010 Inside Out" book as an example. I'm not going to upload the .accdb file from the book's CD because I think it is a copyright violation. The list box control (lstCName) that I am working with (using as an example) is on the Company List form (frmCompany List) in the Contacts database.

An image of the row source for the list box control (lstCName) is qlkpCompanies. The form load event code for the frmCompany List form is: Me!lstCName.Selected(0) = True.
Company List form
My Database Form
My Database Row SourceEE_1140419.mdb
Watch Question

Senior Developer
The problem is for sure that you missed to specify how many columns your data source for the list box has. You must enter this value manually:

E.g. 2 in the Column Count property. And normally we don't display ID values, so you may enter "0;5" in the Column Width property, to assign a zero width to the first column, making it non visible.

Don't mixup the Column Count property on the Format page with the Bound Column property on the Data page. The first controls what we see, the second determines what is returned by the .Value property of the ListBox control.
In design view, select your listbox, and then under the format tab, change "Column Count" from 1 to 2. This is the number of columns you want your listbox (or combobox) to have.

Then you can set the width of each individual column with the ColumnWidths property, e.g.:"1cm;1cm"
The columnwidth property is formatted according to region so might use , instead of ; and inches instead of centimeter, but you can just type the numbers.
Here you can also hide the first column (the id column) if you wish, simply set the width of the first column to 0.


ste5an and Anders Ebro, yes. I understand. The Column Width in the Contacts database is set for 0";1.75";1.25".


Thank you, ste5an and Anders.