BlackDog
BlackDog

Reputation: 317

Use current object name in Access SQL

I have a small program in MS Access with 3 main search forms: RecentOrdersSearch, OldOrdersSearch, SpecialOrdersSearch. All order records have the same structure, they are in the same Orders table.

The thing is, whenever I want to perform an action on a query result, I have to create code in triple for Recent, Old and Special because the reference for the data source changes: there's a query using [Forms]![RecentOrdersResults]![Client], another uses [Forms]![OldOrdersResults]![Client] etc...

It would make my database cleaner and save me a lot of time if there was some way to implicitly refer to the running form instead of calling it explicitly, something like [Current]![Client], so I would need one query instead of three.

I hope I was clear enough in my description, excuse my strenuous English.

Thanks in advance

Upvotes: 0

Views: 285

Answers (2)

iDevlop
iDevlop

Reputation: 25262

You could write a function that returns the contents of the [Client] control in the ActiveForm. Something like (quickly written and not tested):

Function GetClient()
    GetClient = Screen.ActiveForm.Client.Value
end Function

and use that in your queries as a criteria. =GetClient()

Upvotes: 1

Marty
Marty

Reputation: 2016

When you are executing VBA code in an Access form, "Me" refers to the form that is currently in focus and whose code is now running.

Upvotes: 0

Related Questions