Results 1 to 8 of 8
  1. #1
    sharonir22 is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    16

    form validation

    hi
    I created a form with several different fields. I have a button that allows to save the record and close the form. I want to create a code for the click option that checks if all the fields are filled and doesn't allow you to save the recordif there are empty fields.

    I tried doing it in an if statement but it was very long and didn't work.
    I would appreciate a written code or any kind of help
    tnx

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Options:

    1. set each field as Required in table properties then let Access nag the users - they will learn to make sure everything is filled in

    2. VBA code as you attempted - post your code for analysis. What does 'didn't work' mean - error message, wrong results, nothing happens?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    Put this in to validate the form before saving data...

    Code:
    Sub btnSave_click()
    
    if IsValidForm() then
       'Save
       'CLose form
    End if
    End sub
    
    private function IsValidForm() as boolean
    Dim vMsg 
    
    Select case true 
        Case isNull (txtName)
              VMsg = "Name is missing"
        
         case isNull(cboState)
                  VMsg = "State is missing"   
     
         Case txtPass1 <> txtPass2
                VMsg = "Passwords don't match"   
    End select
    
    IsValidForm= vMsg= ""
    if vMsg <> "" then MsgBox vMsg,vbCritical, "Reqd Field"
    end function

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    In Access fields are in tables, controls are on Forms.
    Entered data can be validated in the BeforeUpdate event of the Form.
    Here are some reference links
    http://www.blueclaw-db.com/access_ev...foreupdate.htm
    https://msdn.microsoft.com/en-us/lib.../ff822421.aspx

    Good luck.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    As orange has suggested, the Form_BeforeUpdate event is the usual place for validation code that checks to see that one or more Controls are populated, before allowing the Record to be saved. It's fine that you have a custom 'save & close' Command Button, but Access will, by default, save a Record if the user navigates to another Record, closes the Form or closes Access, itself!

    My usual advice, if a developer really needs a 'save' or 'save & close' button, is to use the button to do just that, save the Record and and close the Form. This will send Access to the Form_BeforeUpdate event, where you can validate the Control(s) and act accordingly, Cancelling the Update, if need be, so that corrective action can be taken.

    Linq ;0)>

  6. #6
    sharonir22 is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    16
    I made the first problem work but I also want to make sure that if a user exits the form by closing it and not through the save button, the record will not be created in the database
    Help?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Disable the form X close. Also disable the right click shortcut menu by Option settings.

    Can also customize the ribbon to disable app X close and menu Exit but this is rather advanced coding.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Then you need to do the validation in the Form_BeforeUpdate event, as stated! Regardless of how you leave a Record, by moving to another Record, closing the Form or closing Access, itself, after the Form has been dirtied, the Form_BeforeUpdate event will fire!

    Linq ;0)>

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

Similar Threads

  1. Form validation before saving
    By BDevil15 in forum Access
    Replies: 3
    Last Post: 09-01-2014, 07:22 AM
  2. VBA Form Validation
    By WickidWe in forum Access
    Replies: 5
    Last Post: 12-30-2013, 07:13 PM
  3. I can't seem to get the Validation set for form fields.
    By shane201980 in forum Programming
    Replies: 10
    Last Post: 10-05-2012, 03:08 AM
  4. Form Validation Rule
    By galkej in forum Forms
    Replies: 4
    Last Post: 07-01-2012, 06:38 PM
  5. Validation at form level
    By Kris in forum Forms
    Replies: 1
    Last Post: 06-28-2011, 06:48 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