为什么我不能直接使用 CurrentDB() 方法用 vba 重新链接表?
谁能解释为什么第一个代码示例有效但第二个代码示例无效?
此重新链接代码确实有效:
Dim db As Database
Dim sNewLinkAddress As String
sNewLinkAddress = "C:\temp\backend.accdb"
Set db = CurrentDb
db.TableDefs("table1").Connect = ";Database=" & sNewLinkAddress
db.TableDefs("table1").RefreshLink
此重新链接代码不起作用,但没有给出错误消息:
Dim sNewLinkAddress As String
sNewLinkAddress = "C:\temp\backend.accdb"
CurrentDb().TableDefs("table1").Connect = ";Database=" & sNewLinkAddress
CurrentDb().TableDefs("table1").RefreshLink
我担心的是,我不知道使用 CurrentDB( 直接返回的数据库对象) 之间存在根本区别)并使用设置为 CurrentDB() 返回的数据库对象的变量“db”。在我看来,两种方式应该是相同的,但显然我错了!
过去,我直接使用 CurrentDB() 来执行各种操作,例如毫无问题地打开记录集。重新链接表似乎存在一个特定问题。对这里发生的事情有什么想法吗?
我使用的是access 2007,但同样的问题也适用于2003。
Could anyone explain why the first code example works but the second doesn't?
This re-link code DOES work:
Dim db As Database
Dim sNewLinkAddress As String
sNewLinkAddress = "C:\temp\backend.accdb"
Set db = CurrentDb
db.TableDefs("table1").Connect = ";Database=" & sNewLinkAddress
db.TableDefs("table1").RefreshLink
This re-link code DOES NOT work, but no error messages are given:
Dim sNewLinkAddress As String
sNewLinkAddress = "C:\temp\backend.accdb"
CurrentDb().TableDefs("table1").Connect = ";Database=" & sNewLinkAddress
CurrentDb().TableDefs("table1").RefreshLink
What concerns me is that there is a fundamental difference that I'm unaware of between using the Database object directly returned by CurrentDB() and using a variable 'db' that is set to the Database object returned by CurrentDB(). To my mind both ways should be identical, but obviously I'm wrong!
In the past I have used CurrentDB() directly for various things such as opening a recordset with no problem. There seems to be a specific issue with re-linking tables. Any ideas to what's going on here?
I'm using access 2007, but the same issue applies to 2003 also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用
CurrentDb()
时,它都会返回数据库对象的一个新实例。为了说明我的意思,我将使用字母来表示数据库实例(字母是任意的,只是为了更容易遵循逻辑)。
因此,在第一个示例中,您使用将
CurrentDb
的返回值分配给数据库对象db
。然后,每次调用db
时,您都会引用同一个数据库对象实例:在第二个示例中,您多次调用
CurrentDb
函数,每次调用都会返回一个 不同的数据库对象实例:这里的结果是,当您在第二行调用
RefreshLink
时,您正在针对数据库对象的全新实例执行此操作;即,其“table1”连接属性尚未更改。Every time you call
CurrentDb()
it returns a new instance of the database object.To illustrate what I mean I'll use letters to represent the database instance (the letters are arbitrary and used only to make it easier to follow the logic).
So in your first example you are using assigning the return value of
CurrentDb
to a database objectdb
. Then each time you calldb
you are referring to the same database object instance:In your second example you make multiple calls to the
CurrentDb
function and each call returns a different database object instance:The upshot here is that when you call
RefreshLink
on the second line you are doing it against a brand new instance of the database object; ie, one whose "table1" Connect property has not been changed.