Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement.... 1

Status
Not open for further replies.

EasyOnly

Technical User
Oct 11, 2008
55
US
I am trying to get this working correctly:

If the user click on travel, then msgbox will say comments required. then the focus should go to comments field and user will not be able to move out of the field till they write comments. the code I am using:

Private Sub Form_BeforeUpdate (Cancel as Integer)
If Me.Travel = True and IsNull(Me.Comments) Then
msgbox "Comments required"
Cancel = True
End If
End Sub

 
What about this ?
Code:
Private Sub Form_BeforeUpdate (Cancel as Integer)
If Me!Travel = True And Trim(Me!Comments & "") = "" Then
   Me!Comments.SetFocus
   Cancel = True
End If
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I would use something like this. checks for comments if isnull then sets focus and highlights the backcolor yellow
then exits the sub without preforming any other code ie: saving the record. This will force them to enter comments before it will goto 'else'. as far as locking them into the field I dont know of an easy way to do this

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    If Me.Travel = True Then
        If IsNull(Me.txtComments) Then
            MsgBox "Comments required"
            Me.txtComments.SetFocus
            Me.txtComments.BackColor = vbYellow
            Exit Sub
        Else
            'perform some update
        Cancel = True
        End If
    End If
End Sub

HTH << MaZeWorX >> Remember amateurs built the ark - professionals built the Titanic [flush]
 
opps sorry PHV replied before your post was updated :)

HTH << MaZeWorX >> Remember amateurs built the ark - professionals built the Titanic [flush]
 
I used the code below but yet when I closed form, it closed without forcing the field.

Private Sub Form_BeforeUpdate (Cancel as Integer)
If Me!Travel = True And Trim(Me!Comments & "") = "" Then
Me!Comments.SetFocus
Cancel = True
End If
End Sub
 
how many other controls are on this form? and is there any other code attached to the form?

HTH << MaZeWorX >> Remember amateurs built the ark - professionals built the Titanic [flush]
 
MazeWorX,

thank you so much. it worked.

now if I want to add conditional format to the comments field if the checkbox is checked the comments box will turn yellow till we have some comments, then it will turn white.

Thanks
 
You could set the format on the afterupdate for the comments field

Me.txtComments.BackColor = vbWhite

and additionally set the focus to another control on your form

Me.controlName.Setfocus

HTH << MaZeWorX >> Remember amateurs built the ark - professionals built the Titanic [flush]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top