vba 通过vba更改字段的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27608708/
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
Change default value of field through vba
提问by Pablo
I am trying to change the default value of a field I just added through VBA. I have this line of code below that I added in to do so but for some reason it is saying the item is not in the collection and I am really confused as to why. Any help would be greatly appreciated!
我正在尝试更改刚刚通过 VBA 添加的字段的默认值。我在下面添加了这行代码,但出于某种原因,它说该项目不在集合中,我真的很困惑为什么。任何帮助将不胜感激!
AddColumn = "ALTER TABLE ShouldImportMetricsIDsTable " & _
"ADD COLUMN [ImportStatus] TEXT"
db.Execute AddColumn
CurrentDb.TableDefs("ShouldImportMetricsIDs").Fields("ImportStatus").DefaultValue = "No"
I have also included the line that adds the field.
我还包括了添加字段的行。
回答by 4dmonster
You just pointed to different tables in ALTER TABLE
you referenced ShouldImportMetricsIDsTable
and in VBA code ShouldImportMetricsIDs
.
您只是在ALTER TABLE
您引用的ShouldImportMetricsIDsTable
和 VBA 代码中指向了不同的表ShouldImportMetricsIDs
。
P.S. DefaultValue
property is build in so you can easily set its value
PSDefaultValue
属性已内置,因此您可以轻松设置其值
CurrentDb.TableDefs("ShouldImportMetricsIDsTable").Fields("ImportStatus").DefaultValue = "No"
but some other field properties are odd so you have to use constructions like that:
但其他一些字段属性很奇怪,所以你必须使用这样的结构:
Dim p As Property
Set p = CurrentDb.TableDefs("ShouldImportMetricsIDsTable").Fields("ImportStatus").CreateProperty("DefaultValue", dbText, "No")
CurrentDb.TableDefs("ShouldImportMetricsIDsTable").Fields("ImportStatus").Properties.Append (p)
回答by Access_Query
I think you can add the default value to the ALTER TABLE
expression:
我认为您可以将默认值添加到ALTER TABLE
表达式中:
CurrentProject.Connection.Execute "ALTER TABLE ShouldImportMetricsIDsTable " & _
"ADD COLUMN [ImportStatus] TEXT" & _
"DEFAULT ""No"";"
I'm basing my answer off this question: SQL SET DEFAULT not working in MS Access
我的回答基于这个问题:SQL SET DEFAULT not working in MS Access
EDIT: Looking at the answer from HansUp in that link, I edited the answer above to reflect the syntax he uses there. I hope this works for you.
编辑:查看该链接中 HansUp 的答案,我编辑了上面的答案以反映他在那里使用的语法。我希望这对你有用。