Log in

View Full Version : Solved: How do I write VBA to set a check box to false if a certain condition is met



nathan2314
01-14-2009, 10:05 AM
Hello all:hi:
I think this should be relatively easy but I cant get it to work. I'm trying to set a check box to false if another text box on my form is blank or has a default text in it. I get it to work fine with the default text but for some reason if I clear the text box the check box does not uncheck.
Here is my VBA code so far:
Private Sub Donation1_AfterUpdate()
If Me.Donation1 Is Null Or Me.Donation1 = "Enter Name of Person or Charity" Then
Me.Donations = False
Else
Me.Donations = True
End If
End Sub
Do in the code, Me.Donation1 is the text box and Me.Donations is the check box. So I want Me.Donations to unchick (false) if the text box, Me.Donations1, is either an empty string (blank) or has the default value of "Enter Name of Person or Charity" in it. The code works for the default value but does not work for the empty string. Ive tried substituting 'Empty', "" and '= Null' for the "Is Null" part of the above code. But that doesnt work either.
Appreciate any help!
:yes

CreganTur
01-14-2009, 10:12 AM
The code works for the default value but does not work for the empty string. Ive tried substituting 'Empty', "" and '= Null' for the "Is Null" part of the above code. But that doesnt work either.

Is Null is proper syntax for SQL statements, but not for VBA. VBA has a built in method to test for null values called IsNull(). This is what you use to test for null values.

Rewriting your code:


Private Sub Donation1_AfterUpdate()
If (IsNull(Me.Donation1) Or Me.Donation1 = "Enter Name of Person or Charity") Then
Me.Donations = False
Else
Me.Donations = True
End If
End Sub


Also, when testing multiple conditions on a single If line, you need to encase all of the conditions within parentheses. This is just like math, it tells the If Function to look at the entire value of what is within the parentheses, instead of looking at each one individually.

HTH:thumb

nathan2314
01-14-2009, 10:29 AM
Hey Thanks!!
That worked great.:friends: