Link to home
Start Free TrialLog in
Avatar of Bevos
Bevos

asked on

Access 2007: Controlling field names in a list or combo box

Hello everyone, I have a question about how to control field names in list and combo boxes.  I have a table that I want to populate a list box and have defined captions for each of the fields.  How do I make it so that the list box displays the captions rather than the field names?

Thank you,
Bevo S.
SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bevos
Bevos

ASKER

Hi the wrong field names (no column heads) are appearing in the list box.  The box is populated with the code:

Private Sub Form_Load()

Set rs = CurrentDb.OpenRecordset("select * from tblStudyDescription")

For Each Field In rs.Fields
  lstFields.AddItem Field.Name
Next

End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What I do is simply turn off the column headings and actually put a label (or multiple Labels) across the top of the list/combobox.
Simple and effective...

JeffCoachman
untitled.JPG
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bevos

ASKER

Hi everyone, thanks for your responses! I've tried each one and am still not getting there yet.
I've also made sure I have assigned a caption to each field in the record source.  

Currently the code looks like this:

Private Sub Form_Load()

Set rs = CurrentDb.OpenRecordset("select * from qryAll")

For Each Field In rs.Fields
  lstFields.AddItem fld.Properties("Caption")
Next

End Sub

And I get the following error when loading the form: 'Run-time error '424': Object required'.   and when I debug I am pointed to: 'lstFields.AddItem fld.Properties("Caption")'.

If I go into the form, as you would expect the listbox is unpopulated.  

Any more suggestions would be very welcome :) thanks so much everyone,
Bevo S.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bevos

ASKER

Hi Fyed and THTC, thanks for the great advice! I tried out your code fyed and it doesn't work as it (it says property not found).  I think I need to do some more research about TableDef from the resource THTC gave me to figure out whats going on.  I'll let you both know if I figure something out :)

Thanks again,
Bevo S.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bevos

ASKER

This worked perfectly Nick, thanks so much for the feedback.  Hi Boag, I'm sorry if you feel that I ignored your response.  It is a really cool trick and I'm sure I'll apply it in the future, but it wasn't addressing the question that I was interested in exactly (replacing the field names in a listbox with their captions) so I didn't respond right away.  Thanks for taking the time to answer.

Thanks again everyone for helping me with this,
Bevo S.