excel - Excel VBA AccessApplication.OpenCurrentDatabase Not Working
问题描述
Here is what I'm trying to do with Excel VBA:
- Copy a range in Excel
- Open an Access database
- Delete records from the CV table
- Paste the new records from Excel into the CV table
- Run a make table query
- Close the database
The code below worked - once. After it ran successfully once, it will not run again. There is no error message - the Access DB just never opens and the macro ends. Nothing ran behind the scenes, the Access DB was never touched.
I am speculating that the error might have to do with the fact that the application was opened once and maybe not closed properly and therefore can't reopen? (No idea if this is accurate/makes sense)
Sheets("NAHVCV").Select
Range("A:C").Select
Selection.Copy
Dim appAccess As New Access.Application
Set appAccess = Access.Application
appAccess.OpenCurrentDatabase AccessDBPath
appAccess.Visible = True
appAccess.CurrentDb.Execute "DELETE * FROM [CV]"
appAccess.DoCmd.OpenTable "CV", acViewNormal, acEdit
appAccess.DoCmd.RunCommand acCmdPasteAppend
appAccess.DoCmd.Close acTable, "CV", acSaveYes
appAccess.DoCmd.OpenQuery "qryMakFutRetroVariance"
appAccess.CloseCurrentDatabase
appAccess.Quit acQuitSaveAll
解决方案
也许在子上方/外部定义访问应用程序:
将 appAccess 调暗为新的 Access.Application
Sub Test() '添加代码的其余部分 End Sub
我遇到了与您类似的问题,但是一旦我将访问应用程序移出子系统,我的访问文件每次都会打开
推荐阅读
- javascript - JavaScript:重组和对象数组
- javascript - 将多个选项逐行添加到表列
- ubuntu - 如何为 Ubuntu 开发桌面应用程序?
- micronaut - 使用 Micronaut Data JDBC,如何转换 WHERE 子句的值?
- c++ - 添加未使用的内存时性能下降
- php - 我们可以为自定义电子商务产品的每个客户创建不同的数据库和管理面板吗?
- excel - 如何在 Excel 中绘制范围与其他范围?
- flutter - 分页数据表和提供者
- unity3d - 再次加载场景时,不会从我应用了单例模式的脚本中调用启动函数
- mysql - mysql查询表值确实改变了