简体   繁体   English

Access VBA 如何从附件字段中读取 txt 文件

[英]Access VBA how to read a txt file from an attachment field

I wrote an R function, which is too long to be stored even in a memo field.我写了一个 R 函数,它太长了,即使在memo字段中也无法存储。 There is probably a way of reading it if I store it in a txt file somewhere in my hard drive.如果我将它存储在硬盘驱动器某处的 txt 文件中,可能有一种读取它的方法。 But can I save this txt file in an attachment field and read it with vb code?但是我可以将这个txt文件保存在附件字段中并用vb代码读取吗? So far the nearest answer I got is as below to print names of the attachment, but not what is in the attachment.到目前为止,我得到的最接近的答案如下所示,用于打印附件的名称,而不是附件中的内容。

Dim dbs As DAO.Database
Dim rst As DAO.Recordset2
Dim rsA As DAO.Recordset2
Dim fld As DAO.Field2

'Get the database, recordset, and attachment field
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblAttachments")
Set fld = rst("Attachments")

'Navigate through the table
Do While Not rst.EOF

'Print the first and last name
Debug.Print rst("FirstName") & " " & rst("LastName")

'Get the recordset for the Attachments field
Set rsA = fld.Value

'Print all attachments in the field
Do While Not rsA.EOF

    Debug.Print , rsA("FileType"), rsA("FileName")

    'Next attachment
    rsA.MoveNext
Loop

'Next record
rst.MoveNext
Loop

I never store files in attachment fields.我从不将文件存储在附件字段中。 Instead I will store the absolute path to the file in the table and then use VBA to display or modify the file.相反,我将在表中存储文件的绝对路径,然后使用 VBA 显示或修改文件。

You can use the following code to print the contents of a text file to the console.您可以使用以下代码将文本文件的内容打印到控制台。 NOTE: You will need to add the Microsoft Scripting Runtime reference to your project.注意:您需要将 Microsoft Scripting Runtime 引用添加到您的项目中。

    Dim fso As FileSystemObject
    Dim ts As TextStream
    Set fso = CreateObject("Scripting.FileSystemObject")
    ' use path stored in table vvv here
    Set ts = fso.OpenTextFile(path, ForReading, False, 0)

    Do While ts.AtEndOfStream <> True
        debug.print ts.ReadLine
    Loop

    ts.Close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM