[ Team LiB ] |
Recipe 14.5 Pass Parameters to Stored Procedures from Pass-Through Queries in an MDB14.5.1 ProblemYou are calling stored procedures that require parameters by using pass-through queries. How can you pass parameters to the pass-through query from your form? If you include a reference to the form in the pass-through query, you get an error message from SQL Server. 14.5.2 SolutionPass-through queries are not processed in the same way as regular Access queries against linked tables. The SQL syntax you type in a pass-through query is passed directly to SQL Server. Any references to forms or controls on forms in a pass-through query are meaningless to SQL Server, so you must pass the actual values for your parameters. A pass-through query has three important properties:
Figure 14-12 shows the properties sheet for a pass-through query to the pubs sample database in SQL Server. Figure 14-12. Pass-through query propertiesThe most versatile way to set these properties is to write a procedure that sets them at runtime by using a DAO QueryDef object. You'll then need to set parameter values to the procedure for connection information, the SQL string that comprises the pass-through query, and whether or not the query returns records. To modify a pass-through query at runtime, follow these general steps:
Figure 14-13. The sample form used to test the acbPassThrough procedure
14.5.3 DiscussionThe acbPassThrough procedure can modify any saved pass-through query by using the DAO QueryDef object: Dim qdf As DAO.QueryDef Dim strConnect As String Set qdf = CurrentDb.QueryDefs(QueryName) There is an optional parameter for the ConnectStr argument. If a connection string is not supplied, the one saved with the QueryDef object is used: If IsMissing(ConnectStr) Then strConnect = qdf.Connect Else strConnect = CStr(ConnectStr) End If The properties for the query are then set to the values passed into the procedure: qdf.Connect = strConnect qdf.ReturnsRecords = ReturnsRecords qdf.SQL = SQLStatement This actually permanently saves changes to the query—if you open the query in design view after executing the procedure, you'll see the last properties that were set. The values on the form are simply collected from the relevant text boxes and combo boxes, and passed to the procedure. Then the form is requeried and the new results of the pass-through query are loaded as the record source of the form. Access lets you create ad hoc queries by using the CreateQueryDef syntax and specifying an empty string for the parameter name. However, using a previously saved query eliminates the overhead of creating a new object from scratch and then discarding it.
|
[ Team LiB ] |