Access97 VBA导出为CSV格式问题

发布于 2024-11-01 10:05:37 字数 263 浏览 1 评论 0原文

我有一个 access97 数据库,我正在尝试编写一些代码以导出到 CSV 文件 - (我是 VBA 新手)。

然而,我有这个工作,我导出的一个字段是一种货币,因此其中的字段是 £3,456.00 - 当我导出到 CSV 时,我得到的正是这个 - 但我需要它只是数字,即 3456.00。

关于类似的问题 - 我的日期为 dd/mm/yyyy,我想知道是否有办法将 VBA 中的日期转换为 yyyy-mm-dd?

请记住,由于我的知识有限,任何解决方案都必须简单!

I have an access97 database and I am trying to write some code to export to a CSV file - (I am new to VBA).

I have this working however, there is one field that I am exporting that is a currency so in it for example is £3,456.00 - when I export to the CSV I get exactly this - however I need it to just be the number i.e 3456.00.

On a similar issue - I have the date as dd/mm/yyyy and I wonder if there is a way to convert that in VBA to yyyy-mm-dd?

Please bear in mind any solutions has to be simple due to my limited knowledge!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

水波映月 2024-11-08 10:05:37

抱歉耽搁了;看似简单的事情却花了更长的时间。正如我从您的假设:

DoCmd.TransferText acExportDelim, "olly_csv", "olly aorder export", "\\10.0.0.38\nw_upload\aorders.csv"

您有一个导出规范“olly_csv”,它确定如何导出
SELECT 查询“olly aorder export”的结果集到文件“aorders.csv”
在目标文件夹“\10.0.0.38\nw_upload”中。

将货币字段导出为普通双精度/浮点/单精度数字的简单方法
具有您选择的格式(dd/mm/yyyy)的日期字段将是
仅在出口规范中提出要求。我发现在 Access 中没有办法做到这一点
2000(据我所知,选择日期格式的方法有限,但是导入向导处理列类型的功能并未由
导出向导)。

关于“TransferText”的文档 (抱歉,Access 2003) 声明:

   SpecificationName  Optional Variant. A string expression that's the name of
   an import or export specification you've created and saved in the current
   database. For a fixed-width text file, you must either specify an argument or
   use a schema.ini file, which must be stored in the same folder as the
   imported, linked, or exported text file. To create a schema file, you can use
   the text import/export wizard to create the file. For delimited text files
   and Microsoft Word mail merge data files, you can leave this argument blank
   to select the default import/export specifications.

现在有 Microsoft Docs 语言学流派:乐观主义者会阅读
如下:如果您没有通过导出规范并且有合适的 schema.ini
文件,则导出过程将遵循文件中的规范。悲观主义者
会说:微软从未同意实现你的白日梦 - 如果你不这样做
为非固定宽度文件指定参数,TransferText 命令将
使用一些晦涩的默认导出规范(请付费咨询顾问
寻求并改变它)。

让我们保持乐观吧!

因此,创建一个 schema.ini 文件,其中包含“aorders.csv”部分。对于我的测试我
使用了一个表格

Tabelle: OlliesOrders 
 Name    Typ           Größe
 OrderId Long Integer   4
 Amount  Währung        8
 DateDue Datum/Uhrzeit  8

(抱歉德国人;金额是货币,DateDue 日期/时间)。对于那张桌子
schema.ini 部分如下所示:

[aorders.csv]
ColNameHeader=True
CharacterSet=1252
Format=Delimited(;)
DateTimeFormat=dd/mm/yyyy
Col1=OrderId Integer
Col2=Amount Float
Col3=DateDue Date

您必须根据您的字段调整此示例。您想要列标题吗?是
windows 代码页可以吗?字段分隔符怎么样?我不得不使用 ; (德语语言环境),你
可能需要“Format=CSVDelimited”。请参阅此处了解一些背景信息。然后打电话

DoCmd.TransferText acExportDelim, , "olly aorder export", "\\10.0.0.38\nw_upload\aorders.csv"

来检查乐观主义者是否占主导地位。

对于悲观主义者:

