Results 1 to 13 of 13
  1. #1
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352

    Having Issues With MsgBox Function.


    There are a few fields in my form I want make sure a user has filled in (but I don't want to make them 'required' since that causes confusion and unnecessary questions if users click on and off a field or erase some data and click off or whatever).
    Behind my submit button I have this
    Code:
    Private Sub btnSubmit_Click()
    If Me.DateEntry.Value = Null Or Me.cboDefect.Value = Null Or Me.cboCommodity.Value = Null Or Me.txtPartNumber_PK.Value = Null Or Me.cboShift.Value = Null Or Me.txtSupplier.Value = Null Then
    MsgBox "Please ensure all applicable fields are filled in.", vbExclamation + vbOKOnly, "Missing Information"
    Else: DoCmd.GoToRecord , , acNewRec
    End If
    End Sub
    I want the message box to appear if the user has the specified fields blank. But this doesn't happen and the acNewRec proceeds anyways. I tried having all the cbos and txts listed as .Value = "" but it didn't work either.
    Thanks in advance.

  2. #2
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    Are you sure you are checking for the right null? These may work better
    value = vbNull
    or
    Nz(Value,"")=""

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    1 IMHO this kind of validation would be better done in the forms Before update event.
    2 Have you tried setting a breakpoint and then step through the code so that you can see the value of each control (by holding the mouse over eg: Me.DateEntry.Value ). I suspect that some of your controls are not holding Null values when the code runs.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Bob, why would before update be better?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Cannot do = Null. Use IsNull(Me.DateEntry)

    See error 5 in http://allenbrowne.com/casu-12.html

    Data validation should be done in BeforeUpdate event if you want to cancel the update if some fields not filled in. Since you have an Else branch, I also think BeforeUpdate is more appropriate.

    If you want to give the user an option to return to enter missing data, consider:

    Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.DateEntry) Or ... Then
    If MsgBox("Some fields are empty. Do you want to resume data entry for this record?", vbYesNo) = vbYes Then Cancel = True
    End If
    End Sub

    Then in the AfterUpdate event:
    DoCmd.GoToRecord , , acNewRec
    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.

  6. #6
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Alright thank you both! What you suggested + link was helpful, thanks June

  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,770
    Did some edits to post you might not have seen.
    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
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    So if the user says no then it is considered 'updated' and the after update would proceed? But the cancel event does not count as updating?

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Right, if user elects to continue edit the update is canceled and record would not be committed. The AfterUpdate would not trigger.
    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.

  10. #10
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    If i take all this out of my 'submit' button how is the message box initiated (via a button)?
    Would a simple Me.Refresh suffice?

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Good question. Guess I need to backtrack a bit. On reflection, I've never actually used Form BeforeUpdate or AfterUpdate (I do use those events with controls, a lot). I do use code behind buttons, similar to your Submit. So for yours:
    Code:
    If IsNull(Me.DateEntry) Or ... Then
        If MsgBox("Some fields are empty. Do you want to resume data entry for this record?", vbYesNo) = vbYes Then Exit Sub  
    End If
    DoCmd.GoToRecord , , acNewRec
    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.

  12. #12
    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
    Quote Originally Posted by nick404 View Post
    If i take all this out of my 'submit' button how is the message box initiated (via a button)?
    Leave the validation code in the Form_BeforeUpdate event, and then, behind your Command Button, use either

    DoCmd.RunCommand acCmdSaveRecord

    or

    If Me.Dirty Then Me.Dirty = False

    When Access goes to save the Record, execution will move to the Form_BeforeUpdate event and the validation code will fire. It will also fire if any other thing tries to save the Record, i.e. by leaving the Record, closing the Form or closing Access, itself.

    Linq ;0)>

  13. #13
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Excellent. Thanks for the insights guys

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

Similar Threads

  1. Msgbox function with Help option
    By John_G in forum Programming
    Replies: 3
    Last Post: 12-01-2014, 11:56 AM
  2. Replies: 0
    Last Post: 03-12-2012, 03:57 PM
  3. MsgBox
    By Mtyetti in forum Forms
    Replies: 4
    Last Post: 07-27-2011, 01:51 PM
  4. Replies: 4
    Last Post: 11-06-2009, 09:51 AM
  5. pass a variable to the MsgBox function
    By 3dmgirl in forum Programming
    Replies: 2
    Last Post: 04-19-2007, 07:14 AM

Tags for this Thread

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