I have been researching for the past couple hours and had no luck finding a solution to this. What I want to do is have users fill a user form from Excel and submit the data to Access but one of the fields requires a screenshot which is essentially an attachment. I have been trying two sets of codes (DAO and ADODB). I am able to submit any other datatype to Access easily using the ADODB connection but not an attachment. Below are my 2 codes:
Private Sub cmdSave_Click()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Title = "Please select file to attach"
If .Show = True Then
SelectFile = .SelectedItems(1)
Else
Exit Sub
End If
End With
Set fd = Nothing
Dim NewCon As DAO.Database
Dim RS As DAO.Recordset
Dim strFileName As String
Dim wrkAcc As Workspace
Set NewCon = OpenDatabase("C:\Users\my.user\Documents\Database1.accdb")
Set RS = OpenRecordset("REPORTS", dbOpenTable)
RS.Edit
RS.Fields("NAME").Value = Application.UserName
RS.Fields("DATE_REPORT").Value = Date
RS.Fields("CLAIM_TYPE").Value = "Fielda"
RS.Fields("CLIENT_NAME").Value = "Fieldb"
RS.Fields("ISSUE").Value = "Fieldc"
RS.Fields("REPORT_NUMBERS").Value = "Fieldd"
'RS.Fields("ATTACHMENTS"). (this is where I want to place the attachment)
RS.Fields("LOG_TIME").Value = Now
RS.Close
NewCon.Close
End Sub
This is the ADODB:
Private Sub Image1_Click()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Title = "Please select file to attach"
If .Show = True Then
SelectFile = .SelectedItems(1)
Else
Exit Sub
End If
End With
Set fd = Nothing
Dim NewCon As ADODB.Connection
Set NewCon = New ADODB.Connection
Dim Recordset As ADODB.Recordset
Set Recordset = New ADODB.Recordset
NewCon.Open "Provider=Microsoft.ace.oledb.12.0;Data Source=C:\Users\my.user\Documents\Database1.accdb"
Recordset.Open "REPORTS", NewCon, adOpenDynamic, adLockOptimistic
Recordset.AddNew
Recordset.Fields(1).Value = Application.UserName
Recordset.Fields(2).Value = Date
Recordset.Fields(3).Value = "Fielda"
Recordset.Fields(4).Value = "Fieldb"
Recordset.Fields(5).Value = "Fieldc"
Recordset.Fields(6).Value = "Fieldd"
' Recordset.Fields(6) (this is where I want to place the attachment)
Recordset.Fields(8).Value = Now
Recordset.Update
Recordset.Close
NewCon.Close
End Sub