Access VBA无法创建表 [英] Access VBA unable to create table

查看:195
本文介绍了Access VBA无法创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用VBA创建Access表时遇到了麻烦,然后无法通过open-recordset访问它.我的目标是将记录写到表中.如果我手动创建表,则我的代码可以完美运行.如果该表不存在,则程序在尝试打开记录集时终止.错误消息为:

I'm having trouble creating an Access table using VBA, then accessing it via open-recordset. My goal is to write out records into a table. If I manually create the table, my code works perfectly. If the table does not exist, the program terminates at the open-recordset attempt. The error msg is:

"MSO Access数据库引擎找不到对象'myTable'

"The MSO Access Database engine could not find the object 'myTable'

其中"myTable"是argTable的值.这是代码段:

Where "myTable" is the value of argTable. Here's the code snippet:

Dim tbl As DAO.TableDef
Dim db As DAO.Database
Dim fld As DAO.Field

Set db = CurrentDb()
Set tbl = db.CreateTableDef(argTable)
Set fld = tbl.CreateField("F1")

Set rstAccessTableOut = db.OpenRecordset(argTable, dbOpenTable)

我做错了什么? (请注意,我不想使用SQL.)

What am I doing wrong? (Note I do not want to use SQL.)

推荐答案

添加完字段后,必须将新表def附加到集合中才能使用它.另外,也需要附加字段

After you finish adding the fields you have to append the new table def to the collection to be able to use it. Also the filed needs to be appended as well

Dim tbl As DAO.TableDef
Dim db As DAO.Database
Dim fld As DAO.Field

Set db = CurrentDb()
Set tbl = db.CreateTableDef(argTable)
Set fld = tbl.CreateField("F1", dbText, 20)

tbl.Fields.Append fld

db.TableDefs.Append tbl

Set rstAccessTableOut = db.OpenRecordset(argTable, dbOpenTable)

这篇关于Access VBA无法创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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