Understanding Cancel

A

AlCamp

I need to better understand how the Cancel method works.
I have the following code that works well... (thanks Jonathan Parminter)
When the user enters a SpoolQty amount that causes the NetWeight calculation
to go Negative (< Zero) the user is msgbox warned, the illigitimate value
they entered reverts back to the original value, and the cursor stays in the
SpoolQty field to allow a legitimate value to be entered. (Several fields
use this same Function)

Field [SpoolQty] events...
BeforeUpdate [EventProcedure]
AfterUpdate =CheckNetWeight([SpoolQty])

Private Sub SpoolQty_BeforeUpdate(Cancel As Integer)
Cancel = CheckNetWeight([SpoolQty])
End Sub

Public Function CheckNetWeight(CallingCtl As Control) As Boolean
Dim fCancel As Boolean
If NetWeight <= 0 Then
' MsgBox stuff here.....
Response = MsgBox(Prompt, vbOKOnly + vbCritical, Title)
If Response = vbOK Then
fCancel = True
CallingCtl.Undo
End If
End If
CheckNetWeight = fCancel
End Function

Could someone tell me how this works? I just need a brief "synopsis",
such as... what the BeforeUpdate code means, how a BeforeUpdate event can
Cancel a function run by the AfterUpdate event, and what designating a
function "as Boolean" means.
Also, is this the best method/events to be using?

Any help would be appreciated... Can't relate this code to what I've
learned about Cancel in Help. I'm missing a concept here, and looking for
an "epiphany."

Thanks in advance,
Al Camp
 
R

Rick Brandt

AlCamp said:
I need to better understand how the Cancel method works.
I have the following code that works well... (thanks Jonathan Parminter)
When the user enters a SpoolQty amount that causes the NetWeight calculation
to go Negative (< Zero) the user is msgbox warned, the illigitimate value
they entered reverts back to the original value, and the cursor stays in the
SpoolQty field to allow a legitimate value to be entered. (Several fields
use this same Function)

Field [SpoolQty] events...
BeforeUpdate [EventProcedure]
AfterUpdate =CheckNetWeight([SpoolQty])

Private Sub SpoolQty_BeforeUpdate(Cancel As Integer)
Cancel = CheckNetWeight([SpoolQty])
End Sub

Public Function CheckNetWeight(CallingCtl As Control) As Boolean
Dim fCancel As Boolean
If NetWeight <= 0 Then
' MsgBox stuff here.....
Response = MsgBox(Prompt, vbOKOnly + vbCritical, Title)
If Response = vbOK Then
fCancel = True
CallingCtl.Undo
End If
End If
CheckNetWeight = fCancel
End Function

Could someone tell me how this works? I just need a brief "synopsis",
such as... what the BeforeUpdate code means, how a BeforeUpdate event can
Cancel a function run by the AfterUpdate event, and what designating a
function "as Boolean" means.

The BeforeUpdate is calling that function on its own. AfterUpdate has
nothing to do with it.

The function returns a Boolean (either True or False). When it returns
true in the context of the BeforeUpdate call then the BeforeUpdate event is
cancelled which means AfterUpdate does not happen.
 
A

AlCamp

Thanks Rick,
I see what you're saying.So the BeforeUpdate evaluates the Function as True or False, and allows
it to perform it's function or not.

I think I was equating SpoolQty BeforeUpdate as "before there's any value
for SpoolQty"... and that was why I couldn't grasp the concept of the code.
So...once a value is entered in SpoolQty, BeforeUpdate can examine that
value before it's really updated (Posted) to the field. That's why it's
Cancellable and AfterUpdate isn't.

Also, the code I posted....
BeforeUpdate [EventProcedure]
AfterUpdate =CheckNetWeight([SpoolQty])
I don't really need the AfterUpdate =CheckNetWeight([SpoolQty])... do I??
I'll try that ASAP.

Thanks Rick, your response did the trick...
Al Camp

Rick Brandt said:
AlCamp said:
I need to better understand how the Cancel method works.
I have the following code that works well... (thanks Jonathan Parminter)
When the user enters a SpoolQty amount that causes the NetWeight calculation
to go Negative (< Zero) the user is msgbox warned, the illigitimate value
they entered reverts back to the original value, and the cursor stays in the
SpoolQty field to allow a legitimate value to be entered. (Several fields
use this same Function)

Field [SpoolQty] events...
BeforeUpdate [EventProcedure]
AfterUpdate =CheckNetWeight([SpoolQty])

Private Sub SpoolQty_BeforeUpdate(Cancel As Integer)
Cancel = CheckNetWeight([SpoolQty])
End Sub

Public Function CheckNetWeight(CallingCtl As Control) As Boolean
Dim fCancel As Boolean
If NetWeight <= 0 Then
' MsgBox stuff here.....
Response = MsgBox(Prompt, vbOKOnly + vbCritical, Title)
If Response = vbOK Then
fCancel = True
CallingCtl.Undo
End If
End If
CheckNetWeight = fCancel
End Function

Could someone tell me how this works? I just need a brief "synopsis",
such as... what the BeforeUpdate code means, how a BeforeUpdate event can
Cancel a function run by the AfterUpdate event, and what designating a
function "as Boolean" means.

The BeforeUpdate is calling that function on its own. AfterUpdate has
nothing to do with it.

The function returns a Boolean (either True or False). When it returns
true in the context of the BeforeUpdate call then the BeforeUpdate event
is
cancelled which means AfterUpdate does not happen.
 

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