Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access .CreateField方法,使用字符串变量作为名称_Ms Access_Vba - Fatal编程技术网

Ms access .CreateField方法,使用字符串变量作为名称

Ms access .CreateField方法,使用字符串变量作为名称,ms-access,vba,Ms Access,Vba,到目前为止,我已经有了一些无关的代码,还有一些匿名数据库和变量名: Dim dB as DAO.Database Dim tdf as DAO.TableDef Dim newField As DAO.Field Dim newFieldNameVar As String newFieldNameVar = "Variable Name" Set dB = CurrentDb Set tdf = dB.TableDefs("PreExistingTable") Set newField

到目前为止,我已经有了一些无关的代码,还有一些匿名数据库和变量名:

Dim dB as DAO.Database
Dim tdf as DAO.TableDef
Dim newField As DAO.Field

Dim newFieldNameVar As String

newFieldNameVar = "Variable Name"

Set dB = CurrentDb

Set tdf = dB.TableDefs("PreExistingTable")

Set newField = tdf.CreateField(newFieldNameVar)

tdf.Fields.Append newField

Set newField = Nothing
Set tdf = Nothing
Set dB = Nothing
前面提到的代码存在于我的函数中,但不在循环中,但newFieldNameVar是循环的产物,因此会根据与问题线程无关的条件定期更改

在.Fields.Append行执行时,我得到以下运行时错误:字段数据类型无效。”3259'我对任何形式或方式的编程都相当陌生,但想象一下,我可能对用作.CreateField方法的Name元素的变量使用了错误的语法。有什么想法吗?我能在.CreateField方法中使用字符串变量作为名称吗


提前谢谢你的帮助

虽然
Type
.CreateField
方法的可选参数,但每个字段都需要一个类型才能将其附加到表中,否则Access将抛出此错误

指定字段大小也是一种很好的做法

指定您的字段类型,例如:

Dim dB as DAO.Database
Dim tdf as DAO.TableDef
Dim newField As DAO.Field

Dim newFieldNameVar As String

newFieldNameVar = "Variable Name"

Set dB = CurrentDb

Set tdf = dB.TableDefs("PreExistingTable")

Set newField = tdf.CreateField(newFieldNameVar, dbText, 255) '255 character long text field

tdf.Fields.Append newField

Set newField = Nothing
Set tdf = Nothing
Set dB = Nothing

这似乎有可能使桌子鼓得相当厉害。为什么需要另一列?我正在转换数据,在整个数据集上运行程序时,我必须从转换的文件(预期的和意外的)中取出一组属性。我了解表的255个字段限制,但我需要能够在程序遇到与此特定表相关的新数据时创建新字段。好的,那么您使用什么标准来确定何时需要新字段?为什么临时表不是一个选项?数据可以迁移吗?这闻起来像是XY问题;您心中有一个解决方案,但我们甚至不知道这个解决方案应该解决的根本原因。我不确定为什么要知道何时需要新字段以及您的第一个回答与我的原始问题相关。我能在.CreateField方法中使用字符串变量作为名称吗?如果您需要参数来回答这个问题,那么假设在x=1或清管器飞行时需要新字段(无论您喜欢什么),临时表不是一个选项,因为我将需要使用收集的数据以供将来使用(使清管器飞行)。数据不需要迁移。我想到的解决方案是使用变量名创建一个新字段。这可能吗?如果不可能的话,我会找到一个解决办法。因为这是一个相当危险的方法,将来会导致麻烦。这就是为什么我试图了解更多关于这个项目的整体目标是什么,这样我们就可以尝试解决根本问题。这就是诀窍。现在我可以让我的猪飞了。非常感谢。如果答案解决了你的问题,请接受。这将有助于未来用户面对同样的问题。谢谢,我是个新手。我假设答案旁边的绿色复选框?@HarmLeastOne是的。你可以阅读更多关于接受的内容,这对初学者来说是一个有用的资源。