default value in combobox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a combobox based on an sql expression. The box is initially displayed
with no value. It muxh be seleted from the list. I with to have a default
value as the, say, first item. How can I do this?
 
Faberk:

The easy way to do this - and this I'm assuming that the records returned by
the SQL will normally be the same - is to set the control's default value to
the value of the first record's binding. If this is not the case you would
have to write a script to evaluate the control's row source then update the
control's default value with the value of the first record's binding.

This can be accomplished somewhat like this...

Private Sub Form_Load()
Dim sqlRowSource As String
Dim cmbComboBox As ComboBox
Dim daoDB As Database
Dim daoRS As Recordset

Set cmbComboBox = Me.TheComboBoxOnYourForm

sqlRowSource = cmbComboBox.RowSource

Set daoDB = Application.CurrentDb
Set daoRS = daoDB.OpenRecordset(sqlRowSource)

cmbComboBox.DefaultValue = daoRS(0)

daoRS.Close
daoDB.Close

sqlRowSource = Empty
Set cmbComboBox = Nothing
Set daoDB = Nothing
Set daoRS = Nothing

End Sub

Hope that helps!

-Robbie
 
In the form's OnLoad( ) event, try:

Me!cboNames = Me!cboNames.Column(0, 0)

.. . . where cboNames is the name of the combo box, the first 0 is the combo
box's
bound column (column numbering starts at zero), and the second 0 is the
first row (row numbering starts at zero).

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
I think I misread your question, and you'd rather have a default value set
for every new record, not just when the form opens. If this is the case,
then try the following in the form's OnLoad( ) event:

Me!cboNames.DefaultValue = Me!cboNames.Column(0, 0)

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Thank you gentlemen

'69 Camaro said:
I think I misread your question, and you'd rather have a default value set
for every new record, not just when the form opens. If this is the case,
then try the following in the form's OnLoad( ) event:

Me!cboNames.DefaultValue = Me!cboNames.Column(0, 0)

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Back
Top