0

I'd like to create and open a select query based on the user's current form filters. I can do this in VBA by parsing the form's Me.Filter string, extracting the bits I need and building a WHERE statement. However, putting in the all the required logic, punctuation and syntax is going to be a pain.

So my question, before I do all that is: is there any existing function to do this?

Thanks.

2
  • 1
    It doesn't have to be a pain, if you use the form's recordsource as base of the select query. Commented Oct 20, 2015 at 14:51
  • Thanks, but source isn't the problem and I can't see any method to get an SQL WHERE string from the recordsource property. Commented Oct 20, 2015 at 15:26

1 Answer 1

2

Maybe my comment was a little cryptic.

Let's say the recordsource of your form is myQuery.

The form is filtered, Me.Filter = myQuery.field1 LIKE 'asdf*' AND myQuery.field2 = 42

So your select query is e.g.

SELECT field1, field3 
FROM myQuery
WHERE myQuery.field1 LIKE 'asdf*' AND myQuery.field2 = 42

or

myQuerydef.SQL = "SELECT field1, field3 FROM " & Me.Recordsource & _
    " WHERE " & Me.Filter

So I don't quite see where the problem is.

The answer to your question is no, there is no function - but you shouldn't need anything besides Me.Filter.

Edit as suggested by HansUp:

If the RecordSource of your form currently isn't a single query, but a SELECT statement, create a named query from that SELECT statement, and use that query as RecordSource.

Sign up to request clarification or add additional context in comments.

2 Comments

True, but then for this use case the solution is to have a single query as RecordSource - if it currently isn't, then create a named query from the current RecordSource. @HansUp
Didn't realize you could use Me.Filter directly as the WHERE ( myQuerydef.SQL = "SELECT field1, field3 FROM " & Me.Recordsource & " WHERE " & Me.Filter). Thanks, new one for the toolkit.

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.