【问题标题】:create custom text file from Microsoft Access从 Microsoft Access 创建自定义文本文件
【发布时间】:2016-01-18 16:37:00
【问题描述】:

我正在尝试从 Access 数据库中创建一个文本文件,看起来完全像这样:

CADWorx P&ID Drop Down List Configuration File.

Notes:

-This file contains information on what
 appears in the drop down list in the
 CEDIT Additional Data Dialog

-Entries should be separated by a semi-colon (;)

-If a value is not set, an edit box will appear
 in the CEDIT Additional Data Dialog instead
 of a drop down list.

-Example: AREA_=031;032;033;034A;034B;
 Example: SERVICE_=AEC;HW;LH;CCH;



[DOCUMENTATION]
TYPE_=
DATESUB_=
DATEAPR_=
CREATEBY_=
APRBY_=


[LINE]
SERVICE_=OIL;FUEL GAS; 
AREA_=
UNIT_=
COUNT_=
TYPE_=
RATING_=
FLGFACE_=
DESIGNPSI_=
DESIGNDEG_=
LINE_NUM_=
OPERPSI_=
OPERDEG_=
SPECPRESS_=
SPECTEMP_=
MINDEG_=
TESTPSI_=
INSULATE_=
HEATTRACE_=
XRAY_=
CODE_=
JOINTEFF_=
WELDPROC_=
INSPECT_=
MATPIPE_=
COMPNOTE_=
NOTE_=
USER1_=

左边的所有字段(以'_='结尾)都是我数据库中的字段标题。然后如上所述,这些字段的值必须添加并用分号分隔。我已经研究了一个多星期,几乎只是在 Access 中进行文本文件自定义的死胡同。有人可以告诉我这是否是要走的路吗?还是应该将数据导出到 Excel 并从那里创建文本文件?

非常感谢您的帮助。提前致谢。

【问题讨论】:

  • 请编辑您的问题,删除屏幕截图,而不是复制和粘贴文本并将其格式化为代码。其实只保留相关部分,见minimal reproducible example
  • 重要的是:文件的哪些部分是不变的,哪些部分是从数据库中填充的?

标签: ms-access vba


【解决方案1】:

以下是将记录写入文件的基础知识:

Dim dbs As Database
Dim rst As Recordset
Dim intFileDesc As Integer      'File descriptor for output file (number used by OS)
Dim strOutput As String         'Output string for entry
Dim strRecordSource As String   'Source for recordset, can be SQL, table, or saved query
Dim strOutfile As String        'Full path to output file

Kill strOutfile                 'Delete the output file before using it.
                                'Not necessary, but ensures you have a clean copy every time
intFileDesc = FreeFile          'Get a free file descriptor
Open strOutfile For Binary As #intFileDesc 'Open the output file for writing
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strRecordSource ) 'open the recordset based on our source string
With rst    'make things easier for ourselves
While Not .EOF
        strOutput = !Field1 & ";" & !Field2 & ";" & !Field3
        Print #intFileDesc, strOutput   'Print output string to file
        .MoveNext       'Advance to next record in recordset
    Wend
    .Close                  'Close this recordset
End With
Close #intFileDesc          'Close output file
Set rst = Nothing
Set dbs = Nothing       'Garbage handling before we exit the function

基本行是这样的:

Print #intFileDesc, strOutput   'Print output string to file

你可以一次写一行。

因此,构建一个在此基础上扩展的函数,逐行创建输出(包括用于间距的空行)直到完成。就是这样。

这需要很多代码,但确实很简单。对于这样的输出,没有其他方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多