0

I have a form that runs some sql on the form load and three sort buttons on the form that all repeat the sql in their own private function. The only difference is the sort order. When I need to update the where clause, I have to update it 4 times. I would like to created a parameterized function like

function getFormRecs(arg){
    sql...
    . 
    .
    .
    order by 'arg'
{

where arg is the argument passed in from the form control or event.

Then I would like the form load and the 3 sort buttons on the form to call the function getFormRecs(), while passing in the arg, like

getFormRecs('formload')
getformRecs('sorta')
getformRecs('sortb')
getformRecs('sortc')

I would define the correct sort field in the top of the function based on the arg passed in.

The end result is simply to reuse a function or Sub-routine that contains nearly identical long winded sql.

0

2 Answers 2

2

This seems to be about what you're asking for:

Function getFormRecs(ByVal arg As String)
    getFormRecs = "sql ... " & _
                  "." & _
                  "." & _
                  "." & _
                  "order by " & arg
End Function
Private Sub Form_Load()
    Dim sql As String
    sql = getFormRecs("formload")
    '... Logic that uses sql
End Sub
Sign up to request clarification or add additional context in comments.

Comments

2

I'm unsure why you need a function. You could start with a base query which does not include an ORDER BY.

SELECT id, fld2, fld3
FROM YourTable;

Then re-use that same query elsewhere by including it as the data source in a new SELECT statement where you add the specific ORDER BY you require.

SELECT q.id, q.fld2, q.fld3
FROM qryBase AS q
ORDER BY q.fld3;

Comments

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.