Solved

Save form after updating default values

Posted on 2013-05-31
7
25 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

708 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

12 Experts available now in Live!

Get 1:1 Help Now