在要导出的表上创建一个新查询。将类型更改为 Ausführung/Execute (?)
并编辑 SQL 直到它看起来像:

SELECT OlliesOrders.* INTO [aorders.csv] IN 'M:\trials\23forum\SOTrials\txt' [TEXT;] FROM OlliesOrders;

resp.:

SELECT YourFieldsList INTO [aorders.csv] IN '\\10.0.0.38\nw_upload' [TEXT;] FROM YourTable;

并执行它(从查询窗口或宏/模块子)。我的结果:

"OrderId";"Amount";"DateDue"
1;1411,09;29/04/2011
2;123,45;13/04/2011

添加:我的主张的证据,即您无法在导出向导中指定类型:

导出
导出向导

导入
导入向导

Sorry about the delay; seemingly easy things took longer. As I assumue from your:

DoCmd.TransferText acExportDelim, "olly_csv", "olly aorder export", "\\10.0.0.38\nw_upload\aorders.csv"

that you have an export specification "olly_csv" that determines how to export the
resultset of the SELECT query "olly aorder export" to the file "aorders.csv"
in the destination folder "\10.0.0.38\nw_upload".

The easy way to export the CURRENCY field(s) as plain Double/Float/Single number
and the DATE field(s) with a format of your choice (dd/mm/yyyy) would be to
request just that in the export specification. I found no way to do that in Access
2000 (As far as I can see, there are limited ways to pick date formats, but the features of the Import Wizard to deal with the types of columns are not implemented by the
Export Wizard).

The Docs about "TransferText" (sorry, Access 2003) state:

   SpecificationName  Optional Variant. A string expression that's the name of
   an import or export specification you've created and saved in the current
   database. For a fixed-width text file, you must either specify an argument or
   use a schema.ini file, which must be stored in the same folder as the
   imported, linked, or exported text file. To create a schema file, you can use
   the text import/export wizard to create the file. For delimited text files
   and Microsoft Word mail merge data files, you can leave this argument blank
   to select the default import/export specifications.

Now there are to schools of Microsoft Docs philology: The optimists will read
that as: If you don't pass an export specification and have a suitable schema.ini
file, then the export process will adhere to the specs in the file. The pessimists
will say: Microsoft never agreed to fullfill your pipe dreams - if you don't
specify an argument for a non-fixed-width file, the TransferText command will
use some obscure default export specification (please pay a consultant to
seek and change it).

Let's be optimistic!

So create a schema.ini file with a section for "aorders.csv". For my tests I
used a table

Tabelle: OlliesOrders 
 Name    Typ           Größe
 OrderId Long Integer   4
 Amount  Währung        8
 DateDue Datum/Uhrzeit  8

(sorry about the German; Amount is Currency, DateDue Date/Time). For that table
the schema.ini section looks like:

[aorders.csv]
ColNameHeader=True
CharacterSet=1252
Format=Delimited(;)
DateTimeFormat=dd/mm/yyyy
Col1=OrderId Integer
Col2=Amount Float
Col3=DateDue Date

You'll have to adapt this example to your fields. Do you want column headers? Is the
windows codepage ok? What about field separators? I had to use ; (German locale), you
may need "Format=CSVDelimited". Look here for some background. Then call

DoCmd.TransferText acExportDelim, , "olly aorder export", "\\10.0.0.38\nw_upload\aorders.csv"

and check if optimists rule.

For pessimists:

Create a new query on the table to export (from). Change the type to Ausführung/Execute (?)
and edit the SQL until it looks like:

SELECT OlliesOrders.* INTO [aorders.csv] IN 'M:\trials\23forum\SOTrials\txt' [TEXT;] FROM OlliesOrders;

resp.:

SELECT YourFieldsList INTO [aorders.csv] IN '\\10.0.0.38\nw_upload' [TEXT;] FROM YourTable;

and execute it (from the query window or a macro/module Sub). My result:

"OrderId";"Amount";"DateDue"
1;1411,09;29/04/2011
2;123,45;13/04/2011

ADDED: Evidence for my claim, that you can't specify types in the Export Wizard:

Export
Export Wizard

Import
Import Wizard

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