vba 从 recordset.field.type 属性获取 ADO 数据类型的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19735653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:12:10  来源:igfitidea点击:

Get the name of an ADO data type from the recordset.field.type property?

vbams-access

提问by user1071914

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

我需要从 Access 表中生成字段列表(名称、类型和大小)。这个简单的 VB 代码几乎满足了我的需要:

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 

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

然而,“类型”当然是​​一个数字常量,比如“10”而不是“Varchar”之类的东西。

I was going to do this:

我打算这样做:

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

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

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

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.

如果这是一个“愚蠢的问题”,请提前道歉,但我并不是每天都在 VB 中工作,谷歌搜索也没有对此给出明确的答案。

采纳答案by user1071914

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 containedin a Field. The closest I was able to come was to do this for the quick-n-dirty:

问题(以上)下评论中的答案非常好,不幸的是,它们归结为“你不能从这里到达那里”。没有简单的方法可以获取字段中包含的数据类型。我能来的最接近的是为快速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

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

建议您访问Allen Browne的优秀页面并获取他的 FieldTypeName() 函数以获得更好的解决方案。

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

感谢所有提供答案的人(mehow、blackhawk、Chris Rolliston 和 Tim Williams)。