If then within a string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When creating a string that will be printing to a flat file, in some
instances I nee to add criteria. In the following example, I'm trying to
replace the P with an I for the flat file only.

What I'd like string result to look like:
SAS|1|20050817|I|

Code would look like:
SAS = "SAS|1|" & Format$(CurrentDate, "yyyymmdd") & "|"
If rs.fields("type") = "P" Then
"I"
Else
rs.fields("type")
Endif


I can include more code if this doesn't make sense.
 
Sash said:
When creating a string that will be printing to a flat file, in some
instances I nee to add criteria. In the following example, I'm
trying to replace the P with an I for the flat file only.

What I'd like string result to look like:
SAS|1|20050817|I|

Code would look like:
SAS = "SAS|1|" & Format$(CurrentDate, "yyyymmdd") & "|"
If rs.fields("type") = "P" Then
"I"
Else
rs.fields("type")
Endif


I can include more code if this doesn't make sense.

There are a couple of ways you could do this. Using a separate If
statement, it might look like this:

SAS = "SAS|1|" & Format$(CurrentDate, "yyyymmdd") & "|"
If rs.fields("type") = "P" Then
SAS = SAS & "I"
Else
SAS = SAS & rs.fields("type")
End If

A more concise way of doing it might look like this:

SAS = "SAS|1|" & Format$(CurrentDate, "yyyymmdd") & "|" & _
IIf(rs.fields("type") = "P", "I", rs.fields("type"))
 
Just to add to Dirk Reply, to add "|" in the end of the string

SAS = "SAS|1|" & Format$(CurrentDate, "yyyymmdd") & "|"
If rs.fields("type") = "P" Then
SAS = SAS & "I"
Else
SAS = SAS & rs.fields("type")
End If
SAS = SAS & "|"

A more concise way of doing it might look like this:

SAS = "SAS|1|" & Format$(CurrentDate, "yyyymmdd") & "|" & _
IIf(rs.fields("type") = "P", "I", rs.fields("type")) & "|"
 
Back
Top