tags:

views:

1394

answers:

6

Hi,

I have a database with many tables. I am looking for a field which may or may not exist in one or many of the tables. How do i check if it exists or not. (without querying each table of course). It is an access database

Thanks tksy

A: 

MSSQL Looking for ADDRESS1 column: select so.name from sysobjects so where so.id in (select sc.id from syscolumns sc where name like 'ADDRESS1')

ORACLE http://en.wikipedia.org/wiki/Oracle_metadata

Google will find the syntax for other DBs...

ericp
The database is ms-access. It says so in the original post.
Remou
A: 

What type of database is it? If it's SQL Server you can try this:

SELECT * FROM sysobjects WHERE xtype = 'U' AND name = 'myTable'

But since it's the column you're looking for and not a table (thanks Brian), try this:

SELECT 
    DISTINCT
    so.[name] AS 'Table',
    sc.[name] AS 'Column' 
    FROM 
     syscolumns sc
    JOIN
     sysobjects so
     ON
      so.id = sc.id
    WHERE 
     sc.[name] = 'myTable'
Paul Mitchell
He's looking for a column not a table. Close though.
Brian Rudolph
Good point. Edited to reflect that fact.
Paul Mitchell
The database is ms-access. It says so in the original post.
Remou
No, it says so in the modified post. The original post did not say so.
Paul Mitchell
A: 

Here's how you access the table schema in MS Access using VBScript:

TestData = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=c:\somefolder\YOURDB.mdb"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open TestData

Set rs = Conn.OpenSchema(4)

do until Rs.eof
    tn = RS("TABLE_NAME")
    fn = RS("COLUMN_NAME")
    ' your script goes here
loop
Diodeus
There is a column schema.
Remou
+2  A: 

There is a schema for fields:

Set cn = CurrentProject.Connection

SelectFieldName = "SomeFieldName" 

'Get names of all tables that have a column = SelectFieldName '
Set rs = cn.OpenSchema(adSchemaColumns, _
Array(Empty, Empty, Empty, SelectFieldName))

From: http://stackoverflow.com/questions/277340/ms-access-how-to-bypasssuppress-an-error#277612

Remou
+2  A: 

Here's what I would do if I wanted to see if a particular column (identified in strSearch) in a particular table.

Public Sub search()

Dim db As Database
Dim strSearch As String
Dim strSQL As String
Dim rsResults As Recordset
Dim i As Integer
Dim cols As Integer

    strSearch = "a3"

    Set db = CurrentDb
    strSQL = "select * from bar"
    Set rsResults = db.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
    If Not rsResults.BOF Then
        rsResults.MoveFirst
    End If

    cols = rsResults.Fields.Count - 1 ' -1 because we start counting cols at 0
    For i = 0 To cols
        If rsResults(i).Name = strSearch Then
            MsgBox "Found the seach string"
        End If
    Next
    MsgBox "end of script"

End Sub

Now I know you don't want to write one of those for each table. So the next thing to do would be to loop through all the tables. You can find a list of all the tables with the following SQL

SELECT 
    name
FROM 
    MSysObjects 
WHERE 
    (Left([Name],1)<>"~") 
    AND (Left([Name],4) <> "MSys") 
    AND ([Type] In (1, 4, 6))

Connecting these two pieces up together, I'll leave as an exercise for the student :)

CodeSlave
There is an easier way using ADO Schemas
Remou
But if the code is running in Access that requires an additional library reference (unless you use late binding, I guess). The context in which the code will run was not made clear by the original questioner. To me, DAO is the obvious first choice for Jet data if you're working in Access itself.
David-W-Fenton
my ', _' trick is just a formatting rule for variable declaration. Makes things more readable. You can also use the same trick when enumerating a function's parameters. Another 'school' is www.stackoverflow.com/questions/166657/do-you-have-your-own-dnutndt-do-not-use-thisnever-do-that-list#166717
Philippe Grondier
Yes, I often do that with functions where the parameters that extend it beyond 80 characters. I'm still inclined to do a separate dim on each line. I was just hoping there was some noticeable advantage to it (like execution speed), but each to his own.
CodeSlave
"To me, DAO is the obvious first choice for Jet data". But in this particular case surely the ADO solution is preferable because OpenSchema returns a recordset consisting only of matches. The DAO involves a lot of looping to find the matches yourself. The extra reference is not expensive!
onedaywhen
+1  A: 

If you really do not want to open any table, a solution is to use the tabledefs collection of the database object. Each tabledef item has its own fields collection that you can browse. It would give something like that:

Public function findFieldInDatabase(p_myFieldName)
dim db as database, _
    tb as tabledef, _
    fd as field

set db = currentDb
for each tb in db.tabledefs
    for each fd in tb.fields
        if fd.name = p_myFieldName then
            debug.print tb.name, fd.name
        end if
    next fd
next tb
set fd = nothing
set tb = nothing
set db = nothing
end function

This code could be easily adapted to accept an optional p_myTableName as an argument to limit the search to a table/range of tables.

Philippe Grondier
Philippe, Why do you do that ", _" trick with your dim's? Is there some huge advantage over multiple dim's?
CodeSlave