Cancel command button

J

James

I have a command button that refreshes its form to complete any pending
updates then goes on to perform some code.

Private Sub cmdButton_Click()
Form.Refresh
...
End Sub

Private Sub Form_BeforeUpdate()
If Record Not Valid Then
Cancel = True
End If
End Sub

This cancels the update but the code for the command button goes on to fire.
Is there a way of cancelling the button click too?

Thanks
 
D

Douglas J Steele

You can have a global variable that gets set when the update is cancelled,
and check for that variable in your cmdButton_Click code.
 
6

'69 Camaro

Hi, James.

No. Once the button is clicked, it's been clicked. However, if you have
certain code that you don't want to execute if the record isn't saved, then
you can create a module level Boolean flag to check for whether the record
save was successful or not. Try:

Private m_fCancel As Boolean ' Put this in the Declarations section.

Private Sub cmdButton_Click()
Form.Refresh
If (Not (m_fCancel)) Then
' Execute code here.
Else
m_fCancel = False ' Reset.
End If
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Record Not Valid Then ' Psuedocode.
Cancel = True
m_fCancel = True
End If
End Sub

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
J

James

Thanks guys

Douglas J Steele said:
You can have a global variable that gets set when the update is cancelled,
and check for that variable in your cmdButton_Click code.
 

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