PDA

View Full Version : Excel to Access Attachment



patilbush
04-18-2018, 07:50 AM
I am trying to send an attachment to Access through Excel VBA. Is there any way I can do that. DO anyone know how to do it.

heedaf
04-18-2018, 09:28 AM
I am trying to send an attachment to Access through Excel VBA. Is there any way I can do that. DO anyone know how to do it.

What you mean by attachment? Can you be more specific on what you are trying to do?

patilbush
04-18-2018, 01:32 PM
Thank you for the reply
Actually I am trying to save file into access database into Attachment field using excel VBA. I want to create a button in Excel VBA and when you click the button brower should open and select a file then after selecting the file should get saved in Access as an attachment.

Here is my code to op file using file dialog box

Private Sub CommandButton1_Click()
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim fd As FileDialog
Dim var As Boolean
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
fd.Filters.Add "All Files", "*.*"
fd.FilterIndex = 1
var = fd.Show
If Not var Then
MsgBox "Please Select a file"
Exit Sub
End If
fd.Execute
Unload Me
End Sub


Here is my code to save file using file dialog box

Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim fd As FileDialog
Dim var As Boolean
Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\File.accdb;Persist Security Info=False;"
Set fd = Application.FileDialog(msoFileDialogSaveAs)
var = fd.Show
If Not var Then
MsgBox "Enter name to save file"
End If
fd.Execute

But this code is saving my file to selected destination but I want it should save directly into access database in Attachment field.

Thank you

heedaf
04-18-2018, 02:04 PM
Thank you for the reply
Actually I am trying to save file into access database into Attachment field using excel VBA. I want to create a button in Excel VBA and when you click the button brower should open and select a file then after selecting the file should get saved in Access as an attachment.

Here is my code to op file using file dialog box

Private Sub CommandButton1_Click()
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim fd As FileDialog
Dim var As Boolean
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
fd.Filters.Add "All Files", "*.*"
fd.FilterIndex = 1
var = fd.Show
If Not var Then
MsgBox "Please Select a file"
Exit Sub
End If
fd.Execute
Unload Me
End Sub


Here is my code to save file using file dialog box

Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim fd As FileDialog
Dim var As Boolean
Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\File.accdb;Persist Security Info=False;"
Set fd = Application.FileDialog(msoFileDialogSaveAs)
var = fd.Show
If Not var Then
MsgBox "Enter name to save file"
End If
fd.Execute

But this code is saving my file to selected destination but I want it should save directly into access database in Attachment field.

Thank you

Hopefully this answers your question but the Access doesn't store an attachment but stores the name and locations of the attachment. You would have to use code to either move the attachment were you want it and set the path or use VBA to create a "link" to the attachment to store in the table. You could also use a hyperlink to have Access open it - which might be easier.

patilbush
04-18-2018, 02:20 PM
Thank you so much
Can you help me with the code to save the path of selected file to store into access.