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

    Form Validation (2003)

    Hi,

    I am using the following codes to validate all fields on the form are inputed before update:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim Msg As String, Style As Integer, Title As String
    Dim DL As String, ctl As Control

    DL = vbNewLine & vbNewLine

    For Each ctl In Me.Controls

    If ctl.ControlType = acTextBox Then
    If Trim(ctl.Value & "") = "" Then

    Msg = "'" & ctl.Name & "' is Required!" & DL & _
    "Please enter a value to proceed . . ."
    Style = vbInformation + vbOKOnly
    Title = "Required Data Missing! . . ."
    MsgBox Msg, Style, Title
    ctl.SetFocus
    Cancel = True
    Exit For
    End If
    End If
    Next
    End Sub

    I have a add button on the form with codes in on click event below:

    DoCmd.GoToRecord , , acNewRec

    Now if I have some fields are blank and then click the add button, the warning message pops up to to inform user that which field is missing. Then I get a error message: Run-time error '2105'. You can't go to the specified record. If I click "Debug," and then it open VB window with yellow highlight on DoCmd.GoToRecord , , acNewRec.

    Any idea why it happened?

    Thanks

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

    Re: Form Validation (2003)

    I'd either insert a line

    On Error Resume Next

    above the line with DoCmd.GoToRecord. or build an error handler.

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

    Re: Form Validation (2003)

    Thanks, Hans. It works.

    If I force to close the form and I will get a Access standard error message "You can't save this record at this time. Microsoft Office Acess may have encountered an error while....." How can I customize this error message?

    Also I have a combo box on the form, what codes should I add to validate the combo box is inputed before user add new data?

    Thanks again.

    Regards

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

    Re: Form Validation (2003)

    You can use the Form_Error event to handle general errors. See <post#=667,850>post 667,850</post#> and the thread starting at <post#=451,828>post 451,828</post#> for examples. The latter thread also explains how to find the relevant error numbers.

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

    Re: Form Validation (2003)

    Thank you so much, Hans.

    One last questison, what codes should I add to the following to validate combo box value on the form:

    Dim Msg As String, Style As Integer, Title As String
    Dim DL As String, ctl As Control

    DL = vbNewLine & vbNewLine

    For Each ctl In Me.Controls

    If ctl.ControlType = acTextBox Then
    If Trim(ctl.Value & "") = "" Then

    Msg = "'" & ctl.Name & "' is Required!" & DL & _
    "Please enter a value to proceed . . ."
    Style = vbInformation + vbOKOnly
    Title = "Required Data Missing! . . ."
    MsgBox Msg, Style, Title
    ctl.SetFocus
    Cancel = True
    Exit For
    End If
    End If

    Next


    Thanks

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

    Re: Form Validation (2003)

    Change

    If ctl.ControlType = acTextBox Then

    to

    If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then

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

    Re: Form Validation (2003)

    Thank you so much, 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
  •