# Solved: Access 2007 VBA SaveToFile Methor Error



## Chilton (Jul 17, 2008)

Hello,
I am trying to get my code to work that will allow the user to download an attachment from the database onto their own computer. I have been following the MSDN information at http://msdn.microsoft.com/en-us/library/bb257443.aspx and yesterday I would get an error saying the file already exists at specified location, however it still saved the attachment there successfully. However, further testing today doesn't give me that run-time error anymore, but simply makes Access 2007 crash and no longer saves the file successfully to specified location. My code is below, any help would be greatly appreciated. Thanks.


```
Dim Recordset As DAO.Recordset
   Dim InnerRecordset As DAO.Recordset
   Dim atch As DAO.Field2
   Dim path As String
   Dim db As DAO.Database
   
   Set db = CurrentDb
   
   With Application.FileDialog(msoFileDialogFolderPicker)
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.count > 0 Then
         path = .SelectedItems(1)
      End If
   End With
      Set Recordset = db.OpenRecordset("Parts")
      Do Until Recordset.EOF
         Text2.SetFocus
         If Recordset("PartNum") = Text2.Text Then
            Set InnerRecordset = Recordset.Fields("Drawing").value
            Set atch = InnerRecordset.Fields("FileData")
            atch.SaveToFile path
            InnerRecordset.Close
         End If
      Loop
      Recordset.Close
      Exit Sub
```


----------



## OBP (Mar 8, 2005)

Chilton, you are in to pretty advanced VBA that I have never used. But it would appear logical to "delete" or "kill" the file you are going to ouptut to, prior to Outputting the data, unless you are "Appending" the data to the file.


----------



## Chilton (Jul 17, 2008)

I am trying to get it to work with the assumption that the file does not exist in the current directory that I am trying to save to.

I have been working on it over the weekend and actually figured out how to get it to save one file successfully,the msdn information that I was following failed to include the file name along with the path in order to get it to save. However, now I am trying to get it to save more than one file to that location from the attachment field, I am getting the crashes from access again. The really weird thing is that even if I comment out the savetofile method call, the program still crashes on me, but the same code will work fine if there is only one attachment for the specified record. My updated code is below:


```
Dim Recordset As DAO.Recordset
   Dim InnerRecordset As DAO.Recordset
   Dim atch As DAO.Field2
   Dim path As String
   Dim db As DAO.Database
   
   Set db = CurrentDb
   
   With Application.FileDialog(msoFileDialogFolderPicker)
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.count > 0 Then
         path = .SelectedItems(1)
      End If
   End With
      Set Recordset = db.OpenRecordset("Parts")
      Do Until Recordset.EOF
         Text5.SetFocus
         If Recordset("DrawingNum") = Text5.Text Then
            Set InnerRecordset = Recordset.Fields("Drawing").value
            InnerRecordset.MoveFirst
            Do Until InnerRecordset.EOF
               Set atch = InnerRecordset.Fields("FileData")
               atch.SaveToFile path & "\" & InnerRecordset.Fields("FileName").value
               InnerRecordset.MoveNext
            Loop
               InnerRecordset.Close
            GoTo Endsub:
         End If
      Loop
      Recordset.Close
      Exit Sub
Endsub:
Recordset.Close
```
Does access have a problem with traversing through a recordset that is a child of another recordset?


----------



## Chilton (Jul 17, 2008)

Wow im dumb, figured out the problem, forgot the Recordset.MoveNext at the end of the loop lol. Thanks to all who had looked at this


----------

