Search code examples
sqlms-accessviewms-access-2003squirrel-sql

Passing parameters from one query to a view in MS-Access?


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).


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];"