Results 1 to 9 of 9
  1. #1
    vickan240sx is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    34

    Simplifying this code

    Hi,

    I'm new to VBA and would like to know if there's a more simple way to write out the code below:

    Code:
     
    Private Sub Save_and_New_Click()
    If IsNull(Me.[BorrowerName]) Or IsNull(Me.[NewRenewalExtensionModification]) Or IsNull(Me.[LDDLogFileCompleteOnArrival]) Or IsNull(Me.[BranchNumber]) Or IsNull(Me.[IncreaseOrDecrease]) Or IsNull(Me.[Amount]) Or IsNull(Me.[LoanOfficer]) Or IsNull(Me.[LDDLogDateTimeCompleted]) Then
    MsgBox ("Borrower Information Missing")
    End If
    If Not IsNull(Me.[BorrowerName]) And Not IsNull(Me.[NewRenewalExtensionModification]) And Not IsNull(Me.[LDDLogFileCompleteOnArrival]) And Not IsNull(Me.[BranchNumber]) And Not IsNull(Me.[IncreaseOrDecrease]) And Not IsNull(Me.[Amount]) And Not IsNull(Me.[LoanOfficer]) And Not IsNull(Me.[LDDLogDateTimeCompleted]) Then
    DoCmd.GoToRecord , , acNext
    End If
    End Sub
    Basically, I'm trying to add a feature to my form that requires that if mandatory fields are not input, the form will not allow the user to "save" and proceed to the next record. Instead a message box will appear and require the user to input all the necessary information before the form can be "saved" and moved to the next record.

    I hope my explanation wasn't confusing.

    Thank you for your help.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    How about:

    Code:
    If ... Then
      MsgBox...
    Else
      DoCmd...
    End If
    You've already tested all the fields, so the Else will only run if none of the fields is Null.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    vickan240sx is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    34
    Quote Originally Posted by pbaldy View Post
    How about:

    Code:
    If ... Then
      MsgBox...
    Else
      DoCmd...
    End If
    You've already tested all the fields, so the Else will only run if none of the fields is Null.
    I feel stupid...

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Don't; we're all at some stage of learning, and nobody knows everything.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    A couple of points.....

    1) What if someone puts a space in a field?? It is not Null, so the record would probably save. I prefer to use

    Code:
    If Len(Trim(Me.[BorrowerName]))>0  OR ....


    2) You might think about using the FORM before update instead of a button to check for empty required fields. What happens if someone (don't you just hate him?) leaves 1 or more controls empty and closes the form or changes the record without using the button? The data in the controls will still be saved, resulting in a partial record. Access automatically saves any changes when you move to different record.

    Just thinking.........
    Last edited by ssanfu; 06-06-2012 at 05:01 PM. Reason: conputer can't spell

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would also use the before update event:

    http://www.baldyweb.com/BeforeUpdate.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    vickan240sx is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    34
    Quote Originally Posted by ssanfu View Post
    A couple of points.....

    1) What if someone puts a space in a field?? It is not Null, so the record would probably save. I prefer to use

    Code:
    If Len(Trim(Me.[BorrowerName]))>0  OR ....


    2) You might think about using the FORM before update instead of a button to check for empty required fields. What happens if someone (don't you just hate him?) leaves 1 or more controls empty and closes the form or changes the record without using the button? The data in the controls will still be saved, resulting in a partial record. Access automatically saves any changes when you move to different record.

    Just thinking.........
    You make a good point. I'll move the code to the before update and try out your code instead.

    Thanks!

  8. #8
    vickan240sx is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    34
    How would I be able to customize the before update code to be specific to each form? I have several forms that would need different criteria under the before update event.

  9. #9
    vickan240sx is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    34
    Quote Originally Posted by vickan240sx View Post
    How would I be able to customize the before update code to be specific to each form? I have several forms that would need different criteria under the before update event.
    Nevermind...it's because I'm using a template form and copying/pasting it to create other forms.

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

Similar Threads

  1. Replies: 29
    Last Post: 06-19-2012, 12:12 PM
  2. Simplifying a table
    By jrmvt in forum Database Design
    Replies: 5
    Last Post: 02-18-2011, 11:00 PM
  3. Word code in Access - How to modify my current code
    By Alexandre Cote in forum Programming
    Replies: 0
    Last Post: 11-15-2010, 08:26 AM
  4. Code in combobox, code in text box
    By float in forum Forms
    Replies: 3
    Last Post: 09-29-2010, 07:12 AM
  5. Access 2003 code vs Access 2007 Code
    By ralphjramirez in forum Access
    Replies: 5
    Last Post: 11-23-2009, 12:33 PM

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