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

i get a else without if statement when running if statment

Status
Not open for further replies.

at51178

Technical User
Mar 25, 2002
587
0
0
US
Hey guys
I believe I am writing this right but I get a else without if error message anyone can help me with this here is the code.

If optLastName.Value = 1 And OptFirstName.Value = 0 Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.LastName LIKE '*" & txtSearch & "*';"
Else
If optLastName.Value = 0 And OptFirstName.Value = 1 Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.FirstName LIKE '*" & txtSearch & "*';"
End If
 
After the second Else, bring the If right up next to the Else part of your code,

ElseIf .....etc.

This should do it.
 
I do not have a second else I am just getting an else without if error message
 
do not have a second else I am just getting an else without if error message

That's the key to your error message. Actually, I think the error message has it backwards - you have and IF without an ELSE...

End it like this:

Else
If optLastName.Value = 0 And OptFirstName.Value = 1 Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.FirstName LIKE '*" & txtSearch & "*';"
else
End If

to just fall through... Me? Ambivalent? Well, yes and no....
Another free Access forum:
More Access stuff at
 
What version of access? I don't think elseif exists in 2000 does it?

You are missing an End IF. You have two IF lines but only one End If.

Change it to:

If optLastName.Value = 1 And OptFirstName.Value = 0 Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.LastName LIKE '*" & txtSearch & "*';"
Else
If optLastName.Value = 0 And OptFirstName.Value = 1 Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.FirstName LIKE '*" & txtSearch & "*';"
End If
End if

Transcend
[gorgeous]
 
I did what you told me but I still get the same error message
here is the modified code that I used

If optLastName.Value = 1 Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.LastName LIKE '*" & txtSearch & "*';"
Else
If optLastName.Value = 0 Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.FirstName LIKE '*" & txtSearch & "*';"
Else
End If
End Sub
 
hey trancend I tried that too and it didn't work I am using access 2000 but I am currently working on a computer with xp on it but I get the error message on both versions
 
What code do you have now?

You are definately missing an 'end if'

you have two ifs and only one end. Adding an extra else won't make a difference.

Transcend
[gorgeous]
 
Try brackets also

change your code to

If (optLastName.Value = 1 And OptFirstName.Value = 0) Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.LastName LIKE '*" & txtSearch & "*';"
Else
If (optLastName.Value = 0 And OptFirstName.Value = 1) Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.FirstName LIKE '*" & txtSearch & "*';"
End If
End if

This compiled ok for me

Transcend
[gorgeous]
 
Sorry to be a bother but the code you gave me still gave me a same error message


Private Sub cmdSearch_Click()
If (optLastName.Value = 1 And OptFirstName.Value = 0) Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.LastName LIKE '*" & txtSearch & "*';"
Else
If (optLastName.Value = 0 And OptFirstName.Value = 1) Then Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.FirstName LIKE '*" & txtSearch & "*';"
End If
End If

End Sub

That is what I entered
 
Are you putting the "If something then do something" all on the one line?

I found that if I did then I got the error message.

But if you set it out like
if something then
do something
else
do something else
end if

instead of

if something then do something
else
end if

it will work .. i hope this makes sense. Basically go into your code and go to the end of the THEN and hit enter

Transcend
[gorgeous]
 
This is from microsoft:

If...Then...Else Statement
Conditionally executes a group of statements, depending on the value of an expression.

If condition Then statements [Else elsestatements ]
Or, you can use the block form syntax:

If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements]] . . .
[Else
[elsestatements]]
End If


So basically it either has to be all on one line, or in the correct block formation.

Transcend
[gorgeous]
 
Well the code worked fine for me when i formatted it properly.

Transcend
[gorgeous]
 
I just ran it again in access .. are you sure you formatted it correctly?

Transcend
[gorgeous]
 
*scratches head* now i'm bugging you :p Repaste your code for me one more time please :)

Transcend
[gorgeous]
 
OK HERE IT IS BUT I THINK IT MIGHT BE THE FORMAT
I CHANGED THE 1'S AND 0'S VALUE ON THE OPTLASTNAME AND FIRSTNAME TO EITHER TRUE OR FALSE THAT i KNOW IS RIGHT BUT NOW i GET
"YOU HAVE ENTERED AN EXPRESSION THAT HAS NO VALUE

Private Sub cmdSearch_Click()
If optLastName.Value = True Then
Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.LastName LIKE '*" & txtSearch & "*';"
ElseIf optLastName.Value = False Then
Me.RecordSource = "SELECT tblmain.LastName, tblmain.firstName " & "FROM tblmain " & "WHERE tblmain.FirstName LIKE '*" & txtSearch & "*';"
End If
End Sub
 
Put a stop in your code at this line:

If optLastName.Value = True Then
You do this by clicking on the grey bar next to the line of code you want to stop at. A Red circle should appear.

After you have done this, run it again, and this time the code should pause at the line you selected.

In the window at the bottom of your screen, type

?optLastName.value

and see what result it gives.

Transcend
[gorgeous]
 
Did you put a stop in the code?
Did the code pause and bring up the line you wanted when you ran it?
If it did a window called "immediate" window should appear

Transcend
[gorgeous]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top