Validation and Dirty in BeforeUpdate of Form

  • Thread starter lad5ko via AccessMonster.com
  • Start date
L

lad5ko via AccessMonster.com

Hallo everyone,

In my database I have a Data/Time field wich I validate by Global modul –
that works fine. After validation user have to determine if he will
save/unsave changes (through dirty property of event BeforeUpdate of Form).

My code :

Private Sub Form_BeforeUpdate(Cancel As Integer)
Call Check_Date ' This works fine

If Dirty Then
If MsgBox("You want save changes?", _
vbYesNo + vbDefaultButton2 + 32, "DB2") = vbNo Then
Cancel = True
Me.Undo
End If
End If
End Sub

1. problem
After validation, when Date is incorrect is opened msgBox with „Date muss be
before June 30. 2006“ and after pressing „OK“ is immediately opened msgBox
with "You want save changes?". This question is a nonsense becouse he have
first correct „Date“ and after that – if is Date correct, by leaving the
record have to be opened question "You want save changes?".
2. problem
If user input correct Date msg "You want save changes?" appears but it is
impossible save the record.

I cann not make validation on field level of table or field level of form
because user can change validation rule in table or jump across the field in
form. It muss be done through event BeforeUpdate of Form (if it is possible).

Is there any way to do this? Thanks.

lad5ko
 
D

Douglas J. Steele

Sounds to me like you should change Check_Date to a function that returns
True if the date's okay, and False if it isn't. Then you can change your
code to:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Check_Date() = True Then
If Dirty Then
If MsgBox("You want save changes?", _
vbYesNo + vbDefaultButton2 + 32, "DB2") = vbNo Then
Cancel = True
Me.Undo
End If
End If
Else
Cancel = True
End If
End Sub
 
L

lad5ko via AccessMonster.com

Hi Douglas,
Thank you for your time.
Probably it is a good idea, exuse me but I do not know how to do that.
My code is :

Function Check_Date()

If (Forms![DHM_form]![Date] > #6/30/2006#) Then
MsgBox "The Date can not be after 6/30/2006. Change it please.", _
vbExclamation, "EM ok-5"
End If
DoCmd.CancelEvent

End Function

Thanks in advance

lad5ko

Sounds to me like you should change Check_Date to a function that returns
True if the date's okay, and False if it isn't. Then you can change your
code to:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Check_Date() = True Then
If Dirty Then
If MsgBox("You want save changes?", _
vbYesNo + vbDefaultButton2 + 32, "DB2") = vbNo Then
Cancel = True
Me.Undo
End If
End If
Else
Cancel = True
End If
End Sub
Hallo everyone,
[quoted text clipped - 37 lines]
 
D

Douglas J. Steele

Function Check_Date() As Boolean

If (Forms![DHM_form]![Date] > #6/30/2006#) Then
MsgBox "The Date can not be after 6/30/2006. Change it please.", _
vbExclamation, "EM ok-5"
Check_Date = False
Else
Check_Date = True
End If

End Function

Up to you whether you want to leave that message box in there, or handle it
in your BeforeUpdate event instead. (I'd do the latter)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


lad5ko via AccessMonster.com said:
Hi Douglas,
Thank you for your time.
Probably it is a good idea, exuse me but I do not know how to do that.
My code is :

Function Check_Date()

If (Forms![DHM_form]![Date] > #6/30/2006#) Then
MsgBox "The Date can not be after 6/30/2006. Change it please.", _
vbExclamation, "EM ok-5"
End If
DoCmd.CancelEvent

End Function

Thanks in advance

lad5ko

Sounds to me like you should change Check_Date to a function that returns
True if the date's okay, and False if it isn't. Then you can change your
code to:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Check_Date() = True Then
If Dirty Then
If MsgBox("You want save changes?", _
vbYesNo + vbDefaultButton2 + 32, "DB2") = vbNo Then
Cancel = True
Me.Undo
End If
End If
Else
Cancel = True
End If
End Sub
Hallo everyone,
[quoted text clipped - 37 lines]
 
L

lad5ko via AccessMonster.com

Hi Douglas,
Thank you again for your time.

It do not work so far.
My original idea was
1. Check the date
2. Save all changes in record (or by entering new record)
But I am a novice in VBA and I do not know how join this two codes in this
order. One is a Function and other is a Sub procedure and they can not be in
one event (may be they can but I do not know how to do that).

lad5ko

Function Check_Date() As Boolean

If (Forms![DHM_form]![Date] > #6/30/2006#) Then
MsgBox "The Date can not be after 6/30/2006. Change it please.", _
vbExclamation, "EM ok-5"
Check_Date = False
Else
Check_Date = True
End If

End Function

Up to you whether you want to leave that message box in there, or handle it
in your BeforeUpdate event instead. (I'd do the latter)
Hi Douglas,
Thank you for your time.
[quoted text clipped - 38 lines]
 
D

Douglas J. Steele

Did you not see how I said to use the function in the BeforeUpdate event in
an earlier post?

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Check_Date() = True Then
If Dirty Then
If MsgBox("You want save changes?", _
vbYesNo + vbDefaultButton2 + 32, "DB2") = vbNo Then
Cancel = True
Me.Undo
End If
End If
Else
Cancel = True
End If
End Sub


--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


lad5ko via AccessMonster.com said:
Hi Douglas,
Thank you again for your time.

It do not work so far.
My original idea was
1. Check the date
2. Save all changes in record (or by entering new record)
But I am a novice in VBA and I do not know how join this two codes in this
order. One is a Function and other is a Sub procedure and they can not be
in
one event (may be they can but I do not know how to do that).

lad5ko

Function Check_Date() As Boolean

If (Forms![DHM_form]![Date] > #6/30/2006#) Then
MsgBox "The Date can not be after 6/30/2006. Change it please.", _
vbExclamation, "EM ok-5"
Check_Date = False
Else
Check_Date = True
End If

End Function

Up to you whether you want to leave that message box in there, or handle
it
in your BeforeUpdate event instead. (I'd do the latter)
Hi Douglas,
Thank you for your time.
[quoted text clipped - 38 lines]
 
L

lad5ko via AccessMonster.com

Hi Douglas,
I copied your code in my BeforeUpdate event of form but it not worked.
Probably I made some mistake. May be it was not good idea to ask for help
because I am too green in VBA.
I found another solution for this problem.
Mea culpa ...

Thank you for your time and effort to help me.

lad5ko
Did you not see how I said to use the function in the BeforeUpdate event in
an earlier post?

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Check_Date() = True Then
If Dirty Then
If MsgBox("You want save changes?", _
vbYesNo + vbDefaultButton2 + 32, "DB2") = vbNo Then
Cancel = True
Me.Undo
End If
End If
Else
Cancel = True
End If
End Sub
Hi Douglas,
Thank you again for your time.
[quoted text clipped - 31 lines]
 

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