Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test for Column in table...

Status
Not open for further replies.

briglass

Programmer
Dec 4, 2001
179
GB
I would like my VB application to test to see if a column "Hello" exists in table "Names" within data.mdb

How can I do this?

THANKS

~Brian~

 
This is from the top of my head so you will need to check it and assumes that you are talking about the current database and using DAO.
Code:
Function IsField(sTbl as string, sField as string) as boolean
  Dim tdf as tabledef
  dim fld as field

  set tdf=currendb.tabledefs(sTbl)
  for each fld in tdf
    if fld.name=sfield then
      isField=true
      exit function
    end if
  next fld
end funciton

Hope this works and helps,
Rewdee

Does anyone know the code how to write this with ADO?
 
ADO code using schema. This is just a function I have handy, there are other ways of doing it.

Function TestTableColumn()

Dim cn As New Connection, cn2 As New Connection
Dim rs As Recordset, rs2 As Recordset
Dim connString As String
connString = "provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\bigtuna\Databases\Bank.mdb;" & _
"Persist Security Info=False"
cn.ConnectionString = connString
cn.Open connString
cn2.Open connString

Set rs = cn.OpenSchema(adSchemaTables, _
Array(Empty, Empty, Empty, "table"))

While Not rs.EOF
Debug.Print rs!table_name
Set rs2 = cn2.OpenSchema(adSchemaColumns, _
Array(Empty, Empty, "" & rs!table_name & ""))
While Not rs2.EOF
Debug.Print " " & rs2!column_name
rs2.MoveNext
Wend
rs.MoveNext
Wend
rs.Close
Set cn = Nothing

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top