open a form with a set value in a combo box

hansnyc

Registered User.
Local time
Today, 13:14
Joined
Apr 12, 2007
Messages
50
I can't get one of my form to open with a specific value in one of its combo box. i tried to set the default value but nothing works

this is the code of the button that opens the form. it is set to return a set of services in the form's subform

the combo box is called: cboRefTypeSales and the control source is RefTypeSales

i'm trying to return the value: "Export" which is in tblTypesSales: TypesalesID = 31; TypesSales= Export



Private Sub cmdaddquote_Click()

Dim rst As New ADODB.Recordset
Dim lngQuoteID As Long
Const cFrmNa = "frmQuotation"

If MsgBox("Are you sure you want to add a new quotation?", vbQuestion + vbYesNo) = vbNo Then Exit Sub

With rst
.Open "SELECT * FROM tblQuotations WHERE 0", CurrentProject.Connection, adOpenStatic, adLockOptimistic
.AddNew
.Fields("QuoteDate") = Date
.Update
lngQuoteID = .Fields("QuotationID")
.Close
End With

CurrentProject.Connection.Execute "INSERT INTO tblServicesQuotation (RefQuotation, RefServices) " & _
" SELECT " & lngQuoteID & ", ServiceID FROM tblDefaultServices"

DoCmd.OpenForm cFrmNa
Forms(cFrmNa).Requery
Forms(cFrmNa).Recordset.FindFirst "QuotationID = " & lngQuoteID

End Sub
 
A combo box uses the ListIndex value (starting from 0) to identify which value has been selected. In order for you to pre-select select a value in a combo box when you open a form you will need to work through the values in the list, when you have found it, then select it by setting the ListIndex value to be the position you are at. My sample below simply checks the location of the value I am interested in and selects it, see below: -

My combobox contains the following values: Z;F;A;E;J;W;D;K;I

I added this code to the Form_Activate event: -

x = 0
Me.Combo0.SetFocus

Do While x <= Me.Combo0.ListCount
If Me.Combo0.ItemData(x) = "A" Then
Me.Combo0.ListIndex = x
Exit Sub
End If
x = x + 1
Loop


Hope this helps
 

Users who are viewing this thread

Back
Top Bottom