[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Question on Command button "Add" new record.

Posted on 2007-10-02
7
Medium Priority
?
1,987 Views
Last Modified: 2013-11-28
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:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_22861455.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
0
Comment
Question by:billcute
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:slamhound
ID: 20003255
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?
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 20004719
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
0
 
LVL 4

Author Comment

by:billcute
ID: 20010705
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Accepted Solution

by:
Angelp1ay earned 2000 total points
ID: 20012524
<< You re-posted this same code in my question. >>
Well pretty much yes - I swapped the save function for a plain bit of save code. At least I know we're in the same place now though :o)

<< ....... 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. >>
This is a little worrying. Please try the code below, repeat your previous action i.e. submit with fields empty and please post which msgboxes you see. It looks like this may be the same issue as with the other cmd button.

    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
            Msgbox ("Mandatory fields not complete - EXIT SUB")
            Exit Sub
        End If
        Msgbox ("Data is ok, save it")
        ' 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
0
 
LVL 4

Author Comment

by:billcute
ID: 20012779
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
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 20013230
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)
0
 
LVL 4

Author Comment

by:billcute
ID: 20044601
Angelp1ay:
Your code works fine. I manipulated my function a little.
Regards
Bill
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question