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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database Structure

Status
Not open for further replies.

cid

Programmer
Jan 4, 2001
18
US
How can I grab table/column info from the database using a query? I know how to do this in Oracle, but not MSA2k. Any help would be appreciated. Thanks. ~[ cid ]~

web // email // cid@cjd-web.com
aim // ciddivine
 
Sub GetField2Description()
' This procedure:
' (1) deletes and recreates a table (tblFields)
' (2) uses a query of (table) MSysObjects to
' get the names of all tables and linked tables in the
' database
' (3) Populates tblFields with info about each field

Dim db As DATABASE, td As TableDef
Dim rs As Recordset, rs2 As Recordset
Dim test As String, namehold As String
Dim typehold As String, SizeHold As String
Dim fielddescription As String, tname As String
Dim n As Long, i As Long, recis As Variant
Dim found As Boolean, prpNew As Property
Dim fld As Field, strSQL As String
n = 0
Set db = CurrentDb
' Trap for any errors.
On Error Resume Next
tname = "tblFields"
'Does table "tblFields" exist? If true, delete it;
found = False
test = db.TableDefs(tname).Name
If Err <> 3265 Then
found = True
docmd.DeleteObject acTable, &quot;tblFields&quot;
End If
'Create new tblTable
db.Execute &quot;CREATE TABLE tblFields(Object TEXT (55), FieldName TEXT (55), FieldType TEXT (20), FieldSize Long, FieldAttributes Long, FldDescription TEXT (20));&quot;

strSQL = &quot;SELECT MSysObjects.Name, MSysObjects.Type From MsysObjects WHERE&quot;
strSQL = strSQL + &quot;((MSysObjects.Type)=1)&quot;
strSQL = strSQL + &quot;ORDER BY MSysObjects.Name;&quot;

Set rs = db.OpenRecordset(strSQL)
If Not rs.BOF Then
' Get number of records in recordset
rs.MoveLast
n = rs.RecordCount
rs.MoveFirst
End If

Set rs2 = db.OpenRecordset(&quot;tblFields&quot;)

For i = 0 To n - 1
fielddescription = &quot; &quot;
Set td = db.TableDefs(i)
'Skip over any MSys objects
If Left(rs!Name, 4) <> &quot;MSys&quot; And Left(rs!Name, 1) <> &quot;~&quot; Then
namehold = rs!Name
found = False
On Error Resume Next
For Each fld In td.Fields
fielddescription = fld.Name
typehold = FieldType(fld.Type)
SizeHold = fld.size
rs2.AddNew
rs2!Object = namehold
rs2!FieldName = fielddescription
rs2!FieldType = typehold
rs2!FieldSize = SizeHold
rs2!FieldAttributes = fld.Attributes
rs2!FldDescription = fld.Properties(&quot;description&quot;)
rs2.Update
Next fld

Resume Next
End If
rs.MoveNext
Next i
rs.Close
rs2.Close
db.Close
End Sub

Function FieldType(intType As Integer) As String

Select Case intType
Case dbBoolean
FieldType = &quot;dbBoolean&quot;
Case dbByte
FieldType = &quot;dbByte&quot;
Case dbInteger
FieldType = &quot;dbInteger&quot;
Case dbLong
FieldType = &quot;dbLong&quot;
Case dbCurrency
FieldType = &quot;dbCurrency&quot;
Case dbSingle
FieldType = &quot;dbSingle&quot;
Case dbDouble
FieldType = &quot;dbDouble&quot;
Case dbDate
FieldType = &quot;dbDate&quot;
Case dbText
FieldType = &quot;dbText&quot;
Case dbLongBinary
FieldType = &quot;dbLongBinary&quot;
Case dbMemo
FieldType = &quot;dbMemo&quot;
Case dbGUID
FieldType = &quot;dbGUID&quot;
End Select
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top