简体   繁体   中英

vbscript: test for existence of a column in a recordset

Bah, vbscript.

I'm trying to figure out how to get this statement to work:

if (not rsObject("columnNameThatDoesntExist") is nothing) then 
' do some stuff
end if
' else do nothin

Where rsObject is a RecordSet and columnNameThatDoesntExist is ... well you know. I'm looking for something like rsObject.Columns.Contains(string). But of course can't find it.

Edit: Looks like looping rsObject.Fields is an option, is that the only way to do this?

I am sure you have your reasons, but if you don't know what fields you are calling back from your database, you could always use On Error Resume Next and On Error Goto 0 to ignore the tossed error. Seems like a bad way to me, but it would work

blnItWasntThere = True
On Error Resume Next
If (rsObject("columnNameThatDoesntExist") <> "") Then 
  blnItWasntThere = False
  ...
  ...
  ...
End If
On Error Goto 0

If blnItWasntThere Then
'handle this error'
End If

But with that said, I think you would be more worried about the mystery recordset you are getting back.

Or make your own function

Function ColumnExists(objRS, Column)
  Dim blnOutput, x
  blnOutput = True
  On Error Resume Next
  x = objRS(Column)
  If err.Number <> 0 Then blnOutput = False
  On Error Goto 0
  ColumnExists = blnOutput
End Function

Either looooop and check if it's there, or just try to grab it:

Dim oRs:Set oRs = Nothing
On Error Resume Next
Set oRs = rsObject("columnNameThatDoesntExist")
On Error Goto 0
If Not rsObject("columnNameThatDoesntExist" Is Nothing Then 
    ' ...
End If

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