Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a names field exists in a record set

Tags:

asp-classic

Is it possible to check if a named field is within a record set?

EG id, field1, field2, field3 have been selected. Is it possible for VBScript to detect if field2 has been selected. I am also hoping this is possible without looping

Please assume I dont know, nor can see the actual SELECT. I need to detect this after the query has been executed.

This is how its done using a loop, I am also hoping this is possible without looping:

dim rs,field,foundField
sql = "SELECT * from table;"
set rs = conn.execute(sql)
For Each field in rs.Fields
   if field.Name = "someFieldName" then 
      foundField = true 
  exit for
   else 
      foundField = false
   end if
next

TYIA

like image 812
Mat41 Avatar asked Feb 16 '23 14:02

Mat41


1 Answers

I use a similar function (in VB6) to the one proposed by bfavaretto... I'm curious why the OP says it doesn't work?

Public Function FieldExists(ByVal rs As Recordset, ByVal fieldName As String) As Boolean

    On Error GoTo merr

    FieldExists = rs.Fields(fieldName).name <> ""
    Exit Function

merr:
    FieldExists = False

End Function

This function works for me... and it doesn't return false negatives as far as I know. Also, it appears to be faster than executing a loop for fields that are included in the collection; for fields that are actually missing, the execution times of both methods seems to be equivalent.


EDIT

For VBScript, the above function would look like this:

Function FieldExists(ByVal rs, ByVal fieldName) 

    On Error Resume Next
    FieldExists = rs.Fields(fieldName).name <> ""
    If Err <> 0 Then FieldExists = False
    Err.Clear

End Function

And the code posted in the question would look like:

dim rs,field,foundField
sql = "SELECT * from table;"
set rs = conn.execute(sql)
foundField = FieldExists(rs, "someFieldName")
like image 172
ferc Avatar answered Feb 23 '23 04:02

ferc