0

I have a program with all employees; each employee has a specific file of attachments. When I click on a specific employee name from the master list, a form opens with their file attachments.
I created a button that should open a Query then loop through to copy each employee attachment to another location.

The loop transfers all employee files. It will not let me filter the query to the specific employee open in my form when I try to apply the employee id (empl_id).

I tried adding the empl_id filter directly to the query itself. It works without the filter, but breaks when I add the filter parameter.
I tried prompting the user from VBA to type in the empl_id, so that it will filter the correct employee.

Currently, I tried using docmd.applyfilter in my VBA after the query is open.

Sub btnTransferAllPublic_Click()

    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim emplid As Integer

    Set db = CurrentDb
    Set rst = db.OpenRecordset("qry_Attachments_All_Public_Ind_Transfer", dbOpenDynaset, dbSeeChanges)
    Set emplid = rst!empl_id & Forms!frm_Employee_All!empl_id
    
    DoCmd.ApplyFilter , emplid
                    
    Do Until rst.EOF
        Debug.Print rst!empl_id & " | " & rst![ATTID]
                
        'Beginning of to do
            
        Const SavePath = "C:\Users\Public\Desktop\HRP_TRANSFER\"
        Dim sourcefile As String
        Dim destfile As String
        Dim ext As String
        On Error Resume Next
        MkDir "C:\Users\Public\Desktop\HRP_TRANSFER\"
        On Error GoTo 0

        sourcefile = rst!ATTACHLINK
        destfile = "C:\Users\Public\Desktop\HRP_TRANSFER\"
        ext = rst!attachfile
    
        FileCopy sourcefile, (destfile & ext)
        
        ' End of to do
        rst.MoveNext
    Loop
    
    rst.Close
    Set rst = Nothing
    Set db = Nothing

   'This is a message that tells user when all files have been transferred successfully
    MsgBox "Transfer Complete"
    
End Sub
Community
  • 1
  • 1
  • 1
    Your syntax is way off. Start by having the code to compile. Then study the [docs](https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/recordset-filter-property-dao?WT.mc_id=M365-MVP-5002361). – Gustav Oct 07 '22 at 14:27
  • ApplyFilter is used to filter a table/query/form/report object, not a recordset. Open recordset with an SQL statement that includes filter criteria. – June7 Oct 07 '22 at 15:05
  • @Gustav, I edited my original question (for the attempt to try by making the changes that you suggested with the docs attachment). It's giving me a mismatch data type now. – user15325333 Oct 07 '22 at 16:52
  • Just to make sure did you use Querydefs to pass the parameters? https://stackoverflow.com/questions/16568461/is-it-possible-to-pass-parameters-programmatically-in-a-microsoft-access-update – mazoula Oct 08 '22 at 04:25

1 Answers1

0

Your id is numeric, so no single-quotes:

rst.Filter = "empl_id = " & emplidn & ""
Gustav
  • 53,498
  • 7
  • 29
  • 55