Breaking News

Sunday, June 24, 2012

Ex19: VBA - Read table records using DAO

How to read the field names and types from a table or query.

Option Compare Database
Option Explicit

'Constants for examining how a field is indexed.
Private Const intcIndexNone As Integer = 0
Private Const intcIndexGeneral As Integer = 1
Private Const intcIndexUnique As Integer = 3
Private Const intcIndexPrimary As Integer = 7


Function ShowFieldsRS(strTable)
'Purpose: How to read the field names and types from a table or query.
'Usage: Call ShowFieldsRS("Table1")
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim strSql As String

strSql = "SELECT " & strTable & ".* FROM " & strTable & " WHERE (False);"
Set rs = DBEngine(0)(0).OpenRecordset(strSql)
For Each fld In rs.Fields
Debug.Print fld.Name, FieldTypeName(fld), "from " & fld.SourceTable & "." & fld.SourceField
Next
rs.Close
Set rs = Nothing
End Function

No comments:

Post a Comment

Designed By Published.. Blogger Templates