带记录集的 VBScript If Instr

VBScript If Instr with a recordset

我正在尝试在 VB脚本中使用记录集执行 if 语句以执行 "like" 比较记录集。

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

如果我只使用 Td(rs("Next Backup")) & "12:00:00 AM" &_ 它工作正常,但如果我添加比较 If Instr(rs("Next Backup"), "*M") > 0 then Td(rs("Next Backup")) Else Td(rs("Next Backup")) & "12:00:00 AM" &_,我会得到一个 VB 编译错误。我不确定这个逻辑中的错误在哪里,或者我是否可以在 InStr 函数中使用记录集。我在这里做错了什么?

我使用多行时得到相同的结果:

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

您不能像在 VBScript 中那样内联条件。将您的代码更改为如下内容:

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