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

How to avoid a trailing comma is a list of written items 1

Status
Not open for further replies.

BigRed1212

Technical User
Mar 11, 2008
550
US
Hi,

I have an application that writes out records in a recordset that meet a criterion; specifically for each soccer game in recordset1 (rs) it writes out the ident of the goal scorers from recordset2 (rs2) for that game.

The control structure I have is:

...[Do while not rs.Eof followed by some response.writes for the game information]...

Do while not rs2.Eof
if rs2("lineid") = no then
response.write rs2("ident") & ", "
End If
rs2.movenext
Loop

...[Loop back the outer loop and close everything up when done]...

where no is a counter. This works great except that it has a trailing comma. If I have 4 values for ident that need to be written they will write out as

2, 3, 4, 6,

I think

2, 3, 4, 6

would look nicer.

I tried to peek ahead in rs2 to see whether I need the comma using:

Do while not rs2.Eof
If rs2("lineid") = no then
response.write rs2("ident")
rs2.movenext
If rs2("lineid") = no then
response.write ", "
End If
rs2.moveprevious
End If
rs2.movenext
Loop

but it throws an error on the moveprevious saying it isn't allowed there.

Got an idea for a better control structure that I can use to ditch the final comma?

TIA.
 
Code:
SomeVariableName = ""

Do while not rs2.Eof
  if rs2("lineid") = no then
    SomeVariableName = SomeVariableName & rs2("ident") & ", "
  End If
  rs2.movenext
Loop

' Remove trailing space
SomeVariableName = Trim(SomeVariableName)

' Remove last comma.
If Right(SomeVariableName, 1) = "," Then
  SomeVariableName = Left(SomeVariableName, Len(SomeVariableName)-1)
End If
response.write SomeVariableName

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
That works a treat. Thanks.

So don't fret about only writing it when it is needed or at just the correct time, simply get the output out of the recordset and then clean it up.

Probably a general programming principle in there somewhere.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top