Undoing Record

D

DS

I have a form based on a table, the form is data entry only. I have
this code on a button. If the user enters nothing into the Name field
and decides to cancel I want to close the form. But if the user enters
something into the Name field and decides to cancel I want to undo the
record and close the form. The first part IsNull(Me.Name) doesn't seem
to work, I keep getting the error message Undo unavailable, which leds
me to believe that the first part is not working any help appreciated.
Thanks
DS



If IsNull(Me.Name) Then
DoCmd.Close acForm, "PartyPlannerAdd"
Else:
DoCmd.RunCommand acCmdUndo
Forms!PartyPlanner!ListParty.Requery
DoCmd.Close acForm, "PartyPlannerAdd"
End If
 
A

Allen Browne

You have confused Access by using a field named Name. The form itself has a
Name property, to Me.Name returns the name of the form. Since the form's
name is never null, the If never executes, i.e. the Else will always
execute.

Firstly, uncheck the boxes under:
Tools | Options | General | Name AutoCorrect
Explanation of why:
http://allenbrowne.com/bug-03.html
Then compact the database to get rid of this junk:
Tools | Database Utilities | Compact

Now rename the field in your table to CustomerName or some such.
Then open the form in design view.
Right-click the text box and choose Properties
Change both the Control Source (Data tab) and the Name (Other tab) to
CustomerName.

A user with a mouse can enter any field they like, so the form could be
dirty even if a name had not been entered, and if so it will need undoing.
Try something like this:

Dim bHadAName As Boolean
bHadAName = Not IsNull(Me.CustomerName)
If Me.Dirty Then
Me.Undo
End If
If Not bHadAName Then
Forms!PartyPlanner!ListParty.Requery
End If
DoCmd.Close acForm, Me.Name
 
D

DS

Allen said:
You have confused Access by using a field named Name. The form itself has a
Name property, to Me.Name returns the name of the form. Since the form's
name is never null, the If never executes, i.e. the Else will always
execute.

Firstly, uncheck the boxes under:
Tools | Options | General | Name AutoCorrect
Explanation of why:
http://allenbrowne.com/bug-03.html
Then compact the database to get rid of this junk:
Tools | Database Utilities | Compact

Now rename the field in your table to CustomerName or some such.
Then open the form in design view.
Right-click the text box and choose Properties
Change both the Control Source (Data tab) and the Name (Other tab) to
CustomerName.

A user with a mouse can enter any field they like, so the form could be
dirty even if a name had not been entered, and if so it will need undoing.
Try something like this:

Dim bHadAName As Boolean
bHadAName = Not IsNull(Me.CustomerName)
If Me.Dirty Then
Me.Undo
End If
If Not bHadAName Then
Forms!PartyPlanner!ListParty.Requery
End If
DoCmd.Close acForm, Me.Name
Great! Thanks You. That was a really good detailed explanation!
I appreciate it!
DS
 

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