Results 1 to 3 of 3
  1. #1
    Nadine67 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    55

    BeforeUpdate Prompt if Field is Empty

    Hello and thank you for any attention this post may receive



    Windows 8.1
    MS Access 2013

    There are mandatory fields on my form which must be filled in prior to saving. I have code which prompts the user upon saving when any of the mandatory fields is empty.

    On the form is two buttons 'Save and New' and 'Save and Exit'. If I chose 'Save and New' then the prompts will appear each time I try to save the record with missing fields. it will prompt until all fields are filled in. This is exactly what I want.

    However if I choose 'Save and Exit' it prompts for the first empty mandatory field and so the user selects 'OK' to acknowledge the prompt (as the code asks for this ), and then instead of going back to the form a warning pops up 'You can't save this record at this time' and asks 'do you want to close the database objet anyway?'. 'Yes' closes the form and the record is lost (the ID does not exist in the table) and the auto-numbering ID will be missing that record. Whereas 'No' results in a 'Macro Single Step' window pops up with the only option of 'Stop All Macros'. Stopping all macros takes me back to the form and allows me to fill the empty fields.

    My code is this

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        'Go through each 'required' field and prompt if empty
        If Me.[Title] & "" = "" Then
            MsgBox "A 'Problem Title' is required, please complete this field.", vbOKOnly
            Me.[Title].SetFocus
            Cancel = True
            Exit Sub
        End If
        If Me.[txtProblemStatement] & "" = "" Then
            MsgBox "A 'Problem Statement' is required, please complete this field.", vbOKOnly
            Me.[txtProblemStatement].SetFocus
            Cancel = True
            Exit Sub
        End If
        If Me.[txtObjective] & "" = "" Then
            MsgBox "A 'Problem Objective' is required, please complete this field.", vbOKOnly
            Me.[txtObjective].SetFocus
            Cancel = True
            Exit Sub
        End If
    End Sub

    The macro behind 'Save and Exit' is the basic 'Close Window' macro with save as 'Yes'. The macro behind 'Save and New' is as per the attached image.

    Click image for larger version. 

Name:	Save and New macro.png 
Views:	13 
Size:	9.3 KB 
ID:	22658


    I am not sure how to solve this issue. Does anyone have any suggestions or has anyone come across this before?

    Thank you!

    Nadine

  2. #2
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    the Go To Record step is not within the If/Then container

    you might consider changing to a new command button and use your vba code instead - - - not that there is anything wrong with macros - - - but if your comfort zone is more vba oriented you might as well.....

  3. #3
    Nadine67 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    55
    Thank you NTC

    I first tried this code - it prompts me for the first empty required field, yet when I answer ok the form closes and I don't have the opportunity to enter anything in the empty required field, nothing is saved and the ID number is lost.

    Code:
    DoCmd.Close acForm, Me.Name
    I then tried this code with the same results.

    Code:
    DoCmd.RunCommand acCmdSaveRecord
    So then I tried this and it works.

    Code:
    Private Sub SaveExit_Click()
    On Error GoTo Err_cmdCloseForm_Click
     
     DoCmd.RunCommand acCmdSaveRecord
     DoCmd.Close
     
    Exit_cmdCloseForm_Click:
     Exit Sub
     
    Err_cmdCloseForm_Click:
     MsgBox Err.Description
     Resume Exit_cmdCloseForm_Click
     
    End Sub
    Thank you for your suggestion NTC, it was very much appreciated. Have a great day!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 10-22-2014, 02:11 PM
  2. Replies: 2
    Last Post: 08-28-2014, 11:47 AM
  3. Replies: 9
    Last Post: 12-08-2013, 07:04 PM
  4. Prompt for email address on field
    By burrina in forum Forms
    Replies: 9
    Last Post: 01-20-2013, 06:55 PM
  5. Run Report with Prompt for Field Criteria
    By diane802 in forum Reports
    Replies: 4
    Last Post: 01-15-2010, 02:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums