Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vbscript: test for existence of a column in a recordset

Tags:

vbscript

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?

like image 217
jcollum Avatar asked Feb 04 '09 22:02

jcollum


1 Answers

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
like image 177
MrChrister Avatar answered Sep 30 '22 19:09

MrChrister