簡體   English   中英

Access VBA 如何從附件字段中讀取 txt 文件

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

我寫了一個 R 函數,它太長了,即使在memo字段中也無法存儲。 如果我將它存儲在硬盤驅動器某處的 txt 文件中,可能有一種讀取它的方法。 但是我可以將這個txt文件保存在附件字段中並用vb代碼讀取嗎? 到目前為止,我得到的最接近的答案如下所示,用於打印附件的名稱,而不是附件中的內容。

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

我從不將文件存儲在附件字段中。 相反,我將在表中存儲文件的絕對路徑,然后使用 VBA 顯示或修改文件。

您可以使用以下代碼將文本文件的內容打印到控制台。 注意:您需要將 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