简体   繁体   中英

Access VBA: Invalid field data type

With an Access VBA, I am trying to load a file from a directory to the table [table name] in the field [field name] which Data Type is Attachment .

The problem is that I get the error Invalid field data type .

在此处输入图片说明

The code is the following.

Dim SQL As String
SQL = " SELECT * FROM [table name] WHERE ID = '10'"

Dim VESRecordSet As Recordset
Set VESRecordSet = CurrentDb.OpenRecordset(SQL)

VESRecordSet![field name].LoadFromFile "D:\Documents\file.vsd"

You have made multiple errors:

  1. To work with attachments, you should use the DAO.RecordSet2 object
  2. You need to open the child recordset containing the file data before you can use .LoadFromFile
  3. You should use .Edit and .Update when editing and updating a recordset

Final code:

Dim SQL As String
SQL = " SELECT * FROM [table name] WHERE ID = '10'"

Dim VESRecordSet As DAO.Recordset2
Dim rsAttachments As DAO.Recordset2
Set VESRecordSet = CurrentDb.OpenRecordset(SQL)

VESRecordSet.Edit
Set rsAttachments = VESRecordSet![field name].Value
rsAttachments.AddNew
rsAttachments.Fields("FileData").LoadFromFile "D:\Documents\file.vsd"
rsAttachments.Update
rsAttachments.Close
VESRecordSet.Update
VESRecordSet.Close

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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