Question

I have a form that contains a combobox with a list of employees. I want to set the last selected as default value for this combobox when it open next time. Below it is following my code that isn't working.

    Private Sub combobox_AfterUpdate()
    newDefault = Form_Calc.combobox.Text
    Form_Calc.combobox.DefaultValue = newDefault
    End Sub 

The list contains complete names, something like: "John Snow";"Mary Jane Johnson";"etc"

Anyone?

Was it helpful?

Solution

The .Text property is only available while the combo has focus. At any other time you must use its .Value property.

This works with my combo box and does what I think you're asking for.

Private Sub cboAccountID_AfterUpdate()
    Me.cboAccountID.DefaultValue = Me.cboAccountID.Value
    MsgBox "DefaultValue: " & Me.cboAccountID.DefaultValue
End Sub

That approach will maintain the DefaultValue as you wish until you close the form. But you want to use the last value again when the form is opened next time.

In that case, you can save the value at Form_Close and restore it next time in Form_Load.

I use a table to store user option values. And I have this query, qryUpdateOption, to update a saved value.

PARAMETERS pKey Text ( 255 ), pValue Text ( 255 );
UPDATE tblOptions AS o
SET o.option_value = [pValue]
WHERE o.option_key=[pKey];

So at Form_Close, I can save the current value of cboAccountID.DefaultValue.

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = currentd
Set qdf = db.QueryDefs("qryUpdateOption")
qdf.Parameters("pValue").value = Me.cboAccountID.DefaultValue
qdf.Parameters("pKey").value = "cboAccountID.DefaultValue"
qdf.Execute dbFailOnError

And at Form_Load, I can retrieve that value and assign it to cboAccountID.DefaultValue.

Me.cboAccountID.DefaultValue = DLookup("option_value", _
    "tblOptions", "option_key='cboAccountID.DefaultValue'")

Note this suggestion assumes a row exists in tblOptions with the "cboAccountID.DefaultValue" key. You may want to refine this outlined suggestion.

And I think it could work for you since each of your users has their own db file which contains both the form and data ... you don't need to be concerned about one user over-writing another user's saved preference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top