shieldsco
asked on
Access VBA Prompt User For Save Folder
I'm using the following code to export attachments to a folder on my disk. I would like to modify the code to prompt the user for the save folder.
Private Sub Export_Attachment_Click()
On Error GoTo Err_SaveImage
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = db.OpenRecordset("select * from tbl_MOU")
Do Until rsParent.EOF
Set rsChild = rsParent.Fields("Open ISA_MOU").Value
rsChild.OpenRecordset
If rsChild.RecordCount <> 0 Then
rsChild.Fields("FileData") .SaveToFil e ("\\cdc.gov\private\L327\x bn8\Attach ments\")
End If
rsChild.Close
Set rsChild = Nothing
rsParent.MoveNext
Loop
Exit_SaveImage:
Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub
Err_SaveImage:
If Err = 3839 Then
MsgBox ("File Already Exists in the Directory!")
Resume Next
Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_SaveImage
End If
End Sub
Private Sub Export_Attachment_Click()
On Error GoTo Err_SaveImage
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = db.OpenRecordset("select * from tbl_MOU")
Do Until rsParent.EOF
Set rsChild = rsParent.Fields("Open ISA_MOU").Value
rsChild.OpenRecordset
If rsChild.RecordCount <> 0 Then
rsChild.Fields("FileData")
End If
rsChild.Close
Set rsChild = Nothing
rsParent.MoveNext
Loop
Exit_SaveImage:
Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub
Err_SaveImage:
If Err = 3839 Then
MsgBox ("File Already Exists in the Directory!")
Resume Next
Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_SaveImage
End If
End Sub
ASKER
Error Message - runtime error 2147467259 (80004005) Method file dialog of object failed on line With Application.FileDialog(mso FileDialog FolderPick er)
Private Sub Export_Attachment_Click()
'On Error GoTo Err_SaveImage
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = db.OpenRecordset("select * from tbl_MOU")
Do Until rsParent.EOF
Set rsChild = rsParent.Fields("Open ISA_MOU").Value
rsChild.OpenRecordset
If rsChild.RecordCount <> 0 Then
With Application.FileDialog(mso FileDialog FolderPick er)
.AllowMultiSelect = False
.Show
strFolderName = .SelectedItems(1)
End With
If strFolderName = "" Then strFolderName = "\\cdc.gov\private\L327\xb n8\Attachm ents\"
' rsChild.Fields("FileData") .SaveToFil e ("\\cdc.gov\private\L327\x bn8\Attach ments\")
End If
rsChild.Close
Set rsChild = Nothing
rsParent.MoveNext
Loop
Exit_SaveImage:
Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub
Err_SaveImage:
If Err = 3839 Then
MsgBox ("File Already Exists in the Directory!")
Resume Next
Else
'MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_SaveImage
End If
End Sub
Private Sub Export_Attachment_Click()
'On Error GoTo Err_SaveImage
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = db.OpenRecordset("select * from tbl_MOU")
Do Until rsParent.EOF
Set rsChild = rsParent.Fields("Open ISA_MOU").Value
rsChild.OpenRecordset
If rsChild.RecordCount <> 0 Then
With Application.FileDialog(mso
.AllowMultiSelect = False
.Show
strFolderName = .SelectedItems(1)
End With
If strFolderName = "" Then strFolderName = "\\cdc.gov\private\L327\xb
' rsChild.Fields("FileData")
End If
rsChild.Close
Set rsChild = Nothing
rsParent.MoveNext
Loop
Exit_SaveImage:
Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub
Err_SaveImage:
If Err = 3839 Then
MsgBox ("File Already Exists in the Directory!")
Resume Next
Else
'MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_SaveImage
End If
End Sub
replace
rsChild.Fields("FileData") .SaveToFil e ("\\cdc.gov\private\L327\x bn8\Attach ments\")
with
rsChild.Fields("FileData") .SaveToFil e (strFolderName )
rsChild.Fields("FileData")
with
rsChild.Fields("FileData")
ASKER
Error Message - runtime error 2147467259 (80004005) Method file dialog of object failed on line With Application.FileDialog(mso FileDialog FolderPick er)
Code:
Private Sub Export_Attachment_Click()
On Error GoTo Err_SaveImage
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = db.OpenRecordset("select * from tbl_MOU")
Do Until rsParent.EOF
Set rsChild = rsParent.Fields("Open ISA_MOU").Value
rsChild.OpenRecordset
If rsChild.RecordCount <> 0 Then
With Application.FileDialog(mso FileDialog FolderPick er)
.AllowMultiSelect = False
.Show
strFolderName = .SelectedItems(1)
End With
If strFolderName = "" Then strFolderName = "\\cdc.gov\private\L327\xb n8\Attachm ents\"
rsChild.Fields("FileData") .SaveToFil e (strFolderName)
'rsChild.Fields("FileData" ).SaveToFi le ("\\cdc.gov\private\L327\x bn8\Attach ments\")
End If
rsChild.Close
Set rsChild = Nothing
rsParent.MoveNext
Loop
Exit_SaveImage:
Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub
Err_SaveImage:
If Err = 3839 Then
MsgBox ("File Already Exists in the Directory!")
Resume Next
Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_SaveImage
End If
End Sub
Code:
Private Sub Export_Attachment_Click()
On Error GoTo Err_SaveImage
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = db.OpenRecordset("select * from tbl_MOU")
Do Until rsParent.EOF
Set rsChild = rsParent.Fields("Open ISA_MOU").Value
rsChild.OpenRecordset
If rsChild.RecordCount <> 0 Then
With Application.FileDialog(mso
.AllowMultiSelect = False
.Show
strFolderName = .SelectedItems(1)
End With
If strFolderName = "" Then strFolderName = "\\cdc.gov\private\L327\xb
rsChild.Fields("FileData")
'rsChild.Fields("FileData"
End If
rsChild.Close
Set rsChild = Nothing
rsParent.MoveNext
Loop
Exit_SaveImage:
Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub
Err_SaveImage:
If Err = 3839 Then
MsgBox ("File Already Exists in the Directory!")
Resume Next
Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_SaveImage
End If
End Sub
Sorry you need the ref to Microsoft Office 15.0(not sure) Object Library to use Application.filedialog
ASKER
I have reference to MS 15.0
ASKER
The code works, however it prompts me for each file... I need to export all the attachments with only one prompt...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
You could use
Open in new window
Regards