add value to combo box

  • Thread starter Thread starter jw
  • Start date Start date
J

jw

Hi,

I would like to understand how to add a value to a bound
combo box and an unbound combo box. I have read the Help
files and read through answers to a google search.

I know how to go into the properties and add a value to
an unbound combo box. I would like the end user to be
able to do this with more automation. It may be simper to
teach them how to go into the recordsource and add a new
value to the list. Is there a simple way to automate
this?

Adding a value to an unbound combo box seems very
complicated. I looked at sample code on the
http://www.mvps.org/access/, and I have understood and
used simple code that people helped me with before.
However, I wonder if such a common event procedure would
have more automation in Access 2002.

I would appreciate any guidance on how the best way to go
about adding a value to both a bound and unbound combo
box list.

Thank you in advance.
 
jw said:
Hi,

I would like to understand how to add a value to a bound
combo box and an unbound combo box. I have read the Help
files and read through answers to a google search.

I know how to go into the properties and add a value to
an unbound combo box. I would like the end user to be
able to do this with more automation. It may be simper to
teach them how to go into the recordsource and add a new
value to the list. Is there a simple way to automate
this?

Adding a value to an unbound combo box seems very
complicated. I looked at sample code on the
http://www.mvps.org/access/, and I have understood and
used simple code that people helped me with before.
However, I wonder if such a common event procedure would
have more automation in Access 2002.

I would appreciate any guidance on how the best way to go
about adding a value to both a bound and unbound combo
box list.

Thank you in advance.

Whether the combo box is bound or not doesn't make any difference, if by
"bound" you mean what is normally meant: having a controlsource that is
a field in the form's recordsource. But I wonder if maybe you are
confusing "bound" with having a table or query as the *rowsource* (not
the recordsource) of the combo box, as opposed to having a value list
for the rowsource. Could you clarify exactly what you mean?

If you want to be able to add entries to a combo box's rowsource on the
fly, and want the new entries to be permanent, then it is easiest to use
a table as the rowsource and simply insert a new record in the rowsource
table. I only use value lists when they are absolutely never going to
change.
 
Thank you for your response! As you suggested I was
confusing unbound and bound. What I would like to know
is how to allow the user to add a value to the list when
the list is based on a query (based on a table).

For my forms, generally I can adjust the value list
myself for the forms with the rowsource based on a value
list, becaues these change very infrequently.

If the recordsource is a query, the end user will
frequently want to add the new value to the list and it
is this I most want to automate.

If I can figure out how to have both of these situations
automated, that would be great! I would like to set it
up so the end user is asked if they would like to add the
value to the list or not. I think my usefulness would
improve if I knew how to do these two tasks.

I would like to check if I understood what you
suggested. I think you suggested the easiest way to add
new values permanently to the rowsource is to base the
rowsource on a table and to add the value to that table.
I currently have a query (based on a base table) as
rowsource. I could create a table from this query and
then delete the query but use the table as row source.
(As tables can have values added as needed.)

What would be involved in the more difficult way, to set
it up where the user is asked if they would like to add
the value to the list? Is this process the same for a
rowsource based on a query or table and a rowsource based
on a value list?

Thanks again for your help!
 
jw said:
Thank you for your response! As you suggested I was
confusing unbound and bound. What I would like to know
is how to allow the user to add a value to the list when
the list is based on a query (based on a table).

For my forms, generally I can adjust the value list
myself for the forms with the rowsource based on a value
list, becaues these change very infrequently.

If the recordsource is a query, the end user will
frequently want to add the new value to the list and it
is this I most want to automate.

If I can figure out how to have both of these situations
automated, that would be great! I would like to set it
up so the end user is asked if they would like to add the
value to the list or not. I think my usefulness would
improve if I knew how to do these two tasks.

I would like to check if I understood what you
suggested. I think you suggested the easiest way to add
new values permanently to the rowsource is to base the
rowsource on a table and to add the value to that table.
I currently have a query (based on a base table) as
rowsource. I could create a table from this query and
then delete the query but use the table as row source.
(As tables can have values added as needed.)

That's not exactly what I meant. The combo box's rowsource may be a
query, and provided that the query is updatable, you can treat the query
as though it were a table, for the purpose of adding an item to the
combo box. You don't have to create a table from the query or anything
like that. Furthermore, the query is still based on one or more tables,
and it's just as easy to add a new item directly to that table (or
tables) as it is to add items to the query. That's something you can do
even if the rowsource query itself is not updatable, so that's what I'd
recommend.
What would be involved in the more difficult way, to set
it up where the user is asked if they would like to add
the value to the list? Is this process the same for a
rowsource based on a query or table and a rowsource based
on a value list?

For most practical purposes you can't really add items to a value-list
rowsource and have them stick. That is, you can add them for the time
the form is open -- the example in the help file under "NotInList
Event - Event Procedures" shows this -- but after you close the form,
the next time you open it the values you added at run time will be gone
again. So you're better off sticking with RowSourceType="Table/Query"
for lists you want to be able to add to on the fly.

In adding items to a table-based list (and that includes query-based
lists), the main question is whether you need to add more information to
the table than the user has already typed. Consider the following
simple case:

Table: tblCountries
Field: CountryID (autonumber primary key)
Field: CountryName (text)

Combo box: cboCountry
RowSource: SELECT CountryID, CountryName
FROM tblCountries
ORDER BY CountryName;
BoundColumn: 1
ColumnWidths: 0"; 1.3"
LimitToList: Yes

The combo box stores the country ID, but displays the name of the
country.
This combo can easily have a country added in its NotInList event,
without having to open an edit form:

Private Sub cboCountry_NotInList(NewData As String, Response As
Integer)

On Error GoTo Err_Handler

Select Case _
MsgBox("The country '" & NewData & "' is not in the
list. " & _
"Do you want to add it?", _
vbQuestion + vbYesNo, _
"Not In List")

Case vbNo
Response = acDataErrContinue

Case vbYes
' Add the new country directly to the table.
CurrentDb.Execute _
"INSERT INTO tblCountries(CountryName) " & _
"VALUES(" & Chr(34) & NewData & Chr(34) & ")", _
dbFailOnError

' Tell Access we've added an item, so that it requeries
' the combo box.
Response = acDataErrAdded

End Select

Exit_Point:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbExclamation, "Unable to Add Item"
Resume Exit_Point

End Sub

That takes care of the simple case, where no additional information is
needed to add a new item.

When adding an item requires more information -- for example, other
required fields in the table where you want to insert the new record --
then what I do is open an edit form on that table in dialog mode, and
pass the NewData to that form in via the OpenArgs argument; e.g.,

'...
Case vbYes
' Open a form to add the country and other required
fields.
' This code will pause until the form is closed.

DoCmd.OpenForm "frmCountries", _
WindowMode:=acDialog, _
OpenArgs:="ADD=" & NewData

' Tell Access we've added an item, so that it requeries
' the combo box.
Response = acDataErrAdded

Code in that form's Open event checks the OpenArgs and sets up the form
appropriately to add the new item, leaving only the other required
fields for the user to fill in. By opening this form in dialog mode, I
make the code in the NotInList event pause until the user closes that
form, and then the code proceeds as usual.
 
Thank you for this information!

I based the rowsource of my FormPlantCover on a query
that draws values out of TblPlantCover.SpeciesNames. So
all the plant species names are available from the combo
box drop down list. Each year more plant species may be
found and need to be added to the list. I tested
entering a record with a species called "NEW" into the
form and closed the form. This record went into the base
table. When I opened the form again the species "NEW"
was available on my combo box list. This means that the
query is updatable and will contain any values
for "Species" that are entered into the base table within
legitimate records.

So if the end user wants to add the value to the list ,
they simply have to use the value once in a legit record,
close the form and reopen the form and the value will be
added to the list.

As far as the more difficult case of asking the user if
they want to to add to list, it seems I should stick to
tables/queries (as opposed to value lists)to use the code
you included.

Thank you for the two examples! I can see more
possibilities to how improve my database for the end user!
 
jw said:
Thank you for this information!

I based the rowsource of my FormPlantCover on a query
that draws values out of TblPlantCover.SpeciesNames. So
all the plant species names are available from the combo
box drop down list. Each year more plant species may be
found and need to be added to the list. I tested
entering a record with a species called "NEW" into the
form and closed the form. This record went into the base
table. When I opened the form again the species "NEW"
was available on my combo box list. This means that the
query is updatable and will contain any values
for "Species" that are entered into the base table within
legitimate records.

So if the end user wants to add the value to the list ,
they simply have to use the value once in a legit record,
close the form and reopen the form and the value will be
added to the list.

This sounds like the somewhat different, specialized case where the
combo box draws its values from the same table on which the form itself
is based. In that case, you don't need any special code to add the item
to the list, but you must set the combo box's NotInList property to No.
Also, if you want to make the new item appear in the list without
closing and reopening the form, you must requery the combo box:

Me!cboMyCombo.Requery

(in some appropriate event). Of course, by doing it this way you give
up complete control over what items get added; if the user misspells
some item that is already in the list, that misspelling will be accepted
as a new item. I can think of a way to regain control, but it's
probably not worth going into it now.
As far as the more difficult case of asking the user if
they want to to add to list, it seems I should stick to
tables/queries (as opposed to value lists)to use the code
you included.

Thank you for the two examples! I can see more
possibilities to how improve my database for the end user!

You're welcome.
 
Back
Top