Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2004
    Location
    Edinburgh, Scotland
    Posts
    17

    Unanswered: Empty Fields message

    I have a save button on my form. I only want it to save the details & close the form if all vital fields have been filled in. If not then a message box should appear to tell the user there are empty fields.

    Here's what I've got so far, but it doesn't work. Any ideas where I'm going wrong?

    Private Sub Save_Click()
    If cboMake = Null Then
    retValue = MsgBox("You have not completed the make!", vbOK)
    End If

    If cboModel = Null Then
    retValue = MsgBox("You have not completed all the model!", vbOK)
    End If

    If no = Null Then
    retValue = MsgBox("You have not completed all the phone number!", vbOK)
    End If

    If Date = Null Then
    retValue = MsgBox("You have not completed all the Ad date!", vbOK)
    End If

    DoCmd.Save
    DoCmd.Close
    End Sub

  2. #2
    Join Date
    Sep 2004
    Posts
    161
    Use Isnull like this
    if Isnull(Date) then
    endif

  3. #3
    Join Date
    Dec 2003
    Location
    Dallas, TX
    Posts
    1,004
    Quote Originally Posted by longIT
    I have a save button on my form. I only want it to save the details & close the form if all vital fields have been filled in. If not then a message box should appear to tell the user there are empty fields.

    Here's what I've got so far, but it doesn't work. Any ideas where I'm going wrong?

    Private Sub Save_Click()
    If cboMake = Null Then 'not the way to phrase it....see BOLD type
    If IsNull(cboMake) Then
    MsgBox("You have not completed the make!"), vbOK
    End If

    If cboModel = Null Then
    retValue = MsgBox("You have not completed all the model!", vbOK)
    End If

    If no = Null Then
    retValue = MsgBox("You have not completed all the phone number!", vbOK)
    End If

    If Date = Null Then
    retValue = MsgBox("You have not completed all the Ad date!", vbOK)
    End If

    DoCmd.Save
    DoCmd.Close
    End Sub
    See that above in bold type. That should be how you write your code to make
    it work as I have in my databases as well. Another simple way you can do it
    and eliminate having to do all this is simply this: in the table, make those
    particular fields REQUIRED / YES Just a thought that might prevent you so much coding all the time. Or if you just want to code it, then do it behind each field in the BeforeUpdate event so that you can catch them right there before they leave. And still have your neat little message to display.
    Hope this helps you out some,

    have a nice one,
    Bud

  4. #4
    Join Date
    Dec 2003
    Location
    Dallas, TX
    Posts
    1,004

    Cool

    Oh, just one more note...food for thought. You might want to add a bit more flexibility on behalf of the end user just in case that don't have the info to place in that field. Instead of just vbOKOnly, might try something like vbOKCancel and when they Cancel it either shoves them to another part of the form or just UnDo any changes previously made before they got to that point and close the form. Just something to think about when you are dealing with designing a system for others to use. Maybe hit them with this message:
    This would be the likely first message if the don't input anything:
    1:This is a required field you have to input something! vbOKOnly
    It then takes them right back to that field once again.

    Second time if they don't input anything do this one next:
    2: You didn't input anything, leaving it blank will close this form. Do you wish to close it?, vbYesNo

    Now, they have a choice just in case they got anxious and messed up.
    If they answer YES it Cancels out and Undoes all changes.
    If they answer NO it takes them right back to that field again.

    I hope this gives you more to consider in future database designs. Just here trying my best to help all that I can. After all, I learned a lot here and elsewhere.

    night all...yawnnnnnn
    Bud

  5. #5
    Join Date
    Dec 2003
    Location
    Dallas, TX
    Posts
    1,004
    Quote Originally Posted by longIT
    I have a save button on my form. I only want it to save the details & close the form if all vital fields have been filled in. If not then a message box should appear to tell the user there are empty fields.

    Here's what I've got so far, but it doesn't work. Any ideas where I'm going wrong?

    Private Sub Save_Click()
    If cboMake = Null Then 'not the way to phrase it....see BOLD type
    If IsNull(cboMake) Then
    MsgBox("You have not completed the make!"), vbOK
    End If

    If cboModel = Null Then
    retValue = MsgBox("You have not completed all the model!", vbOK)
    End If

    If no = Null Then
    retValue = MsgBox("You have not completed all the phone number!", vbOK)
    End If

    If Date = Null Then
    retValue = MsgBox("You have not completed all the Ad date!", vbOK)
    End If

    DoCmd.Save
    DoCmd.Close
    End Sub
    See that above in bold type. That should be how you write your code to make
    it work as I have in my databases as well. Another simple way you can do it
    and eliminate having to do all this is simply this: in the table, make those
    particular fields REQUIRED / YES Just a thought that might prevent you so much coding all the time. Or if you just want to code it, then do it behind each field in the BeforeUpdate event so that you can catch them right there before they leave. And still have your neat little message to display.
    Hope this helps you out some,

    have a nice one,
    Bud
    Last edited by Bud; 10-12-04 at 04:19. Reason: duplicate post, tried to delete this one...

  6. #6
    Join Date
    Oct 2004
    Location
    Edinburgh, Scotland
    Posts
    17
    I've got the save button to work using:

    If IsNull(cboMake) Then
    retValue = MsgBox("You have not completed the make!", vbOKOnly)
    End If

    But how can I get the button to save and close the form if all the required fields have been filled in?

    I've tried:

    If IsNull(cboMake) Then
    retValue = MsgBox("You have not completed the make!", vbOKOnly)
    ElseIf cboMake = "," Then
    DoCmd.Close
    End If

    but that doesn't work.

  7. #7
    Join Date
    Sep 2004
    Posts
    161
    Use your code and add before each end if
    Code:
    Private Sub Save_Click()
    If cboMake = Null Then 'not the way to phrase it....see BOLD type
    If IsNull(cboMake) Then
    MsgBox("You have not completed the make!"), vbOK
    Exit Sub
    End If
    
    If cboModel = Null Then
    retValue = MsgBox("You have not completed all the model!", vbOK)
    End If
    
    If no = Null Then
    retValue = MsgBox("You have not completed all the phone number!", vbOK)
    End If
    
    If Date = Null Then
    retValue = MsgBox("You have not completed all the Ad date!", vbOK)
    End If
    
    DoCmd.Save
    DoCmd.Close
    End Sub

  8. #8
    Join Date
    Oct 2004
    Location
    Edinburgh, Scotland
    Posts
    17

    didn't work

    I tried the following, but it didn't work:

    If IsNull(cboMake) Then
    retValue = MsgBox("You have not completed the make!", vbOKOnly)
    Exit Sub
    End If

    Nothing happens.

  9. #9
    Join Date
    Jun 2002
    Location
    Saudi Arabia / Philippines
    Posts
    126
    LongIT

    Bud and jepi are absolutely correct and the code should work. Three thoughts however: the DoCmd.Save is somewhat redundant as Access will attempt a save when closing a dirty form; are you sure your controls will be null?; you don't need to use the function form of MsgBox.

    I often write:

    strMsg = "Please enter a value in xyz"
    If IsNull(Me.XYZ) then
    MsgBox strMsg
    Exit Sub
    ElseIf Len(Trim(Me.XYZ)) <= 0
    MsgBox strMsg
    Exit Sub
    End If


    At the bottom of the procedure I put my Close.
    Rod

    fe_rod@hotmail.com

  10. #10
    Join Date
    Oct 2004
    Location
    Edinburgh, Scotland
    Posts
    17
    That worked. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •