Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

default value in combo box based on max value

Status
Not open for further replies.

sxschech

Technical User
Jul 11, 2002
1,033
0
36
US
I have this working as intended using vba.

however...

was wondering if is possible to do within the combo box to have the default value be the highest(max value). The combobox row source is:
[tt]
SELECT DISTINCT mid([tblSurveyExtract_Course].[EventCode],2,2) FROM [tblSurveyExtract_Course] ORDER BY 1
[/tt]

and using the above when the combo box is clicked:
14
15
16

In this case, I would like 16 to be the default so that there is no need to select the value from the drop down, unless need to view a different number.

 
I've never tried to use a max for a default value. I think this would work:
Max(mid([tblSurveyExtract_Course].[EventCode],2,2) FROM [tblSurveyExtract_Course])
Please let me know.
 
After populating your combo box, you may want to do something like:

[tt]ComboBox1.ListIndex = ComboBox1.ListCount - 1[/tt]

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Laurie, that is what I have in my vba. Probably should have included in my post, but since was working properly, didn't want to confuse the issue.

I thought about using dmax but couldn't figure out the syntax to pull the substring out for that purpose.

Code:
Private Sub Form_Load()
'Set the default year to the highest year
'based on the eventcode
'20170223
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim stsql As String
        
    stsql = "SELECT max(mid([tblSurveyExtract_Course].[EventCode],2,2)) AS EvalYear FROM [tblSurveyExtract_Course]"
    Set db = CurrentDb
    Set rs = db.OpenRecordset(stsql, dbOpenDynaset)
    Me.cboYear = rs!EvalYear
    Set rs = Nothing
    Set db = Nothing
End Sub

Andy, I tried substituting your code in the Form_Load event and says You've used the ListIndex property incorrectly.
Code:
Private Sub Form_Load()
    Me.cboYear.ListIndex = Me.cboYear.ListCount - 1
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top