單擊表單中的按鈕時,我正在努力弄清楚如何將檔案插入/附加到表格欄位中。我一直在互聯網上搜索并嘗試了很多代碼,但直到現在我都沒有成功。
我在 stackoverflow 上找到了下面的代碼,但這不起作用,因為顯然我需要定義變數 db、rsfile、rsReport 和 filePath。
表名為“GC_Eventos”,存盤檔案的欄位為“Contrato”,其中表單的 Event_ID 等于表中的 Event_ID 暫存器
我將 db 定義為 DAO.Database 但我不知道其他變數是否需要定義為物件或變數或其他東西。有人知道如何修復此代碼或使用按鈕將檔案插入到 Access 上的表中的更好方法嗎?
我將衷心感謝您的幫助
Private Sub Command879_Click()
Dim db As DAO.Database
rsfile = db.OpenRecordset("GC_Eventos")
Do While Not rsfile.EOF
If rsfile.Fields("Evento_ID").Value = 1 Then
'Activate edit mode.
rsfile.Edit
'Instantiate the child recordset.
Set rsReport = rsfile.Fields("Contrato").Value
'Add a new attachment.
filePath = "C:\dbPDF\sitereport.pdf"
rsReport.AddNew
rsReport.Fields("FileData").LoadFromFile (filePath)
rsReport.Update
'Update the parent record
rsfile.Update
End If
'Next row
rsfile.MoveNext
Loop
End Sub
uj5u.com熱心網友回復:
如果模塊頭有強制變數宣告的Option Explicit行,則只需要宣告變數。我建議在創建模塊時默認執行此操作。從 VBA 編輯器 > 工具 > 選項 > 編輯器 > 選中需要變數宣告。必須手動添加到現有模塊。缺少它們的 3 個變數的暗淡陳述句:
Dim rsfile As DAO.Recordset
Dim rsReport As DAO.Recordset
Dim filepath As String
不管變數宣告如何,物件變數都需要設定。您的代碼中已經有一個示例 - rsReport。添加Set到該rsfile =行并為 db 變數添加一個 Set 行。
Set db = CurrentDb
Set rsfile = db.OpenRecordset("GC_Eventos")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/497865.html
