Solved Runtime error 3021 - No current record. only on selecting a new image file (1 Viewer)

Sam Summers

Registered User.
Local time
Today, 15:05
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!

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
 

Attachments

  • Screenshot 2022-07-13 135054.png
    Screenshot 2022-07-13 135054.png
    35 KB · Views: 172
Perhaps test you even have a record?
EOF or BOF ?
Sorry i don't quite understand but i'm thinking it something to do with the sequence?
I have attached a screenshot showing the form as it is after the user has created the record in the subform and after the user has selected the image.
The error message comes up immediately after this
 
Hi. Just a guess, but try saving the record first?
Code:
Private Sub AddCertBtn_Click()

If Me.Dirty Then Me.Dirty = False

Call AddAttachment("EmployeeCert", "CertImage", "EmployeeID", Me.EmployeeID, Me.CertID)

End Sub
 
Hi. Just a guess, but try saving the record first?
Code:
Private Sub AddCertBtn_Click()

If Me.Dirty Then Me.Dirty = False

Call AddAttachment("EmployeeCert", "CertImage", "EmployeeID", Me.EmployeeID, Me.CertID)

End Sub
Once again - that was it! Thank you!!!!
 

Users who are viewing this thread

Back
Top Bottom