Solved

Data type of a table field

Posted on 2011-09-07
6
248 Views
Last Modified: 2012-05-12
Is there any way to find out the data type of a field of a table?

in the following code, how can I find out the data type of rs.fields(1).

Set cn = CurrentProject.Connection
    rs.CursorLocation = adUseClient
    
    sql = "SELECT * FROM Master WHERE ID>100 ORDER BY ID"
    rs.Open sql, cn, adOpenDynamic, adLockOptimistic

    With rs
        ......
        ......
        ......
        ......
    end with

Open in new window

0
Comment
Question by:MnInShdw
  • 2
  • 2
  • 2
6 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
Comment Utility
try this

refer

http://bytes.com/topic/access/answers/601180-find-field-data-type-vba
http://allenbrowne.com/func-06.html

 
Dim intNumberofFields As Integer, intFieldType As Integer, strTypeName As String 
  
Dim fld As Field, intCounter As Integer, strFieldName As String 
  
intNumberofFields = CurrentDb.TableDefs("tblEmployee").Fields.Count 
  
For intCounter = 0 To intNumberofFields - 1 
  strFieldName = CurrentDb.TableDefs("tblEmployee").Fields(intCounter).Name 
  intFieldType = CurrentDb.TableDefs("tblEmployee").Fields(intCounter).Type 
    Select Case intFieldType 
      Case 2    'Byte 
        strTypeName = "Byte" 
      Case 3    'Integer 
        strTypeName = "Integer" 
      Case 4    'Long 
        strTypeName = "Long" 
      Case 6    'Single 
        strTypeName = "Single" 
      Case 7    'Double 
        strTypeName = "Double" 
      Case Else 'Not a Number 
        strTypeName = "N/A" 
    End Select 
     Debug.Print Format(intCounter + 1, "00") & ") " & strFieldName & " - " & strTypeName 
Next intCounter

Open in new window

0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
How about this:

Dim cn, sql, rs As ADODB.Recordset
Dim tType
Set cn = CurrentProject.Connection
    rs.CursorLocation = adUseClient
   
    sql = "SELECT * FROM Master WHERE ID>100 ORDER BY ID"
    rs.Open sql, cn, adOpenDynamic, adLockOptimistic

    With rs
       tType = Choose(.Fields(1).Type, "Yes/N0", "Byte", "Integer", "Long", "Currency", "Single", "Double", "Date/Time", "Binary", "Text", "OLE Object", "Memo")
       Debug.Print tType
    End With

mx
0
 
LVL 1

Author Comment

by:MnInShdw
Comment Utility
mx:
your suggested code behaves strange:
.Fields(1).Type for Text Data types returns 202.

More over the indexes in Choose statement are shifted. I believe it's because the items in choose doesn't follow the following list.

Still I can't understand what this 202 is.

The code suggested by pratima_mcs works perfect.
Since I prefer to work with recordset., I'll wait for some more suggestions.
Otherwise I will accept pratima_mcs' code.


thank you
vbEmpty 0 (uninitialized) 
vbNull 1(no valid data) 
vbInteger 2 
vbLong 3
vbSingle 4 
vbDouble 5 
vbCurrency 6
vbDate 7 
vbString 8 
vbObject 9 
vbError 10
vbBoolean 11 
vbVariant 12 
vbDataObject 13 
vbDecimal 14 
vbByte 17 
vbLongLong 20 
vbUserDefinedType 36 
vbArray 8192

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
Glad to help you
0
 
LVL 1

Author Closing Comment

by:MnInShdw
Comment Utility
Million Thanks
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
I got the Types from the image below - on Allen's site.  If you are using the solution above, then you are missing several data types, including Text!  The Indexes in the Choose statement directly correspond to the Types shown in the image below.

mx
Capture1.gif
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now