Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Mar 2007
    Posts
    91

    Unanswered: Mandatory / Required field

    Hi guys

    I'm trying to force the user to enter the Customername, Contact person and (tel or cell nbr). Everything's fine for the customer and contact person as i hav made the required fields on the table.

    The problem is i the user doesn't enter the customername or contact person it complains but closes the screen and i don't want the application to do dat. I want it to complain then go back to the field that still needs to be entered.

    I also need help with enforcing data in the tell or cell nbr field (i can't make dem required as it's fine not 2 hav 1 if the other is there)

    Here's ma code

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Before closing this form you have to check if the mandotory fields (*) have been entered else complain and don't allow the user to continue
    If ValidateRecord = False Then
    MsgBox ("Please enter the Customer Name or Contatct")
    Me.CustomerName.SetFocus
    On Error GoTo Err_cmdMainScreen_Click
    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "New_Customer"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    Exit_cmdMainScreen_Click:
    Exit Sub

    Err_cmdMainScreen_Click:
    MsgBox Err.Description
    Resume Exit_cmdMainScreen_Click
    Else
    MsgBox ("Done")
    End If

    End Sub

    Ma field types are text by d way.

    Thanks

  2. #2
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    There's nothing in this code that is closing the form. Try putting
    Cancel = True
    in there, and that will stop the update

    Why are you opening the "New Customer" form from this point in the process?
    Inspiration Through Fermentation

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    If isnull(Me.CustomerName.value) then
    Msgbox "Requierd field"
    Me.CustomerName.SetFocus
    exit sub
    Else
    ...
    Endif
    George
    Home | Blog

  4. #4
    Join Date
    Mar 2007
    Posts
    91
    Cancel = True doesn't do the trick. I tried it :-( Am only calling the to avoid it closing but unfortunately dat didn't work either

  5. #5
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    hmmm I never use the Form_____Update events. Will have to read up on it.
    Are you using an after update, also?
    Inspiration Through Fermentation

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Depending on your save method...
    If you have a command button to save the records, run my validation code from a few posts ago adding an elseif for each field you need to check.
    then in the Else statement run your save.

    If however they're saved as soon as they are updated (ie bound objects) then you need to run the validation on and afterupdate event on each control.
    George
    Home | Blog

  7. #7
    Join Date
    Mar 2007
    Posts
    91
    To answer yo Q Redneckgeek nope i don't do anythin afta update

    Here's what i've done

    Private Sub Form_BeforeUpdate(Cancel As Integer)

    If IsNull(Me.CustomerName) Then
    MsgBox ("Please enter the Customer Name")
    Me.CustomerName.SetFocus
    Cancel = True
    Exit Sub
    ElseIf IsNull(Me.CONTACT) Then
    MsgBox ("Please enter the Contact Name")
    Me.CONTACT.SetFocus
    Cancel = True
    Exit Sub
    ElseIf IsNull(Me.PHONE_NO) Or IsNull(Me.CELL_NO) Then
    MsgBox ("Please enter the Telephone or Cell Number")
    Me.PHONE_NO.SetFocus
    Cancel = True
    Exit Sub
    End If

    End Sub


    and i created a module

    under general declarations
    Option Compare Database

    Public blnClose As Boolean

    then

    Private Sub Form_Unload(Cancel As Integer)
    If blnClose Then
    Cancel = True
    End If

    End Sub

    then

    Private Sub Form_Current()
    blnClose = False
    End Sub

    But i still get the same error... plsss help

  8. #8
    Join Date
    Mar 2007
    Posts
    91
    Thanks guys 4 ol yo help

    I found out that it skipped ma save command and went straight to the close command

    Here's another problem

    I modified ma original code so that it can let the user know specifically what's required and not entered...

    I want the application to allow the user the option of entering the phone nbr or cell nbr but 1 of them has to be there.
    Currently it's complainin if both r not the

    Here's ma code

    ElseIf IsNull(Me.PHONE_NO) Or IsNull(Me.CELL_NO) Then
    MsgBox ("Please enter the Telephone or Cell Number")
    Cancel = True
    Me.PHONE_NO.SetFocus
    Exit Sub

    I've tried changin the or to and but still it complains with the same error msg.
    Pls kindly advise

  9. #9
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Remember: You only want to return the error if BOTH are blank...
    "If this is blank OR this is blank then"
    Imagine if phone number was blank but cell wasn't - this statement would be applicable.
    "If this AND this is blank then..."
    If BOTH fields are blank, then handle the error.
    Code:
    If Me.PHONE_NO.Text = "" AND Me.CELL_NO.Text = "" Then
     'Also, isnull is a bit flakey, so if you use .Text you return a string value which you can compare to ""
     MsgBox ("Please enter the Telephone or Cell Number")
     Cancel = True
     Me.PHONE_NO.SetFocus
     Exit Sub
    End If
    Alternatively:
    Code:
    Dim x As integer
    x = 0
    
    If Me.PHONE_NO.Text = "" Then
     x = x + 1
    End If
    
    If Me.CELL_NO.Text = "" Then
     x = x + 1
    End If
    
    If x > 0 Then
     Msgbox "We have an empty field"
     Exit sub
    End If
    George
    Home | Blog

  10. #10
    Join Date
    Mar 2007
    Posts
    91
    4 d 1st option i get d followin error msg

    Run time error '2185'
    U can't reference a property or method for a control unless the control has focus

  11. #11
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I'm assuming it's on the line that reads: If Me.PHONE_NO.Text = ""...
    You know how you used isnull() before...

    Not giving you any more help on this one - you can do it!
    George
    Home | Blog

  12. #12
    Join Date
    Mar 2007
    Posts
    91
    hahahahaaaaaaaa :-)
    Am gonna work on it and let u know how it goes.
    Thanks

  13. #13
    Join Date
    Mar 2007
    Posts
    91
    I got it... here's ma code

    ElseIf IsNull(Me.PHONE_NO) Then
    If IsNull(Me.CELL_NO) Then
    MsgBox ("Please enter the Telephone or Cell Number")
    Cancel = True
    Me.PHONE_NO.SetFocus
    Exit Sub
    End If
    End If


    Thanks guys :-)

  14. #14
    Join Date
    Jan 2007
    Location
    California, USA
    Posts
    520
    May I suggest, rather than checking for IsNull(Me.Phone_No), you check with this code:
    Code:
    Len(Trim(Nz(Me.Phone_No))) = 0
    This code I am suggesting will check to be sure that at least something is entered other than a blank. Of course, if a valid phone number is at least 7 digits, you could change that to "= 7" or ">= 7" if you wanted to be more sure of getting a phone number.

  15. #15
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Good call Vic!
    What's the "Nz" for?
    George
    Home | Blog

Posting Permissions

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