简体   繁体   English

如何在 MS 访问中使用联合查询获取附件

[英]how to get attachment files using union query in ms access

I have two independent tables in ms access now i want to get all data from both tables and show as one table using union query.我在 ms access 中有两个独立的表,现在我想从两个表中获取所有数据并使用联合查询显示为一个表。 all is work but the problem is the attachment fields doesn't not loaded and its Empty.一切正常,但问题是附件字段未加载且为空。

SELECT * 
FROM  Table1
UNION ALL select * from Table2;

--This is my query which gets all records from both table but the attachment type field is empty --这是我的查询,它从两个表中获取所有记录,但附件类型字段为空

This example may be usefull这个例子可能有用

Set rsChild = rsDocs.Fields("DocText").Value
rsChild.Fields("FileData").SaveToFile newFileName
rsChild.Close

where rsDocs - ADO (or DAO) recordset as query result其中 rsDocs - ADO(或 DAO)记录集作为查询结果
DocText - attachment field name DocText - 附件字段名称
rsChild.Fields("FileData") - stream object rsChild.Fields("文件数据") - stream object
SaveToFile - method of attachment field SaveToFile - 附件字段的方法

rsDocs.Fields("DocText").Value.Fields("FileData").SaveToFile

The below example uses an attachment field named AttachmentField .以下示例使用名为AttachmentField的附件字段。 You can pass the name of the file you're looking through the parameter.您可以通过参数传递您正在查看的文件的名称。

If you run only the SQL, the parameter window will pop-up asking for the parameter value.如果只运行SQL,会弹出参数window询问参数值。 To search, find the file and open it, you will need to use VBA.要搜索、查找文件并打开它,您需要使用 VBA。

See an example:看一个例子:

PARAMETERS [FileNameParam] Text (255);
SELECT T.ID, T.FileName
FROM (
    SELECT ID, AttachmentField.FileName AS FileName
    FROM Table1
    UNION ALL
    SELECT ID, AttachmentField.FileName AS FileName
    FROM Table2
) AS T
WHERE T.FileName Like '*' + [FileNameParam] + '*'

Dim q As DAO.QueryDef
Set q = CurrentDb().QueryDefs("Query1")
    q.Parameters("[FileNameParam]").Value = "Blank.pdf"
    
Dim r As DAO.Recordset
Set r = q.OpenRecordset(dbOpenSnapshot)

'nothing found
If r.EOF Then
    MsgBox "File not found."
    Exit Sub
End If

Dim filepath As String
    filepath = r![FileName]

'open
Application.FollowHyperlink filepath

'clean up
If Not r Is Nothing Then r.Close
If Not q Is Nothing Then q.Close

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

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