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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making text field required IF checkbox = true

Status
Not open for further replies.

nivlac

Technical User
May 9, 2001
25
US
How can I make certain text fields required for data-entry ONLY IF a corresponding checkbox is checked on a form. For example, if an employee checks the Add Item box, then the Item Name, Manufacturer, and Catalog Number MUST be filled in. However, if the employee checks the Price Change box, the above fields can be left blank, but the Old Price and New Price fields will then become required. Any suggestions?

Thanks!
Susan
 
One way to do it is to put some VBA code to check things in the "Before Update" event of the form. Something similar to this might work:
-------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
if me.ckBox = true then
if isnull(me.txbox1) or isnull(me.txtbox2) then
msgbox("You must fill in both textbox1 and " _
& "textbox 2 when ckBox is checked")
Cancel = true
end if
else
if isnull(me.txtbox3) or isnull(me.txtbox4) then
msgbox("You must fill in both textbox3 and " _
& "textbox4 when ckBox is not checked")
Cancel = true
end if
end if

End Sub
----------------------------

Hope that helps,

Herb
 
Susan,

Try using the vbmodal property. i.e. if checkbox = true then txtbox.vbmodal = true.

Russ
 
Use the Form's BeforeUpdate Event to check the TextBoxes based on the value of the CheckBox
(something along this line)

Private Sub Form_BeforeUpdate(Cancel As Integer)
If chkAddItem = True Then
'Verify These TextBoxes
If IsNull(txtItemName) Or IsNull(txtMfr) Or IsNull(txtCatNo) Then
Msgbox "You Need to Fill in New Item Data"
Cancel = True
Exit Sub
End If
End If
If chkPriceChg = True Then
'Verify These TextBoxes
If IsNull(txtOldPrice) Or IsNull(txtNewPrice) Then
Msgbox "You Need to Fill in Price Data"
Cancel = True
Exit Sub
End If
End If
End Sub

PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top