VB Code check please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wayneyh
    New Member
    • Mar 2008
    • 67

    VB Code check please

    Hi All

    I am having a little problem with the following code. I can't seem to get it to go back to focus on the Contact field. Please Advise.

    Private Sub Contact_LostFoc us()
    If IsNull(Contact) Then
    MsgBox "This field must be filled in!"
    Me.Contact.SetF ocus
    End If
    End Sub


    Regards

    Wayne
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Originally posted by Wayneyh
    Hi All

    I am having a little problem with the following code. I can't seem to get it to go back to focus on the Contact field. Please Advise.

    Private Sub Contact_LostFoc us()
    If IsNull(Contact) Then
    MsgBox "This field must be filled in!"
    Me.Contact.SetF ocus
    End If
    End Sub


    Regards

    Wayne
    Hi

    I think perhaps you should use the BeforeUpdate Event something like this.

    Code:
    Private Sub Contact_BeforeUpdate(Cancel As Integer)
        If IsNull(Contact) Then
            MsgBox "This field must be filled in!"
            Cancel = True
        End If
    End Sub
    This will prevent focus from leaving the control if it is null.

    Hope that helps.

    MTB

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      As to the
      I can't seem to get it to go back to focus on the Contact field
      this is one of those quirks of Access. The focus both has and hasn't actually left the field in question when the LostFocus event fires. The way to beat this, as Mike TheBike has suggested, is to use the BeforeUpdate event and use the line

      Cancel = True

      The problem with using the BeforeUpdate event of the textbox for this kind of validation is that the user can simply not enter the textbox at all, in which case the validation won't take place at all! You need to place the code in the form's BeforeUpdate. This will insure that the user has to fill in the data.

      Code:
      Private Sub Form_BeforeUpdate(Cancel As Integer)
           If IsNull(Me.Contact) Then
               MsgBox "This field must be filled in!"
               Cancel = True
               Me.Contact.SetFocus
           End If
       End Sub
      This is true for data validation that is intended to insure that a given field is populated before saving the record . Validation that is intended to insure that data entered into a field complies with some criteria, such as an ID number containing only numeric characters or a description field only containing alpha characters, can be placed in either the BeforeUpdate event of the textbox or the form.

      Linq ;0)>

      Comment

      • Wayneyh
        New Member
        • Mar 2008
        • 67

        #4
        Ok, the code works fine. The problem i now have is that the contact field is the first field on the form. When the sales form loads and if i tab away from the contact field the code doesn't work. How do i activate the form so that the code works.

        Regards

        Wayne

        Comment

        • Wayneyh
          New Member
          • Mar 2008
          • 67

          #5
          sorry guys my mistake. i put the code in the contact fields beforeupdate instead of the forms. works fine now. thanks

          Comment

          Working...