Results 1 to 10 of 10
  1. #1
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Required Field (2003)

    Hi,

    I am using the following code to require user to enter the required fields.

    Dim msg As String, Style As Integer, Title As String
    Dim nl As String, ctl As Control


    nl = vbNewLine & vbNewLine

    For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
    If ctl.Tag = "*" And Trim(ctl & "") = "" Then
    msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
    "You can't save this record until this data is provided!" & nl & _
    "Enter the data and try again . . . "
    Style = vbCritical + vbOKOnly
    Title = "Required Data..."
    MsgBox msg, Style, Title
    ctl.SetFocus
    Cancel = True
    Exit For
    End If
    End If
    Next

    Everything is fine except the following access default messages pops up after the my customize message when I try to close the form:

    "You can't go to the specified record"
    "You can't save this record at this time...."

    How can I disable those 2 access default message since I already have the similar messages to warn the users?

    Thanks

    Regards

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Required Field (2003)

    In which event did you put the code?

  3. #3
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required Field (2003)

    Before Update Event.

    Thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Required Field (2003)

    You should not allow the user to close the form by clicking the x in the upper right corner. Put a command button cmdClose on the form to close it, and use code similar to this:

    ' Module level variable to control whether closing is allowed
    Private blnCanClose As Boolean

    Private Sub Form_Unload(Cancel As Integer)
    ' Don't allow closing the form if blnCanClose is False
    Cancel = Not blnCanClose
    End Sub

    Private Sub cmdClose_Click()
    ' Check conditions here
    If ... Then
    blnCanClose = True
    Else
    blnCanClose = False
    End If
    ' Only close if conditions are met
    If blnCanClose Then
    DoCmd.Close
    End If
    End Sub

  5. #5
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Required Field (2003)

    Putting the code there is fine. However, you need a way for the user to undo the changes so they can exit. Put a Command button on the form with a caption of "Undo"; it's only code need be:

    If me.dirty then me.undo

    The user can Undo the changes by hitting the Escape key, but that's often too much for them to remember.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  6. #6
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required Field (2003)

    Thanks, Hans.

    But I am kind of confusing what codes should I put in the If ... Then of Close click event? Should I put the require fields code or should I verify if the field have the vaule?

    Thanks

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Required Field (2003)

    You should check that the required fields have been filled in.

  8. #8
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required Field (2003)

    Thanks, Hans.

    Should I put:

    If IsNull(field) = Ture then

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Required Field (2003)

    You probably do *not* want to allow the user to close the form is one or more fields are blank, so you should use something like

    If IsNull(Me.Field1) Or IsNull(Me.Field2) Then
    MsgBox "Please fill in all required fields."
    blnCanClose = False
    Else
    blnCanClose = True
    End If

  10. #10
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Required Field (2003)

    Thanks, Hans.

Posting Permissions

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