on the click of your "Add/Update" button, you first save the record on the form:
Code:
Me.Dirty = False
If Not IsNull(Me!txtBarcodeDropZone) Then
If Len(Dir$(Me!txtBarcodeDropZone))<>0 And Not IsNull(Me!CustomerSKU) Then
Call ProcessFile(Me!CustomerSKU, Me!txtBarcodeDropZone)
End If
End If
the updated code:
Code:
Private Sub ProcessFile(ByVal CustomerSKU As String, ByVal sFile As String)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset2
Dim rsA As DAO.Recordset2
Dim strFile As String
strFile = Dir(sFile) 'Get File Name ONLY
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT BarCodeImage FROM Product WHERE CustomerSKU = '" & CustomerSKU & "'")
If Not (rst.BOF And rst.EOF) Then
rst.MoveFirst
Set rsA = rst(0).Value
rst.Edit
With rsA
If Not (.BOF And .EOF) Then
' delete old image
.MoveFirst
.Delete
End If
' add new image
.AddNew
.Fields("FileName") = strFile
.Fields("FileData").LoadFromFile sFile
.Update
End With
rst.Update
End If
rst.Close
Set rsA = Nothing
Set rst = Nothing
Set dbs = Nothing
End Sub
pls. take note that adding an attachment field can rapidly bloat your db.
it is best to only keep the Path of the image to a Short text field on Product table.