Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is the active object a form or a report?....

Status
Not open for further replies.

Wrangler36

Technical User
Dec 5, 2005
24
0
0
US
I need to use the Screen.ActiveForm and Screen.ActiveReport commands to hide the active form or close the active report. If I use the Screen.ActiveForm command and it is a report that is active, an error occurs. If I use the Screen.ActiveReport command and it is a form that is active, an error occurs.

How can I detect whether a form or a report is active before I use the Screen object to reference it?

Thanks.
 
Could you try one or the other, then just trap the error?

Code:
Public Sub testActiveObject()
  On Error GoTo Errlbl
     Forms("frmMain").SetFocus
     MsgBox Screen.ActiveReport.Name
     Exit Sub
Errlbl:
  If Err.Number = 2476 Then
    MsgBox Screen.ActiveForm.Name
  Else
    MsgBox Err.Number & "  " & Err.Description
  End If
End Sub

I have a question. I have never used the Acitve screen to reference an object. It just seems that you would know what is active. Is there not a better way for you to reference these objects? Is seems sloppy to me, but I am sure that in certain instaces it works well, I just can not see it.
 
How are ya Blitzen444 . . .

The following function returns [blue]Frm[/blue] for forms, [blue]Rpt[/blue] for reports, and an [blue]empty string[/blue] "" if neither is active:
Code:
[blue]Public Function ActiveType() As String
   Dim typNam As String, flg As Boolean
   
On Error GoTo ActiveErr
   typNam = Screen.ActiveForm.Name
   ActiveType = "Frm"
   Exit Function
   
Reports:
   [purple][b]flg[/b][/purple] = True
   typNam = Screen.ActiveReport.Name
   ActiveType = "Rpt"
   Exit Function

ActiveErr:
   If Not [purple][b]flg[/b][/purple] Then Resume Reports
   
End Function[/blue]

Calvin.gif
See Ya! . . . . . .
 
For the fun of it, just playing around with the latter one, returning both the type of object, and if form or report, also the name through an UDT.

[tt]Enum WassIt
Form
Report
Dunno
End Enum

Type WossName
Name As String
Type As WassIt
End Type

Function GetActiveObjectType() As WossName

Dim strName As String
Dim udtWossName As WossName

On Error Resume Next

With udtWossName
strName = Screen.ActiveForm.Name
If Err.Number = 0 Then
.Type = Form
Else
Err.Clear
strName = Screen.ActiveReport.Name
If Err.Number = 0 Then
.Type = Report
Else
.Type = Dunno
' strName = "Haven't the foggiest..."
Err.Clear
End If
End If
.Name = strName
End With

GetActiveObjectType = udtWossName

End Function[/tt]

So, I think the bottom line here, is that the "brute force" method - i e triggering an exception, is probably the way to go with native methods.

Here's a little sample call of hiding active object, if it's form or report

[tt] Dim udtWossName As WossName
udtWossName = GetActiveObjectType
Select Case udtWossName.Type
Case WassIt.Form
Forms(udtWossName.Name).Visible = False
Case WassIt.Report
Reports(udtWossName.Name).Visible = False
Case Else
MsgBox "OI - nothing has focus ..."
End Select[/tt]

(Type 0 = Form, 1 = Report, 2 = Dunno - or use as above)

But - I'm just thinking - these focus stuff thingies might be somewhat unreliable - wouldn't looping the forms/reports collection (collection of open forms/reports) be a more safe method of determining which form or report is open?

Roy-Vidar
 
Thanks Roy. That is basically what I was hinting at. The focus "thingie" just seems sloppy.
 
Thanks all!

MajP, the reason I don't know whether a form or report is active is that I keep a number of forms open but hidden, and I make them visible when they are needed. When the user clicks a certain menu option to activate a certain form, a global function is called to use the Screen.ActiveForm command to make the form visible.

The problem is that when a report is open and the user tries to switch to a form, that's where I get the conflict between whether it is a form or report that's active.

I originally thought about just trapping the error, as you all have suggested, but I thought maybe there was a built-in function that I was not aware of or a more efficient way to do it.

I guess I will have to go with the error trapping method.
 
Here is another spin on it without using error trapping or the Screen object. I think you could take this and Roys and make a very useful function with more utility.

I use the Application.CurrentObjectType and two functions.
One returns the object, the other tells me what it is. The second function is really not necessary because all it really does is change "acForm" long enum type to to a string "Form".

Here are the constants if you want to complete the code.
acTable The active object is a table.
acQuery The active object is a query.
acForm The active object is a form.
acReport The active object is a report.
acMacro The active object is a macro.
acModule The active object is a modul

Public Function getActiveObject() As Object
Dim ObjType As Long
Dim objActive As Object
ObjType = Application.CurrentObjectType
Select Case ObjType
Case acForm
Set getActiveObject = Screen.ActiveForm
Case acReport
Set getActiveObject = Screen.ActiveReport
Case Else
'could add the other cases

End Select
Exit Function
Errlbl:
MsgBox Err.Number & " " & Err.Description
End Function

Public Function getActiveObjectType() As String
Dim ObjType As Long
ObjType = Application.CurrentObjectType
Select Case ObjType
Case acForm
getActiveObjectType = "Form"
Case acReport
getActiveObjectType = "Report"
Case Else
'could add the other cases
End Select
Exit Function
Errlbl:
MsgBox Err.Number & " " & Err.Description
End Function

Public Function testActive()
DoCmd.OpenForm "frmMain"
MsgBox getActiveObject.Name
MsgBox getActiveObjectType
DoCmd.OpenReport "rptOne", acViewPreview
MsgBox getActiveObject.Name
MsgBox getActiveObjectType
End Function
 
Thanks MajP. I knew there had to be a built-in function available. The Application.CurrentObjectType works great!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top