Default Values form controls (1 Viewer)

YYYY

Member
Local time
Today, 17:02
Joined
Apr 21, 2020
Messages
39
trying to figure out,
I can set Default values for form bound and unbound control in Property Sheet.
but could i set it with VBA?
I tried to add a button on a form, running code to save the current value of specific control to default values,
Code:
me.control.defaultvalue = me.control
when I change the form to design view and look in the property sheet, the default value property changed to the new value.,
but when i close the form and reopen it - it gets back to the previous default value.
can any one explain?
 

June7

AWF VIP
Local time
Today, 13:02
Joined
Mar 9, 2014
Messages
5,466
The programmatic change is not permanent unless you save the form from DesignView.
 

YYYY

Member
Local time
Today, 17:02
Joined
Apr 21, 2020
Messages
39
The programmatic change is not permanent unless you save the form from DesignView.
no way to save it from VBA (maybe changing form to design view and saving)...?!
 

June7

AWF VIP
Local time
Today, 13:02
Joined
Mar 9, 2014
Messages
5,466
Yes, code can do that but why? Users should not be modifying db design.

Use code in form load event to set control DefaultValue.
 

YYYY

Member
Local time
Today, 17:02
Joined
Apr 21, 2020
Messages
39
I"ll ask differently,
how would you handle my scenario?
making a way user could save his choices in the forms unbound/bound control appear by default the next time he opens the form?
 

June7

AWF VIP
Local time
Today, 13:02
Joined
Mar 9, 2014
Messages
5,466
Are there multiple users of this db? How do you organize data? Are records associated with specific users?

You could save this info to a Users table.

Regardless, would need to somehow identify who the user is to pull up their specific info.
 

June7

AWF VIP
Local time
Today, 13:02
Joined
Mar 9, 2014
Messages
5,466
Regardless if value is saved to a "Users" or "DefultSettings" table or value pulled from most recent record and/or form is switched to design view to save, requires VBA code in some event to set DefaultValue property, such as form Load and/or control's AfterUpdate.
 

Isaac

Lifelong Learner
Local time
Today, 14:02
Joined
Mar 14, 2017
Messages
8,777
making a way user could save his choices in the forms unbound/bound control appear by default the next time he opens the form?
You would use vba to do some kind of a lookup value (looking at the table data), in the form's Load event., and populating the textbox's .value property (no need to worry about the default value), at that time.
 

June7

AWF VIP
Local time
Today, 13:02
Joined
Mar 9, 2014
Messages
5,466
Setting Value property will set record in edit mode and if focus is on existing record, possibly change data. Depends on how form is opened. Safer to set DefaultValue.
 

Isaac

Lifelong Learner
Local time
Today, 14:02
Joined
Mar 14, 2017
Messages
8,777
You're assuming the form is bound … in which case the default value will matter even less, because the bound form will display data from the recordsource it is bound to. I guess OP's requirement isn't clear to me.
 

YYYY

Member
Local time
Today, 17:02
Joined
Apr 21, 2020
Messages
39
my form lets user select options from unbound textboxes and combo boxes
[date range, amount, Account, etc.]
and then press CMD Print Report to see a report of transactions in the selected options.

I wanted the CMD button should also show message box
Do you want to save your selection for an other time?
and with code if (msgbox"...",vbyesno) = vbyes then
set defaults for next time.

Am I clear?
 

June7

AWF VIP
Local time
Today, 13:02
Joined
Mar 9, 2014
Messages
5,466
Then you need to either save their response and look it up next time form opens or save edited form. Something to keep in mind about saving modified form, if a new version of db is distributed, this custom default will not be in form design. You say there is only one user. Does that mean db is not split design?
 

YYYY

Member
Local time
Today, 17:02
Joined
Apr 21, 2020
Messages
39
yes.

for people looking up this string I"m providing the standard answer how to create a defaults structure.
create a table, give it a name DefaultsTable
add fields corresponding to the fields in your regular table or form, and a field with autonumbering named ID
create record 1 with the defaults you want for your table 1 or form 1.

in the default value property [in the property sheet of the table or form you are trying to add the defaults to]
add the following:
=dlookup("Fieldname","DefaultsTable","ID =1")
in your table 1 or form 1.

you can add defaults for another table or form in the next record of DefaultsTable
and then using
=dlookup("Fieldname","DefaultsTable","ID =2")
in your table 2 or form 2

and so on.

you can then add code to your form
to update the DefaultsTable with new defaults for specific fields where ID = the number corresponding to your form.
 

Users who are viewing this thread

Top Bottom