Re: Is there an easy way to tell if a field name exists ...



Dale wrote:

in a given table using VB6 and ADO, without cycling through all the fields
and testing to see if the field name is the one we're looking for? Thanks.

You can get metadata from INFORMATION_SCHEMA. For example, this VBScript
example checks if a specified column exists in a specified table. The VB6
code would be similar:

Option Explicit
Dim adoRecordset, strSQL, strConnect
Dim strName, strTable, strColumn

strTable = "Accounts"
strColumn = "AccountName"

strConnect = "DRIVER=SQL Server;Trusted_Connection=Yes;" _
& "DATABASE=MyDatabase;SERVER=MyServer\MyInstance"

Set adoRecordset = CreateObject("ADODB.Recordset")
adoRecordset.ActiveConnection = strConnect

strSQL = "SELECT * FROM PocketLunch.INFORMATION_SCHEMA.COLUMNS " _
& "WHERE TABLE_NAME = '" & strTable & "' " _
& "AND COLUMN_NAME = '" & strColumn & "'"

adoRecordset.Source = strSQL
adoRecordset.Open

If adoRecordset.EOF Then
Wscript.Echo "Column " & strColumn & " is NOT in table " & strTable
Else
Wscript.Echo "Column " & strColumn & " is in table " & strTable
End If

adoRecordset.Close
Set adoRecordset = Nothing

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


.