How Can I Change these If Statements into a Case Statement?

yang0837

New member
Local time
Today, 14:38
Joined
Dec 21, 2011
Messages
4
Sorry if this sounds ...dumb :o, but I need help changing these if statements into a case statement.

Private Sub Undo_LostFocus()
If CurrentProject.AllForms("frmPFS_PtRefund").IsLoaded = True Then
Forms![frmPFS_PtRefund]![btnAddNew].SetFocus
Else
If CurrentProject.AllForms("frmPPFS_PtRefund").IsLoaded = True Then
Forms![frmPPFS_PtRefund]![btnAddNew].SetFocus
Else
If CurrentProject.AllForms("frmPFS_InsRefund").IsLoaded = True Then
Forms![frmPFS_InsRefund]![btnAddNew].SetFocus
Else
Forms![frmPPFS_InsRefund]![btnAddNew].SetFocus
End If
End If
End If
End Sub
 
The nearest Select Case construct would be:-

Code:
Private Sub Undo_LostFocus()

    Select Case True
    
        Case CurrentProject.AllForms("frmPFS_PtRefund").IsLoaded
            Forms![frmPFS_PtRefund]![btnAddNew].SetFocus
            
        Case CurrentProject.AllForms("frmPPFS_PtRefund").IsLoaded
            Forms![frmPPFS_PtRefund]![btnAddNew].SetFocus
            
        Case CurrentProject.AllForms("frmPFS_InsRefund").IsLoaded
            Forms![frmPFS_InsRefund]![btnAddNew].SetFocus
            
        Case Else
            Forms![frmPPFS_InsRefund]![btnAddNew].SetFocus
            
    End Select
    
End Sub

Chris.
 
You have to set Focus to the Form before setting Focus to the Control:
Code:
Private Sub Undo_LostFocus()

 Select Case True
 
  Case CurrentProject.AllForms("frmPFS_PtRefund").IsLoaded
   Forms("frmPFS_PtRefund).SetFocus
   Forms("frmPFS_PtRefund)("btnAddNew").SetFocus
  
  Case CurrentProject.AllForms("frmPPFS_PtRefund").IsLoaded
   Forms("frmPPFS_PtRefund").SetFocus
   Forms("frmPPFS_PtRefund")("btnAddNew").SetFocus
  
  Case CurrentProject.AllForms("frmPFS_InsRefund").IsLoaded
   Forms("frmPFS_InsRefund").SetFocus
   Forms("frmPFS_InsRefund")("btnAddNew").SetFocus
  
  Case Else 
   Forms("frmPPFS_InsRefund")("btnAddNew").SetFocus

End Select

End Sub


Linq ;0)>
 
Well yes, if we want it to also work then we also need to make sure the Form running the code is not Modal.

Chris.
 
...we also need to make sure the Form running the code is not Modal.
Good point, Chris! Also that the Form running the code wasn't opened via code from another Form, in Dialog Mode, which also sets Modal to Yes.

Linq ;0)>
 
All the unknown variables that make Access such an interesting programme. :)

Chris.
 
You have to set Focus to the Form before setting Focus to the Control:
Code:
Private Sub Undo_LostFocus()
 
 Select Case True
 
  Case CurrentProject.AllForms("frmPFS_PtRefund").IsLoaded
   Forms("frmPFS_PtRefund).SetFocus
   Forms("frmPFS_PtRefund)("btnAddNew").SetFocus
 
  Case CurrentProject.AllForms("frmPPFS_PtRefund").IsLoaded
   Forms("frmPPFS_PtRefund").SetFocus
   Forms("frmPPFS_PtRefund")("btnAddNew").SetFocus
 
  Case CurrentProject.AllForms("frmPFS_InsRefund").IsLoaded
   Forms("frmPFS_InsRefund").SetFocus
   Forms("frmPFS_InsRefund")("btnAddNew").SetFocus
 
  Case Else 
   Forms("frmPPFS_InsRefund")("btnAddNew").SetFocus
 
End Select
 
End Sub


Linq ;0)>


This worked perfectly! I added the end quotes after the first case, but sooo awesome! Thank you both!!!
 
Sorry about the typo, but glad we could help!

All the unknown variables that make Access such an interesting programme. :)
They do, indeed! I've been working in Access for a dozen years and it never becomes boring!

Linq ;0)>
 
Sorry about the typo, but glad we could help!


They do, indeed! I've been working in Access for a dozen years and it never becomes boring!

Linq ;0)>

Yes, especially when trying to convert 2007 DBs to 2010. Oh ... what fun.
 

Users who are viewing this thread

Back
Top Bottom