Command button to validate data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eddie Malone
    New Member
    • Feb 2007
    • 5

    #1

    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:

    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Command20_Click()
    On Error GoTo Err_Command20_Click
    
    
        DoCmd.GoToRecord , , acNewRec
    
    Exit_Command20_Click:
        Exit Sub
    
    Err_Command20_Click:
        MsgBox Err.Description
        Resume Exit_Command20_Click
        
    End Sub
    I'm looking to add the following code in some manner:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
        If Me.[Cartons]-Me.[CartonCountSubtotal] <> 0 Then
            MsgBox "Check your carton count!"
            Cancel = True
        End If
    
    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 - CartonCountSubt otal = 0. Any thoughts?

    Thank you in advance. This is driving me nuts.

    Eddie.
    Last edited by NeoPa; Feb 8 '07, 07:41 PM. Reason: Tags
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #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.[CartonCountSubt otal] <> 0 Then
    MsgBox "Check your carton count!"
    else
    docmd.close
    End If


    Nic;o)

    Comment

    • Eddie Malone
      New Member
      • Feb 2007
      • 5

      #3
      Originally posted by nico5038
      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.[CartonCountSubt otal] <> 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.

      Comment

      • Eddie Malone
        New Member
        • Feb 2007
        • 5

        #4
        Originally posted by Eddie Malone
        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.

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          It wil be two separate selfplaced buttons.
          I use them always as it's the general way Windows forms work :-)

          Nic;o)

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            Are the controls you're referring to, [Cartons] and [CartonCountSubt otal] both on the same form, or is one on the form and one on the subform?

            Comment

            • Eddie Malone
              New Member
              • Feb 2007
              • 5

              #7
              Originally posted by missinglinq
              Are the controls you're referring to, [Cartons] and [CartonCountSubt otal] 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 CartonCountSubt otal is in the in subform.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                Look in Referring to Items on a Sub-Form for ways to access CartonCountSubt otal on the subform.
                The Me! version should work best for you.

                Comment

                Working...