Display IF (Classic ASP/VB Script)

I have the code below, which I show on a results table.

<%=(rs_contacts.Fields.Item("Street").Value)%> , <%=(rs_contacts.Fields.Item("city").Value)%>, <%=(rs_contacts.Fields.Item("state").Value)%>

In between the address it displays commas, but I only want it displayed IF there is an actual value for the item to the right of the | or comma, otherwise it should not show up, this is because if there is no address entered it shows two commas in the field, not really nice and clean.

Help is appreciated  :)
LVL 1
AleksAsked:
Who is Participating?
 
Paul MacDonaldDirector, Information SystemsCommented:
<%=(rs_contacts.Fields.Item("Street").Value)%> <%=IF(IsNull(rs_contacts.Fields.Item("city").Value), "", ", ") %> <%=(rs_contacts.Fields.Item("city").Value)%> <%=IF (IsNull(rs_contacts.Fields.Item("state").Value), "", ", ") %> <%=(rs_contacts.Fields.Item("state").Value)%>
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
i would first build the output, then write it out to the screen:

dim street, city, state, fullAddress
street = rs_contacts.Fields.Item("Street").Value
city = rs_contacts.Fields.Item("city").Value
state = rs_contacts.Fields.Item("state").Value

fullAddress = street
if city <> "" then     fullAddress = fullAddress & ", " & city
if state <> "" then  fullAddress = fullAddress &, ", " & state

Response.Write fullAddress
%>

Open in new window

0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
<%=doComma(rs_contacts.Fields.Item("Street").Value)&doComma(rs_contacts.Fields.Item("city").Value )&rs_contacts.Fields.Item("state").Value%>

function doComma(data)
  doComma = ""
  if data<>"" then
      doComma=data&", "
  end if
end function

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
If there is no state value you run the risk of a trailing comma with the solution above
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
That is a valid point.  Simply create a new function that removes the last character including watching for trailing spaces.  There could be multiple uses for both functions so let's put that in a file called functions.asp and include the functions.asp file in all of your pages.  This way you will only need to write the function code once but it will be available to you in many places.  This is much more efficient than writing specific code when you should be able to reuse it.


<%
' Assume you place a functions.asp file in a folder called includes.  Place on top of every page.
%>
<!-- #include virtual="/includes/functions.asp" -->  
<%=removeLastCharacter(doComma(rs_contacts.Fields.Item("Street").Value)&doComma(rs_contacts.Fields.Item("city").Value )&rs_contacts.Fields.Item("state").Value)%>

Open in new window


FUNCTIONS.ASP
function doComma(data)
  doComma = ""
  if data<>"" then
      doComma=data&", "
  end if
end function

function removeLastCharacter(data)
    removeLastCharacter=""
    if data<>"" then
		data=trim(data) ' watch for trailing spaces
        removeLastCharacter = left(data,len(data)-1)
    end if
end function

Open in new window

0
 
AleksAuthor Commented:
Sweet !  yes, I am going to include in my scripts.asp which has an include file at the top of every page.
Now, would this delete the comma and spaces on the right of the value to which I apply the script ?  or the left of it ?
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
To the right

x= " 1 2 3 , "  ' 8 characters including space in front and back space
data=trim(x) 
response.write x '   x will now be  "1 2 3," and only 6 characters because we removed the front and back spaces
x=left(x,len(x)-1) ' len(x) will be 6 and subtract 1 will be 5.  Take the first 5 characters will be "1 2 3"
response.write x ' the proof

Open in new window

0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
If you only want to remove space from the left or right, instead of trim you can use ltrim to remove the left most spaces or rtrim to remove the right most spaces.
0
 
AleksAuthor Commented:
Thanks !
0
 
AleksAuthor Commented:
Thanks for the great follow up to both.
0
 
AleksAuthor Commented:
paul:  I tried your solution but got an error:

<%=(rs_contacts.Fields.Item("Street").Value)%> <%=IF(IsNull(rs_contacts.Fields.Item("city").Value), "", ", ") %> <%=(rs_contacts.Fields.Item("city").Value)%> <%=IF (IsNull(rs_contacts.Fields.Item("state").Value), "", ", ") %> <%=(rs_contacts.Fields.Item("state").Value)%> </span>

Error:

Microsoft VBScript compilation  error '800a03ea'

Syntax error

/BlueDot/Intranet/reports/standard/rp_contacts_02_results.asp, line 299
Response.Write(IF(IsNull(rs_contacts.Fields.Item("city").Value), "", ", "))
0
 
AleksAuthor Commented:
I tried Scott's function but got this error:

Microsoft VBScript runtime  error '800a000d'

Type mismatch: 'doComma'

/BlueDot/Intranet/reports/standard/rp_contacts_02_results.asp, line 299


This is the included function:

<script type="text/javascript">
function doComma(data)
  doComma = ""
  if data<>"" then
      doComma=data&", "
  end if
end function
</script>
0
 
AleksAuthor Commented:
Never mind. it had javascript thanks for the help to everyone !
0
 
AleksAuthor Commented:
There is a problem, the remove last character script is removing the last character regardless of what it is, for example the state shows as "F" instead of "FL". It should only remove the last character IF the last character IS a comma. Can this be fixed ?  This is the script:



function removeLastCharacter(data)
    removeLastCharacter=""
    if data<>"" then
            data=trim(data)
        removeLastCharacter = left(data,len(data)-1)
    end if
end function
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I'm sorry, we need to exclude the last field from the remove last character.

change

<%=removeLastCharacter(doComma(rs_contacts.Fields.Item("Street").Value)&doComma(rs_contacts.Fields.Item("city").Value )&rs_contacts.Fields.Item("state").Value)%>

to

<%=removeLastCharacter(doComma(rs_contacts.Fields.Item("Street").Value)&doComma(rs_contacts.Fields.Item("city").Value ))&rs_contacts.Fields.Item("state").Value%>
0
 
AleksAuthor Commented:
Still another issue, the address shows:

1110 University Drive, Ft LauderdaleFL

it is removing the comma between the city and the state since its the last character  :$
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
<%=makeAddress(doComma(rs_contacts.Fields.Item("Street").Value)&doComma(rs_contacts.Fields.Item("city").Value ),rs_contacts.Fields.Item("state").Value)%>

Actually, since we are adding the state, lets switch up the function.



function makeAddress(address, state)
    if len(address)>0 and len(state)>0 then
           makeAddress=address&","&state
           else
           makeaddress=address&state 'at least one of these is blank so no comma
   end if

end function


function doComma(data)
  doComma = ""
  if data<>"" then
      doComma=data&", "
  end if
end function

function removeLastCharacter(data)
    removeLastCharacter=""
    if data<>"" then
		data=trim(data) ' watch for trailing spaces
        removeLastCharacter = left(data,len(data)-1)
    end if
end function

Open in new window

0
 
AleksAuthor Commented:
It shows commas after the street when there is no city or state in the db. Attached examples of addresses.
Capture.PNG
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I'm sorry, it would probably be beter to test this with data.  

Change the function for makeAddress
	

response.write makeAddress(doComma(rs_contacts.Fields.Item("Street").Value)&doComma(rs_contacts.Fields.Item("city").Value ),rs_contacts.Fields.Item("state").Value)


function makeAddress(address, state)
	if len(address)>0 AND len(state)>0 then
		makeaddress=address&state
	end if
	if len(address)>0 AND len(state)=0 then
		makeaddress=removeLastCharacter(address)
	end if
	if len(address)=0 AND len(state)>0 then
		makeaddress=state
	end if
	if len(address)=0 AND len(state)=0 then
		makeaddress=""
	end if

end function


function doComma(data)
  doComma = ""
  if data<>"" then
      doComma=data&", "
  end if
end function

function removeLastCharacter(data)
    removeLastCharacter=""
    if data<>"" then
		data=trim(data) ' watch for trailing spaces
        removeLastCharacter = left(data,len(data)-1)
    end if
end function

Open in new window


Here is my test
<%

dim array(17)
array(0)="street"
array(1) =""
array(2) = ""


array(3) =""
array(4) =""
array(5) = ""

array(6) = "123 street #123"
array(7) = "some city"
array(8) = "fl"

array(9) = "123 street #123"
array(10) = ""
array(11) = "fl"

array(12) = ""
array(13) = "some city"
array(14) = "fl"

array(15) = "123 street #123"
array(16) = "some city"
array(17) = ""


for x = 0 to 17 step 3
	response.write makeAddress(doComma(array(x)) & doComma(array(x+1)),array(x+2)) &"<hr>"
next
	
	
function makeAddress(address, state)
	if len(address)>0 AND len(state)>0 then
		makeaddress=address&state
	end if
	if len(address)>0 AND len(state)=0 then
		makeaddress=removeLastCharacter(address)
	end if
	if len(address)=0 AND len(state)>0 then
		makeaddress=state
	end if
	if len(address)=0 AND len(state)=0 then
		makeaddress=""
	end if

end function


function doComma(data)
  doComma = ""
  if data<>"" then
      doComma=data&", "
  end if
end function

function removeLastCharacter(data)
    removeLastCharacter=""
    if data<>"" then
		data=trim(data) ' watch for trailing spaces
        removeLastCharacter = left(data,len(data)-1)
    end if
end function


%>

Open in new window

The results http://mypadas.com/ee/Q_28640985.asp
0
 
AleksAuthor Commented:
Looks great thanks !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.