tags:

views:

394

answers:

2

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?

A: 

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 = False
  On Error Resume Next
  x = objRS(Column)
  If err.Number <> 0 Then blnOutput = True
  On Error Goto 0
  ColumnExists = blnOutput
End Function
MrChrister
Nah, I know what columns are coming back, but I'm doing some weird stuff on the backend to deal with a set of columns that might grow in the future, then pulling values for those column names. It's kludgy, arcane and ugly. But can't change it enough to make it right.
jcollum
Have you looked into GetRows? rs.GetRows will return an array that you can whip through. I used it with a bunch of Dim variables, but I guess you could use the index like magic numbers? http://www.w3schools.com/ado/met_rs_getrows.asp
MrChrister
Actually this works for what I need, since this bit of code is going to disappear in about an hour (proof of concept sort of thing, doesn't need to work right)
jcollum
A: 

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
svinto