Cancel form from closing if Validation fails

speakers_86

Registered User.
Local time
Today, 03:06
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.
 
Remember the Unload event I was talking about in your last thread?

Also it should be:

If this AND that Then
 
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
 
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
 
Or perhaps in a single line:

Code:
Cancel = (Nz(Me.txtRemaining, 0) <> 0 And Nz(Me.txtRemaining, 0) <> Me.txtAmount)
:D
 
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
 
Or using Bob's solution you put another Not. So Not ( Not (Criteria))
 

Users who are viewing this thread

Back
Top Bottom