次の方法で共有


DAO で添付ファイルを操作する

DAO では、添付ファイル フィールドは他の複数値を持つフィールドとまったく同じように機能します。 添付ファイルを含むフィールドには、テーブルのレコードセットの子であるレコードセットが含まれます。 LoadFromFileSaveToFile という 2 つの新しい DAO メソッドがあり、添付ファイルのみを扱います。

レコードに添付ファイルを追加する

LoadFromFile メソッドはディスクからファイルを読み込み、そのファイルを添付ファイルとして指定されたレコードに追加します。 次のコード例は、LoadFromFile メソッドの構文を示しています。

Recordset.Fields("FileData").LoadFromFile(<filename>)

注:

FileData フィールドは、バイナリ添付ファイルデータを格納する目的で、Access データベース エンジンによって内部的に予約されています。

次のコード例では、LoadFromFile メソッドを使用して、社員の画像をディスクから読み込みます。

   '  Instantiate the parent recordset.
   Set rsEmployees = db.OpenRecordset("Employees") 
  
   … Code to move to desired employee 
  
   ' Activate edit mode. 
   rsEmployees.Edit 
  
   ' Instantiate the child recordset. 
   Set rsPictures = rsEmployees.Fields("Pictures").Value  
  
   ' Add a new attachment. 
   rsPictures.AddNew 
   rsPictures.Fields("FileData").LoadFromFile "EmpPhoto39392.jpg" 
   rsPictures.Update 
  
   ' Update the parent record 
   rsEmployees.Update 

添付ファイルをディスクに保存する

次のコード例では、SaveToFile メソッドを使用して、特定の従業員に関するすべての添付ファイルをディスクに保存する方法を示します。

'  Instantiate the parent recordset.
   Set rsEmployees = db.OpenRecordset("Employees") 
  
   … Code to move to desired employee 
  
   ' Instantiate the child recordset. 
   Set rsPictures = rsEmployees.Fields("Pictures").Value  
 
   '  Loop through the attachments. 
   While Not rsPictures.EOF 
  
      '  Save current attachment to disk in the "My Documents" folder. 
      rsPictures.Fields("FileData").SaveToFile _ 
                  "C:\Documents and Settings\Username\My Documents" 
      rsPictures.MoveNext 
   Wend 

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。