Which Form Is Open Programmatically?

databasedonr

Registered User.
Local time
Today, 05:32
Joined
Feb 13, 2003
Messages
163
How can I tell which form is open programmatically?

I have a form used to update records - and these records can be accessed from one of two forms. After the update process has run in my code, I want to send the user back to the form from which they came - but I don't know how to identify that.

Here's an example: I have a BrowseRecord and a FindRecord form. From either form, I have an Edit Record button that opens a EditRecord form. Once the record is edited, I want to return the user to the form they used to get to the edit form. As I am leaving the intial form open, I thought I could do something like

If Form_BrowseRecord = "Open" Then
Form_BrowseRecord.SetFocus
Else
Form_FindRecord.SetFocus

But I am not sure how to identify which form is open. I've waded through the Object Browser etc. but can't find the answer....

My apologies if this should rather be in the VBA forum.

Thanks in advance.
Don
 
You can keep track of which form called the Edit Record form by using the Edit Record form's OpenArgs or tag property.

When you open the Edit Record form, just use the OpenArg parameter of the DoCmd.OpenForm method.

If you want to use the tag property, then you can set the tag property from the other forms like this:
Forms![Edit Record].Tag=Me.Name
 
Not quite behind you

But trying to catch up.

I'm not quite certain how to implement this -- I'll give you an example.

If I am opening the EditRecord form from the BrowseRecords form, I might do something like this:

DoCmd.OpenForm strForm, , , strLinkCrit, , , Browse

to set the word Browse as my OpenArg, right? Then, to get back to whence I came, I would use something like this:

If Form.OpenArgs = Browse Then
Form_BrowseRecords.SetFocus
Else
Form_FindRecord.SetFocus

I've tried this and it isn't quite right - I get the Else form -- so it isn't reading my openargs.
 
I take it VBA syntax is not your strong point? :D

Try this
1- change the open args call to include the full name of the form ("BrowseRecords" instead of "Browse"). It just makes it easier - and include the quotation marks!
2- use this code when you close the Edit form:
Code:
Forms(Me.OpenArgs).SetFocus
 
Strong Suit?

You're right - on a couple of counts. VBA period is not my strong suit, but I am learning - and working at it - every day. That is why help from people like yourself and this forum in general is invaluable.

That worked -- thank you so much!

Don
 
Keep in mind that the code I gave you:
Forms(Me.OpenArgs).SetFocus
will only work if that Edit Record form is opened a form that embeds it's name in the OpenArgs parameter of the DoCmd.OpenForm method.

If you ever open the Edit Record form by itself, or if other forms might open it, you will want to change that code, or surround that code with basic error checking like this:
Code:
On Error Resume Next
Forms(Me.OpenArgs).SetFocus
On Error Goto 0

Good luck!
 
Thanks again -- I will remember that.

In this current application, I only have two forms that I use, and I have set OpenArgs on both of them. But, this is a pretty handy way to do things -- and I'll keep error handling stuff in mind.

If you have any suggestions for good sources (other than this site) to help improve my VBA skills, feel free to email me or post -- I'd be grateful.

Thanks again, that worked a treat.

Don
 

Users who are viewing this thread

Back
Top Bottom