热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ExcelVBA运行和导出访问查询-ExcelVBAtoRunandExportAccessQueries

IhavethefollowingcodethatkicksoffaseriesofqueriesinanAccessdatabasefromExcel.When

I have the following code that kicks off a series of queries in an Access database from Excel. When these queries are run by themselves in Access, they work fine and succeed in generating the right file, but when I convert the macros to run in Excel using a button click, I run into some problems. See my code below:

我有以下代码从Excel中启动Access数据库中的一系列查询。当这些查询在Access中自己运行时,它们可以正常工作并成功生成正确的文件,但是当我使用单击按钮将宏转换为在Excel中运行时,我遇到了一些问题。请参阅下面的代码:

Sub AccessImport()

Dim acApp As Object
Dim MyDatabase As String
Dim question As String

question = MsgBox(Prompt:="Are you sure you want to complete this action?  Running this process is lengthy and could take a couple minutes to complete.", Buttons:=vbYesNo, Title:="Run SOD Matrix")

If question = vbYes Then

MyDatabase = "directory string"
OutputFile = "output string"

    'open the database and apend the combination table to existing
    Set acApp = CreateObject("Access.Application")
        acApp.OpenCurrentDatabase (MyDatabase)
        acApp.Visible = True
        acApp.UserCOntrol= True
        acApp.DoCmd.SetWarnings False
        acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_NAMES", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_USER", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYDELETE_SOD_TBL", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYAPPEND_PS_ROLE_NAMES", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYAPPEND_PS_ROLE_USER", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYAPPEND_SOD_TBL", acViewNormal, acEdit
        'acApp.DoCmd.OpenQuery "QRY_HIGH", acViewNormal, acEdit
        acApp.DoCmd.OutputTo acOutputQuery, "QRY_HIGH", "ExcelWorkbook(*.xlsx)", OutputFile, _
            False, "", , acExportQualityPrint
        acApp.DoCmd.SetWarnings True
        acApp.CloseCurrentDatabase
        acApp.Quit
    Set acApp = Nothing

Else
MsgBox ("Process has been cancelled.")
    Exit Sub
End If
MsgBox ("Process has completed successfully.")
End Sub

For the last query, meant to export and save the output, I run into an error telling me Property is not found. I've tried changing the DoCmd to TransferSpreadsheet, the format type of acFormalXLS to prevent conversion issues, but I still cannot get it to complete successfully. Do I need to put this code in a module instead of keeping it on the sheet itself? Thoughts/help?

对于最后一个查询,意味着导出和保存输出,我遇到一个错误,告诉我找不到Property。我已经尝试将DoCmd更改为TransferSpreadsheet,acFormalXLS的格式类型以防止转换问题,但我仍然无法成功完成它。我是否需要将此代码放在模块中而不是将其保留在工作表本身上?想法/帮助?

1 个解决方案

#1


2  

Siddharth identified the issue with this statement:

Siddharth通过以下声明确定了问题:

acApp.DoCmd.OutputTo acOutputQuery, "QRY_HIGH", "ExcelWorkbook(*.xlsx)", OutputFile, _
    False, "", , acExportQualityPrint

Without a reference to the Access object library, Excel will know nothing about the Access constants acOutputQuery and acExportQualityPrint.

如果没有对Access对象库的引用,Excel将无法了解Access常量acOutputQuery和acExportQualityPrint。

You would be wise to add Option Explict to the module's Declarations section and then run Debug->Compile from the VB Editor's main menu. When you do that, I suspect you will discover similar problems with the lines such as this ...

您最好将Option Explict添加到模块的Declarations部分,然后从VB Editor的主菜单中运行Debug-> Compile。当你这样做时,我怀疑你会发现类似这样的问题...

acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_NAMES", acViewNormal, acEdit

Excel will also know nothing about the Access constants acViewNormal and acEdit. However, if your intent was to execute "action" queries (INSERT, UPDATE, DELETE, etc), it's good that those constants are unrecognized. Otherwise you would be opening those queries in Design View instead of executing them.

Excel也对Access常量acViewNormal和acEdit一无所知。但是,如果你的意图是执行“动作”查询(INSERT,UPDATE,DELETE等),那么这些常量是无法识别的。否则,您将在“设计视图”中打开这些查询,而不是执行它们。

Consider a different approach ...

考虑一种不同的方法......

Const dbFailOnError As LOng= 128
'acApp.DoCmd.SetWarnings False ' leave SetWarnings on!
acApp.CurrentDb.Execute "QRYDELETE_PS_ROLE_NAMES", dbFailOnError
acApp.CurrentDb.Execute "QRYDELETE_PS_ROLE_USER", dbFailOnError

推荐阅读
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • Oracle10g备份导入的方法及注意事项
    本文介绍了使用Oracle10g进行备份导入的方法及相关注意事项,同时还介绍了2019年独角兽企业重金招聘Python工程师的标准。内容包括导出exp命令、删用户、创建数据库、授权等操作,以及导入imp命令的使用。详细介绍了导入时的参数设置,如full、ignore、buffer、commit、feedback等。转载来源于https://my.oschina.net/u/1767754/blog/377593。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • Todayatworksomeonetriedtoconvincemethat:今天在工作中有人试图说服我:{$obj->getTableInfo()}isfine ... [详细]
  • 本文介绍了一个视频转换软件MyVideoConverter,该软件支持将mpg转换成swf格式,支持多种格式的转换,转换速度快,还能转换成3GP格式,同时具有音频分离提取功能。欢迎使用MyVideoConverter进行视频转换和音频提取。 ... [详细]
author-avatar
幽兰伴竹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有