PDA

View Full Version : if statement to big!


paulafernandes
12-23-2002, 12:21 PM
Hi!
I have a really simple question for you, but I'm not getting it...

I have an If statement that's really big. And I want to divide it in 3 or 4 lines.
What do I use to link the lines? I thought it was "&_" but it doesn´t work...

Thank's!!!
Paula

webmarkart
12-23-2002, 03:06 PM
here is a sample, hope this helps:


sql = ""
sql = sql & "SELECT ContactID,FirstName,LastName,Address,City,State,PostalCode, "
sql = sql & "CompanyName,Title,WorkPhone,WorkExtension,HomePhone, "
sql = sql & "MobilePhone,FaxNumber,EmailName,BirthDate,ContactTypeID, "
sql = sql & "Notes,SpouseName,ChildrenNames "
sql = sql & "FROM CONTACTS "
sql = sql & "WHERE ContactID IS NOT NULL "
If FirstName <> "" Then
sql = sql & "AND FirstName LIKE '" & FirstName & "%' "
End If
If LastName <> "" Then
sql = sql & "AND LastName LIKE '" & LastName & "%' "
End If
If City <> "" Then
sql = sql & "AND City LIKE '" & City & "%' "
End If
If State <> "" Then
sql = sql & "AND State LIKE '" & State & "%' "
End If
If PostalCode <> "" Then
sql = sql & "AND PostalCode LIKE '" & PostalCode & "%' "
End If
If HomePhone <> "" Then
sql = sql & "AND HomePhone LIKE '" & HomePhone & "%' "
End If
If TypeId <> "" Then
sql = sql & "AND ContactTypeID = " & TypeId & " "
End If
sql = sql & "ORDER BY LastName,FirstName "
'response.write sql
Set rs = oConn.Execute(sql)

BigDaddy
12-23-2002, 04:29 PM
The underscore " _ " should be the last character on the line. You only need the underscore, and make sure you put a space in front of it. The server will then consider the next line as part of that line.

Example:


IF name <> "boo" or name <> "bah" _
or name <> bam then

......

else

......

end if

whammy
12-24-2002, 12:40 AM
BigDaddy nailed it - you have to have a space before the underscore.

An ampersand (&) shouldn't be used unless you are concatenating, in which case it doesn't matter which line it's on. Because of another developer (and the 70 or so sites that I've been staring at for the past year almost) I'm used to looking at lines like:

If someboolean = True AND _
(someotherboolean = True OR _
someadditionalboolean = True) Then ' Note you can still group conditions that are on separate lines...
' Do something
Else
' Do something else
End If


It can also be done just like webmarkart demonstrated... here are two ways:

mystring = "Mary had a " & _
"little lamb " & _
"whose fleece was " & _
"white as snow..."

mystring = "Mary had a "
mystring = mystring & "little lamb "
mystring = mystring & "whose fleece was "
If fleece = "white" Then
mystring = mystring & "white as snow... "
Else
mystring = mystring & mycolor & "..."
End If

webmarkart's demonstration of the latter technique should help you understand how to include variables in your string using conditions, even if it's something as complex as a SQL statement.

Hope this helps! :)

paulafernandes
12-26-2002, 09:28 AM
Hi! What I was looking for was the "_".

Thank's for that and all the other help...

Merry Christmas!!!
Paula