469,283 Members | 2,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Command button to validate data

Hi,

I'm new to access. I have built a database and a form (along with a subform). Before the command button updates I want it to run a validation. The current VBA code for the button is:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Command20_Click()
  5. On Error GoTo Err_Command20_Click
  6.  
  7.  
  8.     DoCmd.GoToRecord , , acNewRec
  9.  
  10. Exit_Command20_Click:
  11.     Exit Sub
  12.  
  13. Err_Command20_Click:
  14.     MsgBox Err.Description
  15.     Resume Exit_Command20_Click
  16.  
  17. End Sub
I'm looking to add the following code in some manner:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  
  3.     If Me.[Cartons]-Me.[CartonCountSubtotal] <> 0 Then
  4.         MsgBox "Check your carton count!"
  5.         Cancel = True
  6.     End If
  7.  
  8. End Sub
I'm trying all kinds of different combinations and it just does not want to work. I only want the form to update if Cartons - CartonCountSubtotal = 0. Any thoughts?

Thank you in advance. This is driving me nuts.

Eddie.
Feb 7 '07 #1
7 3955
nico5038
3,080 Expert 2GB
Better to use a [Cancel] and [Save] button.
In the [Cancel] button code:

If Me.Dirty then
me.undo
endif
docmd.close

In the [Save button code:

If Me.[Cartons]-Me.[CartonCountSubtotal] <> 0 Then
MsgBox "Check your carton count!"
else
docmd.close
End If


Nic;o)
Feb 7 '07 #2
Better to use a [Cancel] and [Save] button.
In the [Cancel] button code:

If Me.Dirty then
me.undo
endif
docmd.close

In the [Save button code:

If Me.[Cartons]-Me.[CartonCountSubtotal] <> 0 Then
MsgBox "Check your carton count!"
else
docmd.close
End If


Nic;o)

Thank you so much for you quick reply. I'll kick it around and see if I can get it to work.

Eddie.
Feb 7 '07 #3
Thank you so much for you quick reply. I'll kick it around and see if I can get it to work.

Eddie.

Stupid question no doubt but is the Canel and Save button the same button? Or are there two? I can't find the save function in teh command button.

Eddie.
Feb 7 '07 #4
nico5038
3,080 Expert 2GB
It wil be two separate selfplaced buttons.
I use them always as it's the general way Windows forms work :-)

Nic;o)
Feb 7 '07 #5
missinglinq
3,532 Expert 2GB
Are the controls you're referring to, [Cartons] and [CartonCountSubtotal] both on the same form, or is one on the form and one on the subform?
Feb 8 '07 #6
Are the controls you're referring to, [Cartons] and [CartonCountSubtotal] both on the same form, or is one on the form and one on the subform?

One is in a form and the other in subform. Cartons is on the form and CartonCountSubtotal is in the in subform.
Feb 8 '07 #7
NeoPa
32,173 Expert Mod 16PB
Look in Referring to Items on a Sub-Form for ways to access CartonCountSubtotal on the subform.
The Me! version should work best for you.
Feb 9 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Matt | last post: by
2 posts views Thread by Humberto Alvarez | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.