Default Value for Combo Box

G

Guest

I would like to code a form to automatically display default values in 4
combo boxes.

For example, I have four combo boxs on a form - each listing 5 possible row
options in a dropdown box from a table based on the Row Source Type set as
Table/Query. For example:

A
B
C
D
E

The user may select A in the first combo box, C in the second combo box, D
in the third combo box, and B in the fourth combo box, etc.

When the form is initially displayed, I also want to show one of the rows as
a default value in each combo box. For example, C in the first combo box, D
in the second combo box, A in the third combo box, etc.

I modified the table so that each row can be assigned a default combo box
value (1, 2, etc.,) so that the records in the table have two fields with
values such as:

Field 1 (PK) Field 2 (Default Value)
-------------- --------------------------
A 3
C 1
D 2
B 0
E 0

I then created a seperate query that filters out the 0 records and sorts the
records so that only the following rows are result:

1 C
2 D
3 A

A couple ideas that I had was

1) Have a DoCmd or something in the default value property of the combo box
that would run the query based on a value. For example the first combo box
would be DoCmd query 1 which would result in C being selected in the first
combo box, the second combo box would be DoCmd query 2 which would result in
D being selected in the second combo box, etc.

2) Have some type of a parameterized query that could be run in code behind
the form at Loadup that would filter the results of the query and enable me
to set the selected value of the combo box equal to the loaded value (C, D,
etc.)

But I been stumbling with both ideas. Any ideas and code would be
appreciated.

Thanks

Steve
 
M

MGFoster

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The easiest way is to use the ComboBox's DefaultValue property. To get
the 2nd item in the RowSource:

DefaultValue: ComboBoxName.ItemData(3)

The ItemData() index is zero-based; therefore, the 1st item is index 0,
the 2nd item is index 1, etc.

As long as you order your query's resultset as A, B, C, D, E the indexes
will be:

Value Index
A 0
B 1
C 2
.... etc. ...

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ/4wnYechKqOuFEgEQKHQgCgtc4OkQMW7HTXzdhm+t22sIhWTR4An3/9
qYvotCHrNn7SOA7PtPgY6HjP
=MWnx
-----END PGP SIGNATURE-----
 

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