Solved Me.Undo Not working (1 Viewer)

Number11

Member
Local time
Today, 23:59
Joined
Jan 29, 2020
Messages
607
So i have an order form, and have a button that if pressed will cancel the order and close the form, but its not working...

I have

Me.Undo
DoCmd.Close acForm, Me.Name

but if say i restarted to enter the order details (and a new record created), when press the "Cancel" Button the form closes but the record is still showing with just the field that was completed?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:59
Joined
Oct 29, 2018
Messages
21,358
Is it a bound or unbound control?
 

Number11

Member
Local time
Today, 23:59
Joined
Jan 29, 2020
Messages
607
Is it a bound or unbound control?
so i forgot to add that the form has a second button - which is called "saved" one its clicked the record is saved but then asks the user to complete the missing fields, so then pressing Cancel does not delete the record so i get a need to add if got a record ID then delete and if no ID undo?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:59
Joined
Oct 29, 2018
Messages
21,358
so i forgot to add that the form has a second button - which is called "saved" one its clicked the record is saved but then asks the user to complete the missing fields, so then pressing Cancel does not delete the record so i get a need to add if got a record ID then delete and if no ID undo?
Same question... Are we talking about a bound or unbound form/control?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:59
Joined
May 21, 2018
Messages
8,463
one its clicked the record is saved but then asks the user to complete the missing fields
You should not save a record and then validate the missing fields. Does not make sense. In the forms before update event you validate the data and cancel the event if not valid.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:59
Joined
May 21, 2018
Messages
8,463
Here is some code for validating required fields. It gives a message and colors the fields that must be filled in.
 

Number11

Member
Local time
Today, 23:59
Joined
Jan 29, 2020
Messages
607
Here is some code for validating required fields. It gives a message and colors the fields that must be filled in.
Thanks so i used your code and placed this on the Forms "Before Update", that all works fine
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:59
Joined
May 21, 2018
Messages
8,463
Code:
Thanks so i used your code
As the post points out that code is not mine. That came from @arnelgp and @oxicottin. It is simple to implement and works well.
 

Number11

Member
Local time
Today, 23:59
Joined
Jan 29, 2020
Messages
607
is there a way to change the order that the required fields message box shows them as its not as the form is layed out?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:59
Joined
May 21, 2018
Messages
8,463
You could, but would not be real easy. If important to you I could probably write it.
Here is the problem. When you loop the controls on a form they are in an order known as z-order. This has to do with when they were added to the form and other position factors. You cannot loop by other order such as tab order and you cannot alter or control the z-order.

What you could do is create a custom collection. When the form loads add the controls to the custom collection in your desired order. Then pass this collection to the validate function. Now you can loop the controls in the custom collection instead of the form's control collection.
 

Number11

Member
Local time
Today, 23:59
Joined
Jan 29, 2020
Messages
607
You could, but would not be real easy. If important to you I could probably write it.
Here is the problem. When you loop the controls on a form they are in an order known as z-order. This has to do with when they were added to the form and other position factors. You cannot loop by other order such as tab order and you cannot alter or control the z-order.

What you could do is create a custom collection. When the form loads add the controls to the custom collection in your desired order. Then pass this collection to the validate function. Now you can loop the controls in the custom collection instead of the form's control collection.
Oh wow that does sound difficult to create i guess :) any help apricated as i got the customer name after the address lines 1 and 2 and then the town is before the address and the post code before the address it just looks messy
 

Isaac

Lifelong Learner
Local time
Today, 16:59
Joined
Mar 14, 2017
Messages
8,738

apricate​

(ˈæprɪˌkeɪt)
vb
1. (intr) to sunbathe or bask in the sun
2. (tr) to expose to sunlight

Thanks ... I learned a brand new word today !
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:59
Joined
May 21, 2018
Messages
8,463
To demo. I named the new function ValidData2. In this version you need to first create a collection of the controls to validate and then pass them in. I do this with a variable at the top of the form module and then load the collection in the desired order in the forms load event. For demo purposes, I did it backwards to make it obvious.

Code:
Option Compare Database
Option Explicit
'add a ctls collection
Private ctls As New Collection

Private Sub cmdClose_Click()
  Dim rtn As Long
  If DataValid2(Me) Then
    DoCmd.Close acForm, Me.Name
  Else
    rtn = MsgBox(" Select OK to Close without saving, or CANCEL to complete form.", vbOKCancel, "Validate Data")
      If rtn = vbOK Then
        DoCmd.Close acForm, Me.Name
      End If
  End If
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
  If Not DataValid2(Me, ctls) Then
    Cancel = True
  End If
End Sub

Private Sub Form_Load()
  'Add controls in order
  ctls.Add Me.Job_Title
  ctls.Add Me.Company
  ctls.Add Me.First_Name
  ctls.Add Me.Last_Name
End Sub

In this example I modified the function that resets the borders. Not sure the reason for the original design, but as soon as you re-enter one of the marked controls all the red borders are reset. I modified this to the after update of the specific field.
 

Attachments

  • ValidateData_2.accdb
    1.8 MB · Views: 167

Number11

Member
Local time
Today, 23:59
Joined
Jan 29, 2020
Messages
607
To demo. I named the new function ValidData2. In this version you need to first create a collection of the controls to validate and then pass them in. I do this with a variable at the top of the form module and then load the collection in the desired order in the forms load event. For demo purposes, I did it backwards to make it obvious.

Code:
Option Compare Database
Option Explicit
'add a ctls collection
Private ctls As New Collection

Private Sub cmdClose_Click()
  Dim rtn As Long
  If DataValid2(Me) Then
    DoCmd.Close acForm, Me.Name
  Else
    rtn = MsgBox(" Select OK to Close without saving, or CANCEL to complete form.", vbOKCancel, "Validate Data")
      If rtn = vbOK Then
        DoCmd.Close acForm, Me.Name
      End If
  End If
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
  If Not DataValid2(Me, ctls) Then
    Cancel = True
  End If
End Sub

Private Sub Form_Load()
  'Add controls in order
  ctls.Add Me.Job_Title
  ctls.Add Me.Company
  ctls.Add Me.First_Name
  ctls.Add Me.Last_Name
End Sub

In this example I modified the function that resets the borders. Not sure the reason for the original design, but as soon as you re-enter one of the marked controls all the red borders are reset. I modified this to the after update of the specific field.
Ok thanks I have copied over this new code and module but i am getting error message Compile error: Argument not optional onthis code..


Private Sub cmdClose_Click()
Dim rtn As Long
If DataValid2(Me) Then
DoCmd.Close acForm, Me.Name
Else
rtn = MsgBox(" Select OK to Close without saving, or CANCEL to complete form.", vbOKCancel, "Validate Data")
If rtn = vbOK Then
DoCmd.Close acForm, Me.Name
End If
End If
End Sub
 

Number11

Member
Local time
Today, 23:59
Joined
Jan 29, 2020
Messages
607
Ok thanks I have copied over this new code and module but i am getting error message Compile error: Argument not optional onthis code..


Private Sub cmdClose_Click()
Dim rtn As Long
If DataValid2(Me) Then
DoCmd.Close acForm, Me.Name
Else
rtn = MsgBox(" Select OK to Close without saving, or CANCEL to complete form.", vbOKCancel, "Validate Data")
If rtn = vbOK Then
DoCmd.Close acForm, Me.Name
End If
End If
End Sub
Ok sorted it i just needed to change DataValid2(Me) to DataValid2(Me, ctls) :0 Thanks
 

Users who are viewing this thread

Top Bottom