Determing active form to process function

S

scott

I suppressed the Ctl-P shortcut by creating a ^p autokey macro that does
nothing. Now I want Ctl-P to execute a function if a specific form is open
and do nothing otherwise. I added a condition to the macro that if the
active from is Data Entry ([Screen].[ActiveForm].[Name]="Data Entry"), it
should run the function and the next command is StopMacro. The function runs
correctly if the active form is Data Entry, but it returns the error "The
Object you referenced in the Visual Basic Procedure as an OLE object isn't an
OLE Object" if the screen the user is on is a report. What is needed to get
it to evaluate the way I want it to?
 
A

Allen Browne

Screen.ActiveForm.Name is the best way to determine the name of the active
form.

As you say, that generates an error if no form is active (e.g. a report is
active.) Just add error-handling to deal with that error.

If error handling is new, here's an introduction:
http://allenbrowne.com/ser-23a.html
 
S

scott

Allen Browne said:
Screen.ActiveForm.Name is the best way to determine the name of the active
form.

As you say, that generates an error if no form is active (e.g. a report is
active.) Just add error-handling to deal with that error.

If error handling is new, here's an introduction:
http://allenbrowne.com/ser-23a.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

scott said:
I suppressed the Ctl-P shortcut by creating a ^p autokey macro that does
nothing. Now I want Ctl-P to execute a function if a specific form is
open
and do nothing otherwise. I added a condition to the macro that if the
active from is Data Entry ([Screen].[ActiveForm].[Name]="Data Entry"), it
should run the function and the next command is StopMacro. The function
runs
correctly if the active form is Data Entry, but it returns the error "The
Object you referenced in the Visual Basic Procedure as an OLE object isn't
an
OLE Object" if the screen the user is on is a report. What is needed to
get
it to evaluate the way I want it to?
 
S

scott

I'm not sure what happened in my last reply (there was an entry, but no text
that I could see), so I am putting it in again. My apologies if it is posted
twice.

This gets me closer, but I'm still having trouble getting it to behave. I
now have a one line autokey macro with no condition that just calls a
function. The function then looks to see if the Data Entry form is open and
runs code if it is. The end of the code puts up the Print Dialog box so the
user can print the report. If the report is open, the resulting error (2475)
is trapped and it then just puts up the print dialog box (DoCmd.RunCommand
acCmdPrint). This works fine as long as the user doesn't canel printing. It
even works fine if the user cancels printing and they invoked the command
from the Data Entry form. The function traps the error (2501) that results
from canceling the printing and just goes back to where it was.

If the user is in the report when ^p is invoked, it works fine if the user
does not cancel. If they cancel, the Micosoft Visual Basic error message
(2501) comes up that the RunCommand action was canceled and gives the buttons
End, Debug and Help, not a screen I want users to see. When I click Debug,
it goes to Line1 (see Code) and when I reset, it displays the Macro Action
Failed screen. I've tried a few variations on the error trapping, but I get
the same (or worse) results. Can you help me get this to behave correctly?

Public Function DisplayRequest()
On Error GoTo Err_DisplayRequest

Dim stDocName As String
Dim stLinkCriteria As String
Dim Problem As Integer
Dim intRecCount As Integer
Dim intAmount As Currency
Dim intEmpty As Integer
Dim intChkreqid As Integer

If [Screen].[ActiveForm].[Name] = "Data Entry" Then
Runs some code
Else
Line1: DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "Check Request"
End If

Exit_DisplayRequest:
Exit Function

Err_DisplayRequest:
Select Case Err.Number
Case 2501
Resume Next
Case 2475
GoTo Line1
Case Else
MsgBox Err.Description
Resume Exit_DisplayRequest
End Select

End Function

Allen Browne said:
Screen.ActiveForm.Name is the best way to determine the name of the active
form.

As you say, that generates an error if no form is active (e.g. a report is
active.) Just add error-handling to deal with that error.

If error handling is new, here's an introduction:
http://allenbrowne.com/ser-23a.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

scott said:
I suppressed the Ctl-P shortcut by creating a ^p autokey macro that does
nothing. Now I want Ctl-P to execute a function if a specific form is
open
and do nothing otherwise. I added a condition to the macro that if the
active from is Data Entry ([Screen].[ActiveForm].[Name]="Data Entry"), it
should run the function and the next command is StopMacro. The function
runs
correctly if the active form is Data Entry, but it returns the error "The
Object you referenced in the Visual Basic Procedure as an OLE object isn't
an
OLE Object" if the screen the user is on is a report. What is needed to
get
it to evaluate the way I want it to?
 
S

scott

Thanks for your help. I solved the problem by creating another function that
is called when the 2475 error is trapped. Now it behaves the way I want it
to.

Allen Browne said:
Screen.ActiveForm.Name is the best way to determine the name of the active
form.

As you say, that generates an error if no form is active (e.g. a report is
active.) Just add error-handling to deal with that error.

If error handling is new, here's an introduction:
http://allenbrowne.com/ser-23a.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

scott said:
I suppressed the Ctl-P shortcut by creating a ^p autokey macro that does
nothing. Now I want Ctl-P to execute a function if a specific form is
open
and do nothing otherwise. I added a condition to the macro that if the
active from is Data Entry ([Screen].[ActiveForm].[Name]="Data Entry"), it
should run the function and the next command is StopMacro. The function
runs
correctly if the active form is Data Entry, but it returns the error "The
Object you referenced in the Visual Basic Procedure as an OLE object isn't
an
OLE Object" if the screen the user is on is a report. What is needed to
get
it to evaluate the way I want it to?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top