Browsing and selecting files to attach in email form

stevekos07

Registered User.
Local time
Today, 11:39
Joined
Jul 26, 2015
Messages
174
Hi. I have an email form which includes an attachment field for a file to be attached to the email.

I would like a browser button that will allow me to select a file and pass the path reference to the attachment field.

Can anyone assist with this, or are there any good tutorials for this? I am using both Access 2010 and 2016.
 
Below is command button code that presents a file browser and puts the selected file path in the ImagePath textbox. All you should need to do to use this code is copy and paste it into your command button subroutine and change the ImagePath to the name of the textbox where you want the file path to go,

Code:
Const msoFileDialogFilePicker = 3
'Dim fdg As FileDialog  'for early binding use Microsoft Office Objects Library
Dim fdg As Object
Set fdg = Application.FileDialog(msoFileDialogFilePicker)
fdg.AllowMultiSelect = False
If fdg.Show Then
       Me.[COLOR="Blue"]ImagePath[/COLOR] = fdg.SelectedItems(1)
End If
Set fdg = Nothing

You can see this work in the attached database where the file, if it is an image file, is displayed in the form. This is about as simple as this code can be. If you want you can add filters to display only certain types of files and you can specify an initial folder. If you want to add that I suggest googling "MS Access filedialog" to find some examples.
 

Attachments

Thanks for that! That works great. Now I have another problem. I have a "send email" button with the following on-click code:

Private Sub Command20_Click()

Dim mess_body As String
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)

Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = Me.Email_Address
.Subject = Me.Mess_Subject
.HTMLBody = Me.mess_text
If Left(Me.Mail_Attachment_Path, 1) <> "<" Then
.Attachments.Add (Me.Mail_Attachment_Path)
End If
'.DeleteAfterSubmit = True 'This would let Outlook send th note without storing it in your sent bin
.Send
End With
'MsgBox MailOutLook.Body
Exit Sub
email_error:
MsgBox "An error was encountered." & vbCrLf & "The error message is: " & Err.Description
Resume Error_out
Error_out:
End Sub

It hangs on the highlighted line with a compile error "user-defined type not defined".
 
Ok, I've fixed part of the problem, I just needed to reference the Microsoft Outlook object library, and the code now hangs on the .send line.

Is there a different or better command line for this?
 
I tested your code replacing the form references with strings and it works fine. I recall reading on this forum that one of the Mail Item properties balks at being set directly to a form control value. I can't remember which one so you can research that or just enclose them all in CStr, e.g,
Code:
.To = [COLOR="Blue"]CStr([/COLOR]Me.Email_Address[COLOR="blue"])[/COLOR]
.Subject = [COLOR="blue"]CStr([/COLOR]Me.Mess_Subjec[COLOR="blue"]t)[/COLOR]
'etc
 

Users who are viewing this thread

Back
Top Bottom