从脚本自动填充 Access 数据库
我有一个脚本,可以将数据从数据库下载到一系列 CSV 文件中。下载后,必须将它们加载到 Access 数据库中以进行报告(我使用 DoCmd.TransferText,并且有一个已保存的文本导入规范)。每次运行生成数据并下载到 CSV 的作业时,我通常需要加载到 Access 数据库的未填充版本的新副本中。有没有办法在批处理脚本中自动执行此操作?
简而言之,我需要能够:
- 将未填充的 Access 文件复制到名称中带有时间戳的新文件
- 将目录中匹配模式(例如“data_for_reporting_2_20111024_135142.csv”)的某些 CSV 文件加载到 Access 文件中。
I have a script that downloads data from a database into a series of CSV files. After they're downloaded, they must be loaded into an Access database for reporting (I use DoCmd.TransferText, and have a saved text import specification). Every time I run the job that generates the data and downloads into CSV, I usually need to load into a fresh copy of the unpopulated version of the Access database. Is there a way to automate this in a batch script?
In short, I need to be able to:
- copy the unpopulated Access file to a new file with the timestamp in the name
- load certain CSV files that match a pattern (such as "data_for_reporting_2_20111024_135142.csv") in the directory into the Access file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以使用 VBScript 来完成您需要的操作。
将未填充的 Access 文件复制到时间戳为的新文件中
名称
FileSystemObject.CopyFile "c:\somefolder\template.mdb", "c:\dest\new.mdb"
请参阅 复制文件方法。
目录中的“data_for_reporting_2_20111024_135142.csv”)
访问文件。
您可以检查 CSV 的文件集合文件夹,确定哪些文件名与您的目标模式匹配,然后使用每个匹配的文件名运行 DoCmd.TransferText。
您将从 Access 应用程序实例运行 DoCmd.TransferText:
因此,从该实例变量执行 Transfertext:
编辑:这对我来说在 VBA 中创建和测试会更快。所以我想我会用它来代替 VBScript。
在模板 MDB 的标准模块中创建一个函数 SnarfCSV。然后创建一个宏 mcrSnarfCSV,并将 SnarfCSV 函数作为其运行代码操作。然后,将模板 MDB 复制到新 MDB 后,使用
/x
命令行开关打开新 MDB 以运行宏。I think you can use VBScript to do what you need.
copy the unpopulated Access file to a new file with the timestamp in
the name
FileSystemObject.CopyFile "c:\somefolder\template.mdb", "c:\dest\new.mdb"
See CopyFile Method.
"data_for_reporting_2_20111024_135142.csv") in the directory into the
Access file.
You can examine the Files Collection of your CSV folder, determine which of those file names match your target pattern, then run DoCmd.TransferText with each matching file name.
You would run DoCmd.TransferText from an Access application instance:
So, do the Transfertext from that instance variable:
Edit: This would be faster for me to create and test in VBA. So I think I would use that instead of VBScript.
Create a function, SnarfCSV, in a standard module in your template MDB. Then create a macro, mcrSnarfCSV, with the SnarfCSV function as its runcode action. Then after you copy the template MDB to the new MDB, open the new one with the
/x
command line switch to run the macro.