从脚本自动填充 Access 数据库

发布于 2024-12-11 19:04:27 字数 342 浏览 0 评论 0原文

我有一个脚本,可以将数据从数据库下载到一系列 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2024-12-18 19:04:27

我认为您可以使用 VBScript 来完成您需要的操作。

  • 将未填充的 Access 文件复制到时间戳为的新文件中
    名称

    FileSystemObject.CopyFile "c:\somefolder\template.mdb", "c:\dest\new.mdb"

请参阅 复制文件方法

  • 加载某些与模式匹配的 CSV 文件(例如
    目录中的“data_for_reporting_2_20111024_135142.csv”)
    访问文件。

您可以检查 CSV 的文件集合文件夹,确定哪些文件名与您的目标模式匹配,然后使用每个匹配的文件名运行 DoCmd.TransferText。

您将从 Access 应用程序实例运行 DoCmd.TransferText:

Option Explicit
Dim appAccess
Dim strMdb
Const cstrFolder = "c:\dest\"

strMdb = "new.mdb"

Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase cstrFolder & strMdb, False

因此,从该实例变量执行 Transfertext:

appAccess.DoCmd.TransferText [your options]

编辑:这对我来说在 VBA 中创建和测试会更快。所以我想我会用它来代替 VBScript。

在模板 MDB 的标准模块中创建一个函数 SnarfCSV。然后创建一个宏 mcrSnarfCSV,并将 SnarfCSV 函数作为其运行代码操作。然后,将模板 MDB 复制到新 MDB 后,使用 /x 命令行开关打开新 MDB 以运行宏。

"Path to MSACCESS.EXE" "Path to your db file" /x mcrSnarfCSV

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.

  • load certain CSV files that match a pattern (such as
    "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:

Option Explicit
Dim appAccess
Dim strMdb
Const cstrFolder = "c:\dest\"

strMdb = "new.mdb"

Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase cstrFolder & strMdb, False

So, do the Transfertext from that instance variable:

appAccess.DoCmd.TransferText [your options]

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.

"Path to MSACCESS.EXE" "Path to your db file" /x mcrSnarfCSV
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文