0

I am using the below SQL, it works fine if I run it from query builder but once I have put it in VBA it throws out an error:

Code:

With CurrentDb.CreateQueryDef("", "SELECT [_tbl_Structure].[User Name], tbl_Genesys_Daily.Field32, [_tbl_Structure].[Supervisor Emp Num], [_tbl_Structure].Supervisor FROM _tbl_Structure RIGHT JOIN tbl_Genesys_Daily ON [_tbl_Structure].[User ID] = tbl_Genesys_Daily.Field5 WHERE ((([_tbl_Structure].Supervisor)=?));")
            .Parameters(0) = [Forms]![frm_Manager_Stats_NEW]![Text279]  
            Set lvxObj = AvailabilityCap.Object
                Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
        End With

Error:

Too few parameters. Expected 1. (Runtime 3061)

Any help on understanding why this works for one but not another is appreciated

2
  • Please don't do Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset) if I answer you should use Set rs = .OpenRecordset. That's totally not the same. Commented Apr 3, 2018 at 11:04
  • Apologies - I have got this working, thank you Commented Apr 3, 2018 at 11:16

1 Answer 1

1

Form- and report-based parameters are only available in the GUI context (queries run using the GUI, forms, reports, macros and DoCmd.RunSQL). You're probably executing this through CurrentDb, and need to use a querydef instead.

With CurrentDb.CreateQueryDef("", "SELECT [_tbl_Structure].[User Name], tbl_Genesys_Daily.Field32, [_tbl_Structure].[Supervisor Emp Num], [_tbl_Structure].Supervisor FROM _tbl_Structure RIGHT JOIN tbl_Genesys_Daily ON [_tbl_Structure].[User ID] = tbl_Genesys_Daily.Field5 WHERE ((([_tbl_Structure].Supervisor)=?));")
    .Parameters(0) = [Forms]![frm_Manager_Stats_NEW]![Text279]
    Set rs = .OpenRecordset
End With

You can learn more about the different types of parameters, and when to use which one, in this answer

Sign up to request clarification or add additional context in comments.

1 Comment

wow that was quick - Thank you. I have updated my question to incorporate your answer but still not working unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.