Sam Summers
Registered User.
- Local time
- Today, 08:41
- Joined
- Sep 17, 2001
- Messages
- 939
Hi again,
As i don't fully understand the code below i get the above error only when i add an image to a new record i have entered in the subform as in the attachment.
It fails at this line - rstMain.Edit
Once i select Save and then go back in again it allows me to add the image with no problem?
I just dont understand it but i am sure it is something simple.
Many thanks for your help in advance!
As i don't fully understand the code below i get the above error only when i add an image to a new record i have entered in the subform as in the attachment.
It fails at this line - rstMain.Edit
Once i select Save and then go back in again it allows me to add the image with no problem?
I just dont understand it but i am sure it is something simple.
Many thanks for your help in advance!
Code:
Private Sub AddCertBtn_Click()
Call AddAttachment("EmployeeCert", "CertImage", "EmployeeID", Me.EmployeeID, Me.CertID)
End Sub
Public Function AddAttachment(strTableName, strAttachField, strIDfield As String, i As Long, lngCertID As Long)
Dim fd As FileDialog
Dim oFD As Variant
Dim strFileName As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.ButtonName = "Select"
.AllowMultiSelect = False
.Title = "Choose File"
.InitialView = msoFileDialogViewDetails
.Show
For Each oFD In .SelectedItems
strFileName = oFD
Next oFD
On Error GoTo 0
End With
Set fd = Nothing
Dim cdb As DAO.Database, rstMain As DAO.Recordset, rstAttach As DAO.Recordset2, _
fldAttach As DAO.Field
Set cdb = CurrentDb
Set rstMain = cdb.OpenRecordset("SELECT " & strAttachField & " FROM " & strTableName & " where " & strIDfield & "= " & i & " AND CertID = " & lngCertID, dbOpenDynaset)
rstMain.Edit
Set rstAttach = rstMain(strAttachField).Value
rstAttach.AddNew
Set fldAttach = rstAttach.Fields("FileData")
fldAttach.LoadFromFile strFileName
rstAttach.Update
rstAttach.Close
Set rstAttach = Nothing
rstMain.Update
rstMain.MoveNext
rstMain.Close
Set rstMain = Nothing
Set cdb = Nothing
End Function