HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

A function that implements a query

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementsqueryfunctionthat

Problem

I have some parameterized queries in Access 2010.
They are often used in VBA functions having the same parameters.
In such cases, I give the query and function the same name.
The following is such a function (from my actual project):

Public Function get_assignments(e_id As Long, yr As Integer, wk As Integer) As DAO.Recordset
    Dim db As DAO.Database
    Set db = CurrentDb
    Dim qd As DAO.QueryDef
    Set qd = db.QueryDefs!get_assignments
    qd.Parameters![e_id] = e_id
    qd.Parameters![year] = yr
    qd.Parameters![week] = wk
    Set get_assignments = qd.OpenRecordset
    qd.Close
End Function


Both the query and function are named get_assignments and things work fine.

Am I asking for trouble? While I don't usually add "type" suffixes to names, I could rename the query as get_assignments_qry or even rename the function as get_assignments_fn. What's best?

Feel free to give feedback on any aspects of this code.

Solution

Namespace scope and pollution is often a problem in programming, and when reading code, if there are two usages of a particular identifier, a reader may not understand exactly which one you mean, and the complier may have different scope rules than you expect, so it is worthwhile to not re-use names.

In this particular case, consider using query instead of get in the public API

Public Function query_assignments(e_id As Long, yr As Integer, wk As Integer) As DAO.Recordset

Code Snippets

Public Function query_assignments(e_id As Long, yr As Integer, wk As Integer) As DAO.Recordset

Context

StackExchange Code Review Q#116926, answer score: 5

Revisions (0)

No revisions yet.