Reade Data type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to "read" the data type from a field in a table?
I am writing code to document my DB and with

For Each field In rs.Fields...

can get a list of the fieldnames, but can I also get the datatype (and
number of characters for text fields as a bonus)?

Thanks so much!!
 
This will list the numeric value of the Type property. You'll need to modify
it if you want a textual description of the Type.

Public Sub ListType()

'Using DAO ...
Dim db As DAO.Database
Dim rstd As DAO.Recordset
Dim fldd As DAO.Field

Set db = CurrentDb
Set rstd = db.OpenRecordset("SELECT * FROM TheTable")
For Each fldd In rstd.Fields
Debug.Print fldd.Name, fldd.Type, fldd.Size
Next fldd
rstd.Close

'Using ADO ...
Dim rsta As ADODB.Recordset
Dim flda As ADODB.Field

Set rsta = New ADODB.Recordset
With rsta
.ActiveConnection = CurrentProject.Connection
.Open "SELECT * FROM TheTable"
For Each flda In .Fields
Debug.Print flda.Name, flda.Type, flda.DefinedSize
Next flda
.Close
End With

End Sub


--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
thanks! that does it.
Sam

Brendan Reynolds said:
This will list the numeric value of the Type property. You'll need to modify
it if you want a textual description of the Type.

Public Sub ListType()

'Using DAO ...
Dim db As DAO.Database
Dim rstd As DAO.Recordset
Dim fldd As DAO.Field

Set db = CurrentDb
Set rstd = db.OpenRecordset("SELECT * FROM TheTable")
For Each fldd In rstd.Fields
Debug.Print fldd.Name, fldd.Type, fldd.Size
Next fldd
rstd.Close

'Using ADO ...
Dim rsta As ADODB.Recordset
Dim flda As ADODB.Field

Set rsta = New ADODB.Recordset
With rsta
.ActiveConnection = CurrentProject.Connection
.Open "SELECT * FROM TheTable"
For Each flda In .Fields
Debug.Print flda.Name, flda.Type, flda.DefinedSize
Next flda
.Close
End With

End Sub


--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Back
Top