Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2008, 08:50 AM
Authorized User
 
Join Date: Oct 2006
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading fields with TableDefs

I have created code that reads the tables in my project and creates tables that contain information about the table structure.

It uses the following method to loop thru the objects...
    For Each tbl in db.TableDefs
         For Each fld In tbl.Fields

Then it uses the VarType function to return the integer indicating the subtype of each field.

The problem I have is that if the field has not been initialized yet (the table is empty) then it returns all the fields as type 9 (object), even though the field is specified as say a 'Long' in the table design.

Tried the TypeName function also and have a different problem if they are uninitialized.

How can I get the definitions of the types of fields in tables when the table is still empty??

Thankyou


 
Old April 7th, 2008, 09:03 AM
Authorized User
 
Join Date: Oct 2006
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Never mind I think I found something that I can use.....

Field Object, Fields Collection Example

This example shows what properties are valid for a Field object depending on where the Field resides (for example, the Fields collection of a TableDef, the Fields collection of a QueryDef, and so forth). The FieldOutput procedure is required for this procedure to run.

Sub FieldX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim fldTableDef As Field
    Dim fldQueryDef As Field
    Dim fldRecordset As Field
    Dim fldRelation As Field
    Dim fldIndex As Field
    Dim prpLoop As Property

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees")

    ' Assign a Field object from different Fields
    ' collections to object variables.
    Set fldTableDef = _
        dbsNorthwind.TableDefs(0).Fields(0)
    Set fldQueryDef =dbsNorthwind.QueryDefs(0).Fields(0)
    Set fldRecordset = rstEmployees.Fields(0)
    Set fldRelation =dbsNorthwind.Relations(0).Fields(0)
    Set fldIndex = _
        dbsNorthwind.TableDefs(0).Indexes(0).Fields(0)

    ' Print report.
    FieldOutput "TableDef", fldTableDef
    FieldOutput "QueryDef", fldQueryDef
    FieldOutput "Recordset", fldRecordset
    FieldOutput "Relation", fldRelation
    FieldOutput "Index", fldIndex

    rstEmployees.Close
    dbsNorthwind.Close

End Sub

Sub FieldOutput(strTemp As String, fldTemp As Field)
    ' Report function for FieldX.

    Dim prpLoop As Property

    Debug.Print "Valid Field properties in " & strTemp

    ' Enumerate Properties collection of passed Field
    ' object.
    For Each prpLoop In fldTemp.Properties
        ' Some properties are invalid in certain
        ' contexts (the Value property in the Fields
        ' collection of a TableDef for example). Any
        ' attempt to use an invalid property will
        ' trigger an error.
        On Error Resume Next
        Debug.Print " " & prpLoop.Name & " = " & _
            prpLoop.Value
        On Error GoTo 0
    Next prpLoop

End Sub







Similar Threads
Thread Thread Starter Forum Replies Last Post
reading and incrementing a value phantom3008 ASP.NET 1.0 and 1.1 Basics 0 April 18th, 2007 10:29 AM
Reading webcontrols shellhb ASP.NET 1.x and 2.0 Application Design 0 October 20th, 2005 01:02 PM
Reading Excel bhuvanagiri Classic ASP Components 0 May 2nd, 2005 02:24 AM
Using Forum fields select fields on the fly hellosureshkumar Crystal Reports 0 December 17th, 2004 08:20 AM
reading parameters skaya Crystal Reports 0 August 10th, 2004 09:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.