Question

If you define a query in Microsoft Access,

For instance,

SELECT * FROM T_Employees t WHERE t.LastName=[LastName]

a popup dialog will display asking you to enter a value for LastName. LastName a named parameter.

Now from here one can also create a view

CREATE VIEW paramTest AS SELECT * FROM T_Employees t WHERE t.LastName=[LastName]

But when calling this view in another query, the parameter is ignored....

So how does one call the view created above from another query and specify the value of the named parameter within that view? (preferably without VBA, I'm trying to use it to write a query involving a view in SQuirreL SQL).

Was it helpful?

Solution

That CREATE VIEW statement creates the named query, paramTest, with this SQL:

SELECT *
FROM T_Employees AS t
WHERE (((t.LastName)=[LastName]));

When running that query, the db engine does not interpret [LastName] to be a pararmeter, so doesn't pop up the parameter dialog, because the name of the parameter is the same as the field name. Essentially, the db engine returns the rows where each LastName field is equal to itself ... all rows.

If you want to create a parameter query which works, give the parameter a name which doesn't match any field names in the data source. And, as Ron mentioned. CREATE VIEW won't allow you to create a parameter query. However you can create one with CREATE PROCEDURE.

CurrentProject.Connection.Execute "CREATE PROCEDURE paramTest " & _
    "(Which_LastName TEXT(255)) AS" & vbCrLf & _
    "SELECT * FROM T_Employees t WHERE t.LastName=[Which_LastName];"

OTHER TIPS

VIEWS do not allow parameters. Think of them more like tables than queries. Normally a stored procedure is used when parameters are needed. In order to incorporate a parameter in a view, you must create a table with the parameter value and include it in the VIEW SQL. The parameter table typically includes a USER ID to avoid concurrent request conflicts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top