Private Sub btnfill_Click()
    Dim userNote As String
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sql As String
    Dim InvoiceNumbers As String
    Dim InvoiceIDList As String
    Dim InvoiceID As Variant
    Dim noteExists As Variant
    ' Prompt the user to enter a note
    userNote = InputBox("Enter the note for PayNote:", "Add PayNote")
    ' Check if user entered a note or cancelled the input
    If userNote <> "" Then
        On Error GoTo ErrHandler
        ' Get the current database and the filtered recordset from the form
        Set db = CurrentDb()
        Set rs = Me.RecordsetClone
        ' Initialize the InvoiceNumbers and InvoiceIDList strings
        InvoiceNumbers = ""
        InvoiceIDList = ""
        ' Loop through each filtered record
        If Not rs.EOF Then
            rs.MoveFirst
            Do While Not rs.EOF
                ' Ensure the InvoiceNumber field exists and is valid
                If Not IsNull(rs!InvoiceNumber) Then
                    InvoiceNumbers = InvoiceNumbers & "'" & rs!InvoiceNumber & "', "
                End If
                ' Move to the next record
                rs.MoveNext
            Loop
        End If
        ' Remove the trailing comma and space
        If Len(InvoiceNumbers) > 0 Then
            InvoiceNumbers = Left(InvoiceNumbers, Len(InvoiceNumbers) - 2)
        End If
        ' Get the list of InvoiceIDs based on the InvoiceNumbers
        sql = "SELECT InvoiceID FROM tblInvoice WHERE InvoiceNumber IN (" & InvoiceNumbers & ")"
        Set rs = db.OpenRecordset(sql)
        If Not rs.EOF Then
            rs.MoveFirst
            Do While Not rs.EOF
                InvoiceIDList = InvoiceIDList & rs!InvoiceID & ", "
                rs.MoveNext
            Loop
        End If
        ' Remove the trailing comma and space
        If Len(InvoiceIDList) > 0 Then
            InvoiceIDList = Left(InvoiceIDList, Len(InvoiceIDList) - 2)
        End If
        ' Update the corresponding records in tblInvoice
        sql = "UPDATE tblInvoice SET isPaid = True WHERE InvoiceNumber IN (" & InvoiceNumbers & ")"
        db.Execute sql, dbFailOnError
        ' Loop through each InvoiceID to update or insert PayNote
        For Each InvoiceID In Split(InvoiceIDList, ", ")
            noteExists = DLookup("PayNote", "tblNotes", "InvoiceID = " & InvoiceID)
           
            If IsNull(noteExists) Or noteExists = "" Then
                ' Insert new note
                sql = "INSERT INTO tblNotes (InvoiceID, PayNote) VALUES (" & InvoiceID & ", '" & userNote & "')"
            Else
                ' Update existing note
                sql = "UPDATE tblNotes SET PayNote = '" & userNote & "' WHERE InvoiceID = " & InvoiceID
            End If
            db.Execute sql, dbFailOnError
        Next InvoiceID
        ' Notify the user that the process is complete
        MsgBox "All filtered invoices have been updated.", vbInformation, "Process Complete"
        Exit Sub
ErrHandler:
        ' Handle potential errors
        MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
    Else
        MsgBox "No note was entered. The PayNote was not updated.", vbExclamation, "No Note Entered"
    End If
End Sub