Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Determine if Field Exists in Recordset

How can I determine if a field is part of a recordset?

I don't want to loop through the Fields collection, or use ON ERROR.

Example:

rstMyTable.open "SELECT * FROM MyTable", objConn

If the field "Myfield" does not exist:

IsObject(rstMyTable.Fields("Myfield"))     'returns error 3265
IsNull(rstMyTable("Myfield"))               'returns error 3265

Any ideas?
0
apollois
Asked:
apollois
  • 3
  • 2
  • 2
  • +2
1 Solution
 
shanesuebsahakarnCommented:
Why are you unable to loop through the collection or error trap ?

Also, under what circumstances do you need to test for the field's existence ?
0
 
apolloisAuthor Commented:
shanesuebsahakarn,

>>>Why are you unable to loop through the collection or error trap ?

Constraints given to me.


>>>Also, under what circumstances do you need to test for the field's existence ?

I have a custom DLL which returns a recordset.  It returns different fields depending on the criteria.  I do not have access to the DLL or it's source code.




Best Regards,
>apollois<
0
 
MoliereCommented:
Use:
ON ERROR RESUME NEXT
then assign isnull(rstMyTable.fields("Myfield")) to a variable.
Check for an error 3265. If it is there, then the field does not exist. If the ERR=0, then the field exists.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MoliereCommented:
Sorry, I missed the part about not using ON ERROR
0
 
apolloisAuthor Commented:
Moliere,

Please re-read my question:
I don't want to loop through the Fields collection, or use ON ERROR.


Best Regards,
>apollois<
0
 
shanesuebsahakarnCommented:
Hmm, well under these circumstances, I know of no way that it can be done.

Neither IsObject or IsNull will work since both of these require valid Variant references in the first place.

However, I am not really conversant enough with ADO. Conceivably, the line:
rstMyTable.open "SELECT " & MyField & " FROM MyTable", objConn

might return something you could check for, but I suspect that it would be a parameter error which you would need to trap.
0
 
HermeticCommented:
I am not sure of your situation, but if you are trying to determine which of two nearly identical tables you are selecting from, save MyField, then you could possibly use a count of the fields to tell if it is there.
0
 
DrTechCommented:
I don't see any reason NOT to loop through the fields collection. It takes almost no time, and is the only solution I can think of.
0
 
apolloisAuthor Commented:
I think you're right.  The only options are check the Fields collection or use ON ERROR.

This is what I thought, but others were insisting there must be another way.

Thanks to all for confirming this.  Shane was first, so he gets the points.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now