1

I am trying to set the default of a control using VBA so that when I navigate away from the form and return later, the default value will be what I set it to previously.

I would expect this to work:

Private Sub searchButton_Click()
    Me.searchType.DefaultValue = Me.searchType.Value
End Sub

Is it possible to set the default value of a control?

2 Answers 2

3

Persistant changes to the DefaultValue can only be made in DesignView. Look here and at Allan Browne's reply here

Sign up to request clarification or add additional context in comments.

1 Comment

Seems odd. Fair enough, I guess will have to make a table with the default value in it.
2

I guess (it's not clear from your question) that you load a form, do stuff, save a default value, close (unload) the form. On a new load, you want to set the saved default value.

For that to work, you need to save the default value in a variable outside the form. Once you unload the form, all variables inside that form are released from memory.

So make a variable in the code module where you load the form.

As an example, put this in a code module:

Option Explicit
Public Default_value As Boolean

Sub jzz()

'load and show form
Load testForm
testForm.Show

'load and show form again
Load testForm
'set default value:
testForm.Default_checkbox.Value = Default_value
testForm.Show

End Sub

And a userform named testForm, with 2 elements, a checkbox named Default_checkbox and a Commandbutton.

Option Explicit

Private Sub OK_button_Click()
'save default value in Module1:
Module1.Default_value = Me.Default_checkbox.Value

'unload form
Unload Me
End Sub

3 Comments

Cool idea. But I think I might just store the value in a table, rather than a variable in a module.
Database storage is better. Module variables get lost on error and when closing down the db, afaik.
This works fine. It should be the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.