Link to home
Create AccountLog in
Avatar of billcute
billcute

asked on

Question on Command button "Add" new record.

I have a small snag on the command click code to add new record.

If a user enters data, clicks AddNew button to save the data and advance to a new record screen; and the code for mandatory fields is processed and all the form fields have data on them as expected; the form should save and then present a new blank form for user to enter another record.
In my current code, current record is not added to the table and the screen does not advanced to a blank record screen.

My current Mandatoryfields function works. However, In order to avoid pasting too many code on this post, the MandatoryFields Function can be found at the link below:
https://www.experts-exchange.com/questions/22861455/Tag-property-question.html
' *************
Here is my current code for the command button:

Private Sub btnAddNew_Click()
On Error GoTo Err_btnAddNew_Click
    If Not MandatoryFields Then     ' <---Check for Mandatory Fields
      Exit Sub
    End If
'    Call SaveCurrentControlValues   ' For Autofill Routine - this works fine
' ****************************************************************
    If Me.AllowAdditions = False Then Me.AllowAdditions = True
    Me.Filter = ""
    Me.FilterOn = False
    DoCmd.GoToRecord , "frmMain", acNewRec
  Exit_btnAddNew_Click:
     Exit Sub
Err_btnAddNew_Click:
     MsgBox err.Description
   Resume Exit_btnAddNew_Click
End Sub
Avatar of slamhound
slamhound

The best method to use would be to set the Required setting of the fields in question to "Yes". This way Access will take care of all the issues of the manditory fields. This means it won't save the record if this field is empy and will make the user choose to either enter the right information or quit the page without saving.

Once you've made this change and removed your MandatoryFields code, does the rest of the procedure work now?
What happens when you use this code? What needs fixing / changing?

    Private Sub btnAddNew_Click()
    On Error GoTo Err_btnAddNew_Click
       
        If Not MandatoryFields Then
            ' MandatoryFields sub has hilighted missing mandatory data
            ' Exit sub and let them correct
            Exit Sub
        End If
        ' Data is ok, save it
        DoCmd.RunCommand acCmdSaveRecord
       
        If Me.AllowAdditions = False Then Me.AllowAdditions = True
        Me.Filter = ""
        Me.FilterOn = False
        DoCmd.GoToRecord , "frmMain", acNewRec
       
    Exit_btnAddNew_Click:
        Exit Sub
    Err_btnAddNew_Click:
        MsgBox err.Description
        Resume Exit_btnAddNew_Click
    End Sub
Avatar of billcute

ASKER

Angelplay:
You re-posted this same code in my question.
      Problems:
      When user add data on the form and the mandatory fields are not completed;
      and the add button is clicked:

        ....... all missing fields flagged as expected and save any incomplete or partially added data in the bounded table. The exit sub seems not working as well.

 Note: Records are not expected to be saved in the table until all mandatory fields are completed.

Regards
Bill
ASKER CERTIFIED SOLUTION
Avatar of Angelp1ay
Angelp1ay
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Angelplay:
I have just tested your last posted code and here are the results.
(a).
With MandatoryFields = True removed from my main function, I addd just one data into my main form then
clicked the btnAddNew, I received this msgbox.

Mandatory Fields not conplete - Exit Sub.
Note I checked the bounded table, the single record I added was saved in the bounded table.

(b).
With MandatoryFields = True  - "NOT" removed from my main function,
                                                   I addd just one data into my main form then
                                                  clicked the btnAddNew, I received this msgbox.

Data Ok, Save it.
Note:
Again, I checked the bounded table, the single record I added was saved in the bounded table.

Note: I just tried editing my mandatoryfields function just to see any effect on the btnAddNew - but it did not make a difference with what was happening at btnAddNew.

Regards
Bill
Again, it appears the if statement is working fine. There must be something else in your code causing a save action. I'll have a look at the code again and get back to you :o)
Angelp1ay:
Your code works fine. I manipulated my function a little.
Regards
Bill