是否从recordset.field.type属性获取ADO数据类型的名称? [英] Get the name of an ADO data type from the recordset.field.type property?

查看:115
本文介绍了是否从recordset.field.type属性获取ADO数据类型的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Access表中产生一个字段列表(名称,类型和大小).这个简单的VB代码为我几乎提供了我所需的东西:

I need to produce a list of the fields (name, type and size) from an Access table. This simple VB code gives me almost what I need:

Set rs = CurrentDb.OpenRecordset("myTable")
For x = 0 To rs.Fields.Count - 1
    Print #1, rs.Fields(x).Name & vbTab & rs.Fields(x).Type & vbTab & rs.Fields(x).Size
Next 

但是,类型"当然是​​诸如"10"之类的数字常量,而不是诸如"Varchar"之类的数字常量.

However the "Type" of course is a numeric constant like "10" instead of something like "Varchar".

我正要这样做:

Select Case rs.Fields(x).Type
  Case adChar
    fieldType = "adChar"
  Case adInteger
    fieldType = "adInteger"
  Case adDouble
ETCETERA....

但是我想知道是否有更好的方法,例如DataTypeEnum.FindName(Type)之类的东西?

But I wonder if there's a better way, something like DataTypeEnum.FindName(Type) or something?

如果这是一个愚蠢的问题",请提前道歉,但是我不是每天都在VB工作,Google对此没有给出明确的答案.

Apologies in advance if this is a "stupid question", but I don't work in VB every day and Googling has produced no clear answer on this.

推荐答案

问题(上面)下的注释中的答案非常好,不幸的是,它们归结为您不能从这里到达那里".没有简单的方法可以在字段中获取包含的数据类型.我能找到的最接近的方法是为了快速清除n:

The answers in the comments under the question (above) are excellent, unfortunately they boil down to "you can't get there from here." There is no simple way to get the data type contained in a Field. The closest I was able to come was to do this for the quick-n-dirty:

Function Scripting()
    Dim rs As DAO.Recordset
    Dim ff As String
    Dim fieldType As String
    ff = "c:\Users\me\Desktop\Structure.txt"
    Open ff For Output As #1
    Set rs = CurrentDb.OpenRecordset("myTable")
    For x = 0 To rs.Fields.Count - 1
        Print #1, rs.Fields(x).Name & vbTab & TypeName(rs.Fields(x).Value) & vbTab & rs.Fields(x).Size
    Next
    Close #1
    rs.Close
    Set rs = Nothing
End Function

不建议您访问艾伦·布朗(Allen Browne)的出色页面,并抓住他的FieldTypeName( )功能以寻求更好的解决方案.

You would be well advised go to Allen Browne's excellent page and grab his FieldTypeName() function for a better solution.

感谢所有回答答案的人(技巧,黑鹰,克里斯·罗里斯顿和蒂姆·威廉姆斯).

Thanks to everyone (mehow, blackhawk, Chris Rolliston and Tim Williams) that chipped in on the answer.

这篇关于是否从recordset.field.type属性获取ADO数据类型的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