Msgbox cancel button

S

stoehrkr

Hello!

I'm fairly new to Access and VB and I've been getting along alright
but I've run into a problem with a message box I set up in my form.

Situation: the field FvsNF will always be filled out. Depending on
the answer (yes or no) certain fields become enabled or disabled. The
problem is if a user decides to change their answer for FvsNF after
filling out the wrong fields. The incorrect fields need to go back to
zero if they decide to proceed with that action. However if they
don't want to proceed I want to give them an out without losing their
entries. The code I have now is:

Private Sub FvsNF_BeforeUpdate(Cancel As Integer)
Result = MsgBox("Warning: All values in section will become zero.",
vbOKCancel)
If vbOK Then
TotalCC.Value = 0
OSCC.Value = 0
Clumpiness.Value = 0
ClumpDensity.Value = 0
ClumpSize.Value = 0
CrownDiff.Value = 0
CanopyLayers.Value = 0
Logging.Value = 0
pcc.Value = 0
DensOS.Value = -99
DensUS.Value = -99
sizeOS.Value = 0
sizeUS.Value = 0
sppOS.Value = 0
sppUS.Value = 0
Nonforesttype.Value = 0
ElevBelt.Value = 0
nonforestsppOS.Value = 0
nonforestOSCC.Value = 0
treecovergstype.Value = 0
Else

End If
End Sub

I've tried a lot of things under Else...and lots of different
combinations but I can't seem to get the cancel button to cancel! Any
help would be greatly appreciated!

Kali
 
R

ruralguy via AccessMonster.com

Me.UnDo will return all of the controls to the values last saved.
 
G

Guy

Kali,

I assume you meant:

If Result = vbOK then
' Do something
Else
' Do something else
End If

Or you can combine it in the If statement such as:

If MsgBox("Warning: All values in section will become zero.", vbOKCancel) =
vbOK then
' Do something
Else
' Do something else
End If

Or if you have more options you can also use the Select Case as in:

Select Case MsgBox("Do you really want to do this?", vbYesNoCancel +
vbQuestion, "Do this Confirmation")
Case vbYes:
' Yes - Do something
Case vbNo:

' No -Do something
Case Else

' Cancel - Do something
End Select

Or

Select Case MsgBox("Do you really want to do this?", vbYesNoCancel +
vbQuestion, "Do this Confirmation")
Case vbYes:
' Yes - Do something
Case vbNo:

' No -Do something
Case Cancel

' Cancel - Do something
Case Else
(Note. this action should never be taken for the above statement, but it
is generally good practice to include an Else for more complex scenarios)
' Else - Do something
End Select

Of course Me.Undo will also work, if returning to your last set of saved
values is your intention.

Hope this helps
Guy
 
S

stoehrkr

Hmmm...
Thank you for the suggestions but I'm still having problems. I think
I need what the "do something" code is for making it cancel because I
can't make my code do anything but return all of the values to zero no
matter which button I choose (OK or Cancel).

Could I be doing something wrong with Me.undo? It doesn't seem to be
doing anything at the moment.

I'll keep playing around with it but if you have any other suggestions
I'm all ears!

Thanks again,
Kali
 
R

ruralguy via AccessMonster.com

Have you single stepped the code to make sure it is executing? Me.UnDo is
useless after you have saved the record. What values are in the table that
the controls should return to. If the controls are *not* bound to fields in
the underlying table/query then Me.UnDo will not work.
 
S

stoehrkr

Yes, I figured out that I need it to return to the values the user has
just entered not the ones that were last saved (It was returning to
zero which was the last saved record. It confused me at the time). I
got it to work by using FvsNF.undo, it keeps the last values but
unfortunately it still switches the focus and enables the wrong set of
options for a user who canceled.
 
S

stoehrkr

Alright- I'm deleting my message box and hoping the user of this form
know's what they're doing...they'll learn quickly if they don't!
Thanks to all for your help!
Kali
 

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