Interface form min and max help

Wayne, I stumbled onto an error. If you open a form from the menu and leave it open and try to close the app it gives an run-time error 2450 cant find the form "Background". I have no clue as to how to fix this? Please help :confused: It also wont let you run the debug to see the code in question. It just closes the app?

Thanks,
Chad
 

Attachments

Last edited:
Wayne can I use something like this?

Code:
Err_imgCmdLogOff_Click:
If Err.Number = 2450 Then 
Resume Next
Else
MsgBox Err.Number & " - " & Err.Description
Resume Exit_imgCmdLogOff_Click
End If
End Sub
 
Oxi,

No, you need to put the error trapping in your Timer event, that's
where you'll be setting focus to a form that might not be open.

Code:
On Error GoTo ErrHandler

'
' Your regular timer code here
'
Exit Sub

ErrHandler:

If Eff = 2450 Then
   Me.txtFormName = "BackGround"
   Forms!Background.SetFocus
   Exit Sub
End If
MsgBox("Error - " & Err.Description)
End Sub

Wayne
 
Wayne, I added the code to my timer event and im getting an error on the "Eff" section of the code? I made it "Err" then it goes back to the same thing like I never added an error trap. Here is what I have... Thanks!!
Code:
Private Sub Form_Timer()
On Error GoTo ErrHandler
Dim Response As Boolean

'//This is so when the app is maximized or minimized the app will open to what was open or on top.
Response = IsAccessMaximized()
If Response And Me.txtAppMode = "Min" Then
  Select Case Me.txtFormName
  
     Case "frmUpdateEmployees"
       Forms!FrmUpdateEmployees.SetFocus
       
     Case "frm_LBStoMLFConversion"
       Forms!frm_LBStoMLFConversion.SetFocus
    
     Case "frm_ReportsSwitchboard"
       Forms!frm_ReportsSwitchboard.SetFocus
       
     Case "frmMaindB"
     Forms!frmMaindB.SetFocus
     
     End Select
  Me.txtAppMode = "Max"
End If

'//This is how clock is viewed hours minutes AM/PM
Me!lblClockMain.Caption = Format(Now, "h:mm AMPM")

     Exit Sub
   
ErrHandler:

If Eff = 2450 Then
   Me.txtFormName = "BackGround"
   Forms!Background.SetFocus
   Exit Sub
End If
MsgBox ("Error - " & Err.Description)

End Sub
 
Last edited:
Oxi,

What's the error?

And what's the "Eff section of code"?

Wayne
 
Wayne, Its a Compile Error: Variable not defined and it takes me to the Eff in the code below.
Code:
If Eff = 2450 Then
It outlines the Eff in blue...

Thanks,
Chad
 

Attachments

  • error.JPG
    error.JPG
    22.8 KB · Views: 83
Last edited:
I figured it was a typo earlyer and when I fixed it to Err it did the same thing as before. Then I noticed Background had its g in caps so I fixed that now, there is a new Compile Error: Label not defined see picture. Thanks!
 

Attachments

  • error 2.JPG
    error 2.JPG
    21.5 KB · Views: 76
Last edited:
Oxi,

Whatever "SomeLabel" is it just has to appear twice as shown below:

On Error Goto SomeLabel

' regular code
Exit Sub

SomeLabel:

Wayne
 
Im not quit sure what you are talking about? actually im not quite sure what all this is! Im still stuck on why it has to be in the timer event... Is this what you mean if so I get the same error 2450. Or is "SomeLabel" A label created on the background form and instead of somelabel I use the name of the label? Im lost...Thanks!

Code:
Private Sub Form_Timer()
On Error GoTo SomeLabel
Dim Response As Boolean

'//This is so when the app is maximized or minimized the app will open to what was open or on top.
Response = IsAccessMaximized()
If Response And Me.txtAppMode = "Min" Then
  Select Case Me.txtFormName
  
     Case "frmUpdateEmployees"
       Forms!FrmUpdateEmployees.SetFocus
       
     Case "frm_LBStoMLFConversion"
       Forms!frm_LBStoMLFConversion.SetFocus
    
     Case "frm_ReportsSwitchboard"
       Forms!frm_ReportsSwitchboard.SetFocus
       
     Case "frmMaindB"
     Forms!frmMaindB.SetFocus
     
     End Select
  Me.txtAppMode = "Max"
End If

'//This is how clock is viewed hours minutes AM/PM
Me!lblClockMain.Caption = Format(Now, "h:mm AMPM")

Exit Sub

SomeLabel:

End Sub
 
Oxi,

The SomeLabel part of the code is the Error Handler.
When we can't Set Focus to our last form, just Set Focus to BackGround.

Code:
Private Sub Form_Timer()

Dim Response As Boolean

On Error GoTo SomeLabel ' <-- In the event of an error (like not having the form open).

'//This is so when the app is maximized or minimized the app will open to what was open or on top.

Response = IsAccessMaximized()
If Response And Me.txtAppMode = "Min" Then
  Select Case Me.txtFormName
  
     Case "frmUpdateEmployees"
       Forms!FrmUpdateEmployees.SetFocus
       
     Case "frm_LBStoMLFConversion"
       Forms!frm_LBStoMLFConversion.SetFocus
    
     Case "frm_ReportsSwitchboard"
       Forms!frm_ReportsSwitchboard.SetFocus
       
     Case "frmMaindB"
     Forms!frmMaindB.SetFocus
     
     End Select
  Me.txtAppMode = "Max"
End If

'//This is how clock is viewed hours minutes AM/PM

Me!lblClockMain.Caption = Format(Now, "h:mm AMPM")

Exit Sub ' <-- This keeps the code from "falling" into the
         ' <-- error handler inadvertantly.

SomeLabel:
'
' If there was an error condition, process it here
'
' If it's because we can't set focus because a form's closed
' then just Set Focus to the BackGround form and exit.
'
' Otherwise just report the error and exit.

If Err = 2450 Then ' <-- 2450 is the Err.Number for "can't find form by that name ..."?
   Me.txtFormName = "BackGround"
   Forms!Background.SetFocus
   Exit Sub
End If
'
MsgBox ("Error - " & Err.Description)
Me.txtFormName = "BackGround"
Forms!Background.SetFocus

End Sub

Wayne
 
Hello,

Allow me to suggest the approach i would take...

I would create a new standard module where we can store data such as Enumations, constants, etc... we'll call this module bas_GlobalDeclarations or something meaningfull...

bas_GlobalDeclarations
Code:
Option Compare Database
Option Explicit

Public Type RECT
        left As Long
        top As Long
        right As Long
        bottom As Long
End Type


Public Type POINTAPI
        X As Long
        Y As Long
End Type


Public Type WINDOWPLACEMENT
        Length As Long
        flags As Long
        showCmd As Long
        ptMinPosition As POINTAPI
        ptMaxPosition As POINTAPI
        rcNormalPosition As RECT
End Type

Then i would create a new class module and encapsulate some rather useful API's to manipulate and obtain window information such as minimize, maximze, etc...

cWindow
Code:
Option Compare Database
Option Explicit

Private Declare Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Function GetWindowPlacement Lib "user32" ( _
                        ByVal hWnd As Long, _
                        lpwndpl As WINDOWPLACEMENT) _
                        As Long

Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8

Private Const SW_RESTORE = 9

[COLOR="SeaGreen"]'// set focus to window[/COLOR]
Public Sub Focus(hWnd As Long)
    SetFocus (hWnd)
End Sub

[COLOR="seagreen"]'// check window minimized state[/COLOR]
Public Function Minimized(hWnd As Long) As Boolean
Dim wndPl As WINDOWPLACEMENT
Dim lngRetVal As Long
    lngRetVal = GetWindowPlacement(hWnd, wndPl)
    If (wndPl.showCmd = SW_SHOWMINIMIZED) Then
        Minimized = True
    End If
End Function

[COLOR="seagreen"]'// check window maximized state[/COLOR]
Public Function Maximize(hWnd As Long) As Boolean
Dim wndPl As WINDOWPLACEMENT
Dim lngRetVal As Long
    lngRetVal = GetWindowPlacement(hWnd, wndPl)
    If (wndPl.showCmd = SW_SHOWMAXIMIZED) Then
        Maximize = True
    End If
End Function

[COLOR="seagreen"]'// check window normal state[/COLOR]
Public Function Normal(hWnd As Long) As Boolean
Dim wndPl As WINDOWPLACEMENT
Dim lngRetVal As Long
    lngRetVal = GetWindowPlacement(hWnd, wndPl)
    If (wndPl.showCmd = SW_SHOWNORMAL) Then
        Normal = True
    End If
End Function

Then i would create a new standard module to include all of the code required to minipulate the interface, at this point i would create a variable to hold the handle to any form that's open from the menu...

bas_DBInterface
Code:
Option Compare Database
Option Explicit

[COLOR="SeaGreen"]'// public variable to hold the handle to an open
'// form that has been open from the main menu[/COLOR]
Public m_lngLastWindowHwhd As Long

As previously mentioned, every form that is opened from the main menu should populate the lngLastWindowHwhd variable with it's handle on the OnOpen event or OnLoad

The variable should be cleared on the form's OnClose event or OnUnload and reset it to a 0 value...

Form_SomeForm
Code:
Option Compare Database
Option Explicit

Private Sub Form_Close()
    [COLOR="seagreen"]'// reset handle pointer[/COLOR]
    m_lngLastWindowHwhd = 0
End Sub

Private Sub Form_Load()
    [COLOR="seagreen"]'// save handle pointer[/COLOR]
    m_lngLastWindowHwhd = Me.hWnd
End Sub

Now that we have all the goodies in place, we will use the OnResize event to find out and manipulate our interface...

Form_BackGround
Code:
[COLOR="seagreen"]'// window class instance goodies[/COLOR]
Private Window As New cWindow

Private Sub Form_Open(Cancel As Integer)
    [COLOR="seagreen"]'// maximize current window[/COLOR]
    DoCmd.Maximize
End Sub

Private Sub Form_Resize()
    [COLOR="seagreen"]'// test if window is maximized[/COLOR]
    If Window.Maximize(Me.hWnd) Then
        [COLOR="seagreen"]'// set the focus to the last form
        '// that was opened from your menu (if any) [/COLOR]
        If Not m_lngLastWindowHwhd = 0 Then
             Window.Focus m_lngLastWindowHwhd
        End If
    [COLOR="seagreen"]'// test if window is in normal state,
    '// if it is, we'll assume that the user
    '// has requested the application's attention[/COLOR]
    ElseIf Window.Normal(Me.hWnd) Then
        [COLOR="seagreen"]'// maximize form[/COLOR]
        DoCmd.Maximize
    End If
End Sub

Private Sub imgCmdMinimizeButton_Click()
    [COLOR="seagreen"]'// minimize application[/COLOR]
    DoCmd.RunCommand acCmdAppMinimize
End Sub

And there you have it... try it and let us know how it turns out :)
 
Last edited:
Wayne and DXecutioner, I posted this error trap question on another forum as well as this one to widen a solution and someone came up with a simple solution. Just add the line (On Error Resume Next) to the on close event and this works without adding the error trap in the timer event of my background form. Is this solution a good one or should I look for a better one? Dom this app is from your ACC2000 XLogonXP example and thank you for that! Dom I’m new to access and all the code you gave is great but I have no clue what it does or where it would go, Wayne has been very patient with me on helping me with this interface and I would like to thank you for that!! If someone would show me on how to add this code and why I would be happy to learn!!!!

Example:
Code:
Private Sub Form_Close()
On Error Resume Next
'//Sets focus back to the background form
Forms!BackGround!txtFormName = "Background"

End Sub

Thanks,
Chad
 
Oxi,

In this case, you can use the On Error Resume Next.

In your Timer event, just put that as the first instruction and
remove the previous error code.

The only error you're likely to encounter in your Timer Event is
that the form to set focus to is not currently open.

The Resume Next will essentially ignore the error leaving your
BackGround form with the focus ... not really a bad thing.

Most people will generally not use the On Error Resume Next
because it is essentially saying "Ignore all errors", leading
to a lack of control over the code.

BUT, it is in your Timer Event that you need to put the code.

Wayne
 
Wayne, so what your saying is take the code out of the forms and put into the timer event? Make it the first thing in the timer event code?

Thanks,
Chad
 
I understand the fact that you're new to access, but perhaps you may want to reread my last post; in either case i've attached a sample with the revised code...
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom