Search code examples
formsms-accessms-access-2013microsoft-reporting

Add new record only when all forms are filled out


I'm creating a new database in ms access. I've created all forms already.

How can I set up my forms and buttons so that a new record is added to their respective tables ONLY when the required fields in all forms are filled out?

I have 8 forms for the user to fill out for each"customer". Any help is greatly appreciated.

Thanks


Solution

  • If you are clicking a button to submit the form, you can have an On Click event for that button which will run the following code:

    Private Sub button_Click()
    
    if ([field1].Value = "" or isnull([field1].value)) Then
        MsgBox "Fill out all the required fields before submitting"
        End
    else 
        'Submit record using DoCmd.RunSQL "INSERT INTO....."
    End If
    
    End Sub
    

    Have a chain of if statements to check all the fields that you want to be filled out.

    If you really want to have a chain of forms that open one another and then submit all forms together at the end, try this:

    Using an On Click event for a button in the first form, open the next form, followed by running the macro/code to submit the record. In the next form do the same. Basically this keeps adding to a stack, and when the last form is called and submits the record, it finishes the On Click event and the code returns to the form before. The form before will move to the next line of code which is to submit the record and then that code finishes and the same thing happens. Here is a little pseudo code:

    On form 1:

    Private Sub button_Click()
    
    DoCmd.OpenForm 'enter parameters to open next form here
    'Code to submit record
    'DoCmd.CloseForm 'form name
    
    End Sub
    

    On form 2, form 3, and so on:

    Private Sub button_Click()
    
    DoCmd.OpenForm 'enter parameters to open nextform here
    'Code to submit record
    'DoCmd.CloseForm 'form name
    
    End Sub
    

    Make sure you use the parameter acDialog when using DoCmd.openForm to pause the code. You will have to close the form after in order to jump back to the previous code. I added some more to the code above. Follow this link for more information about pausing the code http://www.pcreview.co.uk/threads/pause-code-while-waiting-for-a-popup-form-to-close.1155074/