简体   繁体   中英

VBScript If Instr with a recordset

I am trying to do an if statement with a recordset within VBScript to do a "like" compare a recordset.

While Not rs.EOF
    result = result & "<tr style='" & FontStyle(FontFamily, FontSize) & _
             " background-color: " & RowColors(number mod 2) & "'>" &_
             Tdc(number) & Td(rs("Name")) & Tdc(rs("Machine Type")) & _
             Tdc(ChooseStatusColor(rs("Backup Status"))) & _
             Tdc(rs("Backup State")) & Td(rs("Last Backup Start")) & _
             Td(rs("Last Backup End")) & _
             If InStr(rs("Next Backup"), "*M") > 0 Then Td(rs("Next Backup")) Else Td(rs("Next Backup")) & "12:00:00 AM" & _
             End If &_
             "</tr>"
    rs.MoveNext
    number = number + 1
Wend

If I use just Td(rs("Next Backup")) & "12:00:00 AM" &_ it works fine but if I add the compare If Instr(rs("Next Backup"), "*M") > 0 then Td(rs("Next Backup")) Else Td(rs("Next Backup")) & "12:00:00 AM" &_ , I get a VB compilation error. I am not sure where the error is in this logic or if I can even use a recordset in an InStr function. What am I doing wrong here?

I get the same result when I use multiple lines:

While Not rs.EOF
    result = result & "<tr style='" & FontStyle(FontFamily, FontSize) & _
             " background-color: " & RowColors(number mod 2) & "'>" & _
             Tdc(number) & Td(rs("Name")) & Tdc(rs("Machine Type")) & _
             Tdc(ChooseStatusColor(rs("Backup Status"))) & _
             Tdc(rs("Backup State")) & Td(rs("Last Backup Start")) & _
             Td(rs("Last Backup End")) & _
             If InStr(1, (rs("Next Backup")), "*M") > 0 Then & _
             Td(rs("Next Backup")) & _
             Else Td(rs("Next Backup")) & "12:00:00 AM" & _
             End If &_
             "</tr>"
    rs.MoveNext
    number = number + 1
Wend

You can't inline conditionals like that in VBScript. Change your code to something like this:

While Not rs.EOF
    result = result & "<tr style='" & FontStyle(FontFamily, FontSize) & _
             " background-color: " & RowColors(number mod 2) & "'>" & _
             Tdc(number) & Td(rs("Name")) & Tdc(rs("Machine Type")) & _
             Tdc(ChooseStatusColor(rs("Backup Status"))) & _
             Tdc(rs("Backup State")) & Td(rs("Last Backup Start")) & _
             Td(rs("Last Backup End"))
    If InStr(rs("Next Backup"), "M") > 0 Then
        result = result & Td(rs("Next Backup"))
    Else
        result = result & Td(rs("Next Backup") & "12:00:00 AM")
    End If
    result = result & "</tr>"
    rs.MoveNext
    number = number + 1
Wend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM