PROS / CONS -- Me.Dirty

K

kealaz

Hello,

I have this bit of code, and I'm not entirely sure of what it means, but was
told it was a good idea to use and have been using it.

' Force this record to be saved, if it hasn't been already.
If Me.Dirty Then Me.Dirty = False

When is it a good idea to use this, and when is it not?

I am using some code to validate my form fields, and I'm not sure if I'm
running into some conflict due to the code above.

For example, if I have this code in my Before Update......

If Len(Me.NameOfControl1.Value & "") = 0 Then
MsgBox "You must fill in data for 'name of control or data'!", _
vbOK, "Missing Data"
Cancel = True
Me.NameofControl1.SetFocus

It's saying don't save if the information is not entered. And then "If
Me.Dirty Then Me.Dirty = False" is saying to save.

Are these in conflict? OR am I missing something? If they are, what is a
good way to resolve this conflict?

Thanks for any and all help!!!
 
D

Dirk Goldgar

kealaz said:
Hello,

I have this bit of code, and I'm not entirely sure of what it means, but
was
told it was a good idea to use and have been using it.

' Force this record to be saved, if it hasn't been already.
If Me.Dirty Then Me.Dirty = False

When is it a good idea to use this, and when is it not?

I am using some code to validate my form fields, and I'm not sure if I'm
running into some conflict due to the code above.

For example, if I have this code in my Before Update......

If Len(Me.NameOfControl1.Value & "") = 0 Then
MsgBox "You must fill in data for 'name of control or data'!", _
vbOK, "Missing Data"
Cancel = True
Me.NameofControl1.SetFocus

It's saying don't save if the information is not entered. And then "If
Me.Dirty Then Me.Dirty = False" is saying to save.

Are these in conflict? OR am I missing something? If they are, what is a
good way to resolve this conflict?

Thanks for any and all help!!!


Where do you have the "Me.Dirty = False" line? It doesn't make any sense to
have that in the form's BeforeUpdate event, because that event would only
fire if the record was already in the process of being saved. Why force a
save when it's already being saved? And if you put that line in the same
conditional block where you're cancelling the BeforeUpdate event, then
you're definitely going to have a conflict.
 
K

kealaz

I have the following in BeforeUpdate.

If Len(Me.PART_NO.Value & "") = 0 Then
MsgBox "You must fill in data for 'PART_NO'!", _
vbOK, "Missing Data"
Cancel = True
Me.PART_NO.SetFocus

Then I also have two buttons, one is
Save and Close

' Force this record to be saved, if it hasn't been already.
If Me.Dirty Then Me.Dirty = False

' Close this form.
DoCmd.Close acForm, Me.NAME, acSaveNo

And the other is
Close Form w/o Saving

Me.UNDO
DoCmd.Close

Exit_Close_Click:
Exit Sub

Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click



I wanted these buttons in my form, because I wanted a way to close my form
if I had opened and entered information in a form and did not want to save it.

Is there a better way? (I'm sure there is.)

Thanks for your help!!!
 
D

Dirk Goldgar

kealaz said:
I have the following in BeforeUpdate.

If Len(Me.PART_NO.Value & "") = 0 Then
MsgBox "You must fill in data for 'PART_NO'!", _
vbOK, "Missing Data"
Cancel = True
Me.PART_NO.SetFocus

Then I also have two buttons, one is
Save and Close

' Force this record to be saved, if it hasn't been already.
If Me.Dirty Then Me.Dirty = False

' Close this form.
DoCmd.Close acForm, Me.NAME, acSaveNo

And the other is
Close Form w/o Saving

Me.UNDO
DoCmd.Close

Exit_Close_Click:
Exit Sub

Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click



I wanted these buttons in my form, because I wanted a way to close my form
if I had opened and entered information in a form and did not want to save
it.

Is there a better way? (I'm sure there is.)

Thanks for your help!!!


Now that you explain that the code snippets are in separate events, I don't
see anything wrong with it. The code you've posted, though incomplete,
seems appropriate for the events you have it in.

Note, though, that the code in the "save and close" button will -- quite
rightly -- raise an error if the current record cannot be saved. You'll
probably want to trap that error, to avoid confusing the user.

By the way, why are NAME and UNDO capitalized in your code? If the VBA
editor insists on doing that, it suggests that you have objects or variables
named "NAME" and "UNDO". Those would be bad names for you to use for
anything.
 
K

kealaz

Hi Dirk,

Thanks for the help and the info on naming objects.

What does it mean to "trap the error"? How do I do that, and what do I do
with it?

Thank!
 
D

Dirk Goldgar

kealaz said:
Hi Dirk,

Thanks for the help and the info on naming objects.

What does it mean to "trap the error"? How do I do that, and what do I do
with it?


For a more complete explanation than I could possibly give, I suggest you
open the VB Editor environment from your Access database, search the VB help
for "Error Handling", and read the first few help topics that are returned.
Any VBA procedure can set up a block of code for error-handling, such that
when an error is raised within that procedure, the error-handling code
receives control and can display a message or take whatever steps you wish
to handle the error.

Also, a form has an Error event that is raised when normal data operations
on the form, not VBA code, result in an error. You can write an event
procedure for the Error event, and so deal with those kinds of errors
yourself rather than relying on Access's default error messages.

Probably what you should do, to get a handle on this whole problem, is to
test your form, with its current code, in various scenarios that you can
imagine might cause errors -- for example, start creating a record but don't
fill in all the required information, and then close the form. Once you see
what happens in various trying scenarios, you'll know better what
error-handling you might want to set up (if any) to give a different user
experience.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top