Cancel form from closing if Validation fails (1 Viewer)

speakers_86

Registered User.
Local time
Today, 01:36
Joined
May 17, 2007
Messages
1,919
Another thing that should be simple. I can't stand getting stuck on the easy parts.

Code:
Private Sub TransactionSubAmount_BeforeUpdate(Cancel As Integer)
If Me.Text8 <> 0 Then
If Me.Parent.txtRemaining <> 0 Then
Cancel = True
End If
End If
End Sub

If both if statements are true, then I don't want the form to close. Right now I have the code inside the subform's before update event. I am not sure if that is where it should be. I tried to put it in the parent form's before update event, but that did nothing. It seems like by the time this code fires, txtremaining is already unavailable.
 

vbaInet

AWF VIP
Local time
Today, 06:36
Joined
Jan 22, 2010
Messages
26,374
Remember the Unload event I was talking about in your last thread?

Also it should be:

If this AND that Then
 

speakers_86

Registered User.
Local time
Today, 01:36
Joined
May 17, 2007
Messages
1,919
Here is the winning code:

Code:
Private Sub Form_Unload(Cancel As Integer)
If Me.txtRemaining = 0 Or Me.txtRemaining = Me.txtAmount Then
Else
Cancel = True
End If

End Sub
 

vbaInet

AWF VIP
Local time
Today, 06:36
Joined
Jan 22, 2010
Messages
26,374
Actually you want something like this:
Code:
If Nz(Me.txtRemaining, 0) [COLOR=Red][B]<>[/B][/COLOR] 0 [COLOR=Red][B]And[/B][/COLOR] Nz(Me.txtRemaining, 0) [COLOR=Red][B]<>[/B] [/COLOR]Me.txtAmount Then
    Cancel = True
End If
 

boblarson

Smeghead
Local time
Yesterday, 22:36
Joined
Jan 12, 2001
Messages
32,059
Or perhaps in a single line:

Code:
Cancel = (Nz(Me.txtRemaining, 0) <> 0 And Nz(Me.txtRemaining, 0) <> Me.txtAmount)
:D
 

vbaInet

AWF VIP
Local time
Today, 06:36
Joined
Jan 22, 2010
Messages
26,374
I've just noticed the criteria is different from your first, so here's the amended bit:
Code:
If [COLOR=Red][B]Not[/B][/COLOR] (Nz(Me.txtRemaining, "") = 0 Or Nz(Me.txtRemaining, "") = Nz(Me.txtAmount, "")) Then
    Cancel = True
End If
Notice the negation there using NOT. Or you can do this too:
Code:
If (Nz(Me.txtRemaining, "") = 0 Or Nz(Me.txtRemaining, "") = Nz(Me.txtAmount, "")) [COLOR=Red][B]= False[/B][/COLOR] Then
 

vbaInet

AWF VIP
Local time
Today, 06:36
Joined
Jan 22, 2010
Messages
26,374
Or using Bob's solution you put another Not. So Not ( Not (Criteria))
 

Users who are viewing this thread

Top Bottom