Verify Field Name

Hello,

I would like to verify the validity of fieldname. For example, I have this code:

rs.Field("MyText").Value = "Test"

I would like to ask if “MyText” is an invalid field name does it return any error code?

to find out the field (column) names you will have to look at the table scheme. recordsets cant confirm of a field (column) exists or not. Or if it can, it is not in a way that I have been able to find.

Hello Scott,

Thanks for the answer.

You also can do this:

dim Rs as recordSet=myDB.SQLSelect(“Select * from myTable limit 0”)

dim fieldNameToCheck as string=“MyText”
dim fieldExists as boolean=false
for i=1 to rs.columncount
if Rs.idxfield(i).name=fieldName then
…//we found it
fieldExists=true
end if
next

I while ago I made my own database wrapper. I used a class that represents a record in a table. This class deals with saving, deleting and creating the record. This (base)record checks it’s properties using introspection and compares it with the optional recordset that is passed along in the constructor. A string array is populated with field names in lowercase. When checking if a field exists I simply use MyArray.indexOf( fieldname.lowercase.trim). If the result is -1, the field does not exist. The record class then adds the field to the table.
Finally, I subclass this record class to use it with other tables. The name of this subclass is the same as the table name in my database.
The fieldnames array for this table is being stored, for later use, in order not to redo this operation.

This way I never have to deal with building the database. It just makes it a lot easier, I think.

To test whether a field is in the recordset or not (it may be valid, but wasn’t requested), use

if rs.Field("MyText") <> Nil then
   // it's in the recordset
end

Tim! That’s brilliant! Simple and elegant!

I check for Nil all the time. But never used it to see if a field exists. I will use this one from now on. Thanks a billion!