Displaying two fields in combo box

M

MikeB

I have a combobox that I do a lookup in another table for.

The key is fk_Player. The combobox should display Firstname &
Lastname

What I get is that it displays two columns of first name and last name
(one each in a colums. After the selection is made, the combobox
displays only the first column (firstname). I would like to know how
to construct the combobox so it will display a person's full name.

I think I have done it in the past by creating a query where one field
combines the firstname and lastname fields. Is that the only way or
can I use the table data directly?

Thanks.
 
J

Jeff Boyce

Mike

If I'm reading between the lines correctly, you are attempting to do this
directly in a table. STOP! Step away from the keyboard!

Even though MS Access offers a "lookup" data type, it seems to cause more
confusion than it solves. The lookup data type displays the looked-up value
(one field only), but stores the primary key of the row. Thus, for example,
when you try to create a query and use one of the looked-up values as the
criterion, you get NOTHING! That's because the field actually holds the
IDs.

The preferred approach is to store only the ID in the table, then use a
query to do the kind of concatenation you mentioned ([FirstName] & " " &
[LastName]), then display that in a combobox on a form.

Access tables may look like a spreadsheet, but tables are merely "buckets o'
data". Access forms have a very rich event environment, so you can take
much stronger control over what is displayed and how.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
M

MikeB

Hi Steve, thanks, but I'm missing something.

Here is my properties. http://i40.tinypic.com/oifb52.jpg I believe
they are as they should be. 3 columns, one for the foreign key, one
each for First and Last name. The widths hide the FK.

The form acts like this: http://i40.tinypic.com/oifb52.jpg

You can see that the top row is static, it simply shows the first
name. The 2nd row is in the process of being filled in. The drop-down
shows both columns.

What I'd like is to have both names appear in the completed combobox.

Mile
 
M

MikeB

Hi Jeff,
Thanks for your input. Let me see if I understand you correctly.
Perhaps I should explain more.

I have Professors and Courses and Sections. Each Course can have
multiple Sections. Each Professor can teach a number of Sections (or
none).

So in the table tblSections there are fields for fk_Course and
fk_Prof. I'm trying to create a form that will allow me to review
which professor is teaching which section and perhaps assign/change
professors.

If I understand you correctly, I should create a query qryProfs that
has a field where LastName and FirstName are concatenated and where
the ProfID key is also a field. Then I should store this value (which
is the same key as the ProfID key in tblProfs in the tblSections. Have
I got it right? Easy enough. Thanks.

I was wondering if I could do it without having to construct the query
for the sole purpose of concatenating the fields.

Mike

If I'm reading between the lines correctly, you are attempting to do this
directly in a table.  STOP!  Step away from the keyboard!

Even though MS Access offers a "lookup" data type, it seems to cause more
confusion than it solves.  The lookup data type displays the looked-up value
(one field only), but stores the primary key of the row.  Thus, for example,
when you try to create a query and use one of the looked-up values as the
criterion, you get NOTHING!  That's because the field actually holds the
IDs.

The preferred approach is to store only the ID in the table, then use a
query to do the kind of concatenation you mentioned ([FirstName] & " " &
[LastName]), then display that in a combobox on a form.

Access tables may look like a spreadsheet, but tables are merely "bucketso'
data".  Access forms have a very rich event environment, so you can take
much stronger control over what is displayed and how.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP


I have a combobox that I do a lookup in another table for.
The key is fk_Player.  The combobox should display Firstname &
Lastname
What I get is that it displays two columns of first name and last name
(one each in a colums. After the selection is made, the combobox
displays only the first column (firstname).  I would like to know how
to construct the combobox so it will display a person's full name.
I think I have done it in the past by creating a query where one field
combines the firstname and lastname fields. Is that the only way or
can I use the table data directly?
 
J

Jeff Boyce

I will encourage you to work in forms, not directly in the tables, and to
use queries to 'feed' your comboboxes. That way, you can create a simple
combobox on your form that is labeled, for example, "Professor". The field
in the underlying table (i.e., behind the form) to which that combobox is
bound is a foreign key field that points to the rowID of the professor
selected from the professor table.

But what the combobox displays (by setting the width of the first column,
the ID, to 0) is the "second" field in the query. If you use, from your
Professor table, something like: [LastName] & ", " & [FirstName] (assuming
those are the names you've put on the fields), the your combobox will be
displaying the LastName, FirstName of the selected professor.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP



Hi Jeff,
Thanks for your input. Let me see if I understand you correctly.
Perhaps I should explain more.

I have Professors and Courses and Sections. Each Course can have
multiple Sections. Each Professor can teach a number of Sections (or
none).

So in the table tblSections there are fields for fk_Course and
fk_Prof. I'm trying to create a form that will allow me to review
which professor is teaching which section and perhaps assign/change
professors.

If I understand you correctly, I should create a query qryProfs that
has a field where LastName and FirstName are concatenated and where
the ProfID key is also a field. Then I should store this value (which
is the same key as the ProfID key in tblProfs in the tblSections. Have
I got it right? Easy enough. Thanks.

I was wondering if I could do it without having to construct the query
for the sole purpose of concatenating the fields.

Mike

If I'm reading between the lines correctly, you are attempting to do this
directly in a table. STOP! Step away from the keyboard!

Even though MS Access offers a "lookup" data type, it seems to cause more
confusion than it solves. The lookup data type displays the looked-up
value
(one field only), but stores the primary key of the row. Thus, for
example,
when you try to create a query and use one of the looked-up values as the
criterion, you get NOTHING! That's because the field actually holds the
IDs.

The preferred approach is to store only the ID in the table, then use a
query to do the kind of concatenation you mentioned ([FirstName] & " " &
[LastName]), then display that in a combobox on a form.

Access tables may look like a spreadsheet, but tables are merely "buckets
o'
data". Access forms have a very rich event environment, so you can take
much stronger control over what is displayed and how.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP


I have a combobox that I do a lookup in another table for.
The key is fk_Player. The combobox should display Firstname &
Lastname
What I get is that it displays two columns of first name and last name
(one each in a colums. After the selection is made, the combobox
displays only the first column (firstname). I would like to know how
to construct the combobox so it will display a person's full name.
I think I have done it in the past by creating a query where one field
combines the firstname and lastname fields. Is that the only way or
can I use the table data directly?
 
J

John... Visio MVP

Did you paste the wrong image for the form? (they are both oifb52.jpg)?

John... Visio MVP
Hi Steve, thanks, but I'm missing something.

Here is my properties. http://i40.tinypic.com/oifb52.jpg I believe
they are as they should be. 3 columns, one for the foreign key, one
each for First and Last name. The widths hide the FK.

The form acts like this: http://i40.tinypic.com/oifb52.jpg

You can see that the top row is static, it simply shows the first
name. The 2nd row is in the process of being filled in. The drop-down
shows both columns.

What I'd like is to have both names appear in the completed combobox.

Mile
 
D

Damon Heron

The only way I know to accomplish this is to put a textbox that covers the
combobox field (but not the down arrow) and have this code in the
afterupdate of the combobox:

Me.txtCover.Visible = True
Me.txtCover.SetFocus
Me.txtCover = Me.Combo1.Column(1) & " " & Me.Combo1.Column(2)
Me.txtCover.SelStart = 0

The default visible property of the textbox is false, locked = true.

Damon


Hi Steve, thanks, but I'm missing something.

Here is my properties. http://i40.tinypic.com/oifb52.jpg I believe
they are as they should be. 3 columns, one for the foreign key, one
each for First and Last name. The widths hide the FK.

The form acts like this: http://i40.tinypic.com/oifb52.jpg

You can see that the top row is static, it simply shows the first
name. The 2nd row is in the process of being filled in. The drop-down
shows both columns.

What I'd like is to have both names appear in the completed combobox.

Mile
 
M

MikeB

Did you paste the wrong image for the form? (they are both oifb52.jpg)?

<smacks forehead>

Sorry!

http://i42.tinypic.com/2cdbcef.jpg is how the form appears. But I
believe, based on Jeff Boyce's responses (even though I odn't yet
fully understand what he's saying), that the question is now moot,
since I created a query to combine the first and last names and I'm
displaying the combobox from the query.
 
B

Bob Quintal

m:
Hi Jeff,

I was wondering if I could do it without having to construct the
query for the sole purpose of concatenating the fields.
I'm interpreting your statement as "I don't want to have this query
appear in the database objects window."

If that is the reason, instead of having the stand-alone query, you can
paste the SQL into the Combobox's Row Source Property. Once the form is
saved you can delete the query from the queries list.

..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top