Solved

Save form after updating default values

Posted on 2013-05-31
7
38 Views
Last Modified: 2016-07-05
I'm trying to have a form save automatically after updating values.

I have two fields that I've successfully been able to get to update the default values with the last values entered, but when the form is closed and reopened without physically saving, it reverts to the old default values.

My form has two fields and a button that runs a macro.

This is the code to update the default values of the two fields:

Option Compare Database

Private Sub Mailer_LostFocus()

    Me.Mailer.DefaultValue = Chr(34) & Me.Mailer.Value & Chr(34)

End Sub

Private Sub Start_LostFocus()

    Me.Start.DefaultValue = Chr(35) & Me.Start.Value & Chr(35)

End Sub

Open in new window


Then in the Macro that I'm running, at the end I use the "SaveObject" command and pick Object type "Form" and Object Name "Run Me".

Somehow though, the form is not actually saving when the command runs.  Is there a VBA command I can run after each of the two above to cause the form to save?

Thanks for your help!
0
Comment
Question by:dgran
  • 3
7 Comments
 
LVL 84
ID: 39211778
You can use this to force Access to save your data:

If Me.Dirty Then Me.Dirty = False

But I'm not sure what you mean by "it reverts to the old default values.". Default values are values that Access fills in for NEW records. They have no bearing on existing data.
0
 

Author Comment

by:dgran
ID: 39211851
The VBA code I use (above) updates the default value of the field to the new value entered.  When I enter a new value, save and close the form and then reopen it, the old value still appears.  After entering the new value, I've opened properties and checked the default value and it is correctly updating to the new value, but somehow, when I close and reopen the form without manually saving THAT FORM, it reverts to old default values instead of the new ones assigned by the VBA code (above).
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 39211927
I see - you're trying to persist the Default Value of a Form's CONTROL? If so, the only way to do that is by opening the form in Design view (and there are a multitude or reasons you shouldn't do that in a production database).

Instead, you could use the Current event of the Form to set your default values:

Sub Form_Current()

If Me.NewRecord Then
  Me.SomeField1 = DefaultValue1
  Me.SomeField2 = DefaultValue2
End If

You could then store your DefaultValues in a table, which can be persisted between "sessions".
0
 
LVL 21

Assisted Solution

by:Boyd (HiTechCoach) Trimmell, Microsoft Access MVP
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 250 total points
ID: 39217969
Since I compile all my front ends into a MDE/ACCDE I can't save a form's design changes.

What I do is use the Form's On Load event to load the default value for the form

A simple way for a single user apps is to read the last record in the table to set the default values in the form's On Load event

If it is a multiple user app and each user wants their own defaults then I use separate table to store the default values. You will also have to save the defaults using the Form's After Update or  On Unload event.  The table has the fields: User Name, Form Name, Control Name, and default value.  This allows a single table to hold all default values for an application
0
 
LVL 84
ID: 41691823
The two comments below provide instructions on persisting a Form's Default Value between sessions, and should be accepted as the solutions:

https://www.experts-exchange.com/questions/28144383/Save-form-after-updating-default-values.html#a39211927
https://www.experts-exchange.com/questions/28144383/Save-form-after-updating-default-values.html#a39217969
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now