If-Then Help! Seems easy

M

Maver911

This question is hard to explain but basically im getting a runtime
error#13 at the bottom where it says: "if strng then". Idk if thats
proper syntax or not but im trying to make it so that i can have a
search and a search criteria withuot doing ALL the different IF
combonations. This is the best way i could think of and it doesnt seem
to like the whole string being the criteria. Could anyone please help
me.

dim strng As String

Set find = CurrentDb.OpenRecordset("Name", dbOpenDynaset)
find.MoveFirst

If chkJob.value = -1 Then
If Len(strng) = 0 Then
strng = "find!title = " & cboJobtitle
Else
strng = strng & " AND find!title = " & cboJobtitle
End If
End If
If chkyears.value = -1 Then
If Len(strng) = 0 Then
strng = "find!yrs = " & txtYrs
Else
strng = strng & " AND find!yrs = " & txtYrs
End If
End If
If chkdegree.value = -1 Then
If Len(strng) = 0 Then
strng = "find!degree = " & cboDegree
Else
strng = strng & " AND find!degree = " & cboDegree
End If
End If
If chkpmi.value = -1 Then
If Len(strng) = 0 Then
strng = "find!title = " & cboJobtitle
Else
strng = strng & " AND find!pmi = " & cboPMI
End If
End If
If chkmort.value = -1 Then
If Len(strng) = 0 Then
strng = "find!mortgage = " & cboMort
Else
strng = strng & " AND find!mortgage = " & cboMort
End If
End If

Do Until find.EOF
If strng Then
ReDim Preserve names(x)
names(x) = find!id
x = x + 1
End If
find.MoveNext
Loop
 
B

BruceM

I have a few observations. First, it would be best if you spell out things
such as "I don't know" (I assume that's what you meant). Not everybody here
speaks Instant Messenger, and those of us who can figure it out may waste
time doing so. Now for the code. I don't see a need for checking the
length of the string, since the string isn't defined before the Len
statements. Also, the .Value property is the default, so you don't need to
specify it for the check box (although there's no harm in doing so). The
problem with strng at the end of the code is that it needs to equal
something (If strng = "", or whatever).

More comments inline.

Maver911 said:
This question is hard to explain but basically im getting a runtime
error#13 at the bottom where it says: "if strng then". Idk if thats
proper syntax or not but im trying to make it so that i can have a
search and a search criteria withuot doing ALL the different IF
combonations. This is the best way i could think of and it doesnt seem
to like the whole string being the criteria. Could anyone please help
me.

dim strng As String

Set find = CurrentDb.OpenRecordset("Name", dbOpenDynaset)
find.MoveFirst

If chkJob.value = -1 Then
If Len(strng) = 0 Then
strng = "find!title = " & cboJobtitle
Else
strng = strng & " AND find!title = " & cboJobtitle
End If
End If

Two points here: First, strng has no value that I can see before this line,
so this is the same as "find!title = " & cboJobtitle. Second, your string
is going to be (assuming for now that the value in cboJobTitle is
"Supervisor":
find!degree = Supervisor
The string is the literal value within the quotes plus the combo box value.
However, this part of the code means nothing, since you keep redefining
strng. You define strng, then you move to the next part of the code
(chkYears), where the length of strng will not be zero. If chkYears is 0
there is no code to cover that contingency. I have added something in
uppercase to illustrate this point:

If chkyears.value = -1 Then
If Len(strng) = 0 Then
strng = "find!yrs = " & txtYrs
Else
strng = strng & " AND find!yrs = " & txtYrs
ELSE
WHAT HAPPENS IF CHKYEARS = 0
End If
End If

If all of the check boxes are 0, strng will be Null (or maybe an empty
string; I'm not sure which), since there is nothing in the code to cover the
contingency of the check boxes being false. If all of the check boxes are
true, strng will be the cumulative value of all sections of the code. If
some are true and some are false, strng will have the cumulative value of
all the True conditions.

What exactly are you trying to do? I gather that it's a search, but are you
trying to filter the search depending on the values in the check boxes?
What do you expect strng to equal if chkYears is -1 and the rest of the
check boxes are 0, and what do you intend to do with that string in that
case?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top