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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change ocus prior to On Current Event

Status
Not open for further replies.

khwaja

Technical User
Aug 27, 2001
431
0
0
AU
I have a form and have one of the field set to invisible if certain conditions are met and put the code on current event. Something similar to the one shown below:

Private Sub Form_Current()
If [Schedule].Form![Dev_Code] Like "*N*" Then
Me![Schedule].Form![DateOpen1].Visible = True
Else
Me![Schedule].Form![DateOpen1].Visible = False
End If
End Sub


It works well but if the field has the focus, then I get the error message that field cannot be invisible when it has the focus. What is the best way to manage this. Ideally, there should be an event that is triggered before on current event kicks in but I could not find it. I tried dactivate and unload to shift the focus but to no avail. Could someone help?



Cheers

AK

Note: Using Access 97 for back end A2002 for front end.
 
How are ya khwaja . . . . .

Just move the focus to another field at the start of the routine:

[blue]Me!TextBoxName.SetFocus[/blue]

Calvin.gif
See Ya! . . . . . .
 
Or us the following code instead:
Code:
[blue]Private Sub Form_Current()
   Dim sfrm As Form, prp As Property
   
   Set sfrm = Me!Schedule.Form
   Set prp = sfrm!DateOpen1.Properties("Visible")
   
   If frm!Dev_Code Like "*N*" Then
      prp = True
   Else
      If Screen.ActiveControl.Name = "DateOpen1" Then
         sfrm![purple][b]SomeOtherControlName[/b][/purple].SetFocus
      End If
      
      prp = False
   End If
   
   Set prp = Nothing
   Set frm = Nothing

End Sub[/blue]

Calvin.gif
See Ya! . . . . . .
 
One way or another you're going to have to setFocus to another control before you can hide the DateOpen1 control.

Either determine which control to setFocus to or use SendKeys "{TAB}" to go to the next control.

If the DateOpen1 control has the focus, I think you can use the ActiveControl property of the Form.
Code:
If Me.ActiveControl.Name = "DateOpen1" Then
    [I][red]nextControl[/red][/I].SetFocus
EndIf

Alternatively, trap the error and handle it.

Code:
On Error GoTo ErrorHandler
.
.
.
ErrorHandler:
    [I][red]nextControl[/red][/I].SetFocus
    Resume Next
 
Thank you all for your help. I tried the above two solutions. The last two produce run time error 2474 (The expression you enrered requires control to be in the active window).

I am sure you have taken note that the control I am hiding is on the sub form. I used Aceman1 code as under after adding an 's' to frm in a couple of lines as I was getting an error:

Private Sub Form_Current()
Dim sfrm As Form, prp As Property

Set sfrm = Me!Schedule.Form
Set prp = sfrm!DateOpen1.Properties("Visible")

If sfrm!Dev_Code Like "*N*" Then
prp = True
Else
If Screen.ActiveControl.Name = "DateOpen1" Then
sfrm![PM].SetFocus
End If

prp = False
End If

Set prp = Nothing
Set sfrm = Nothing

End Sub

In cse of Edski, I placed the code just before the other code I currently have.

Private Sub Form_Current()
If Me.ActiveControl.Name = "DateOpen1" Then
Me![Schedule].Form![PM].SetFocus
End If
If [Schedule].Form![Dev_Code] Like "*N*" Then
Me![Schedule].Form![DateOpen1].Visible = True
Else
Me![Schedule].Form![DateOpen1].Visible = False
End If
End Sub

PM is the field on the sub form that can have the focus before hiding the DateOpen1 field.

Will appreciate some more help.

Cheers

AK

Note: Using Access 97 for back end A2002 for front end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top