vba Visual Basic - 检查 If 语句中的记录集字段类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13615144/
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-11 18:37:53  来源:igfitidea点击:

Visual Basic - Check a recordset field type in an If statement

vbams-accesstypes

提问by user1844098

I'm trying to check the datatype of a field in a table, to have the code do one thing if it is of Text type and another if it is of any other type. Below is the code that I have but I don't think I have the third line right at all. Any help would be greatly appreciated!

我正在尝试检查表中字段的数据类型,如果它是 Text 类型,则让代码做一件事,如果它是任何其他类型,则让代码做另一件事。下面是我拥有的代码,但我认为我根本没有正确的第三行。任何帮助将不胜感激!

Set dbExample = CurrentDb
Set rsTester = dbExample.OpenRecordset("tester", dbOpenDynaset)
If TypeOf rsTester!exampleField Is Text Then
    'Does what the code needs to do
Else
    'Does what the code needs to do
End If
rsTester.Close

采纳答案by prprcupofcoffee

TypeOfisn't used with DAO Fieldobjects. What you want to do is something more like

TypeOf不用于 DAOField对象。你想做的是更像

If rsTester("exampleField").Type = dbText Then
    ' do the thing
Else
    ' do the other thing
End Select

回答by Patrick Honorez

You can test the Type property of a field in a TableDef object.

您可以测试 TableDef 对象中字段的 Type 属性。

Dim db As DAO.Database
Dim td As DAO.TableDef

Set db = CurrentDb
With db.TableDefs("Factures")
    Debug.Print .Fields("nofact").Type
End With