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

Focus of Textfield 1

Status
Not open for further replies.

janetlyn

Technical User
Jan 29, 2003
516
US
I have a textfield [ProjectNo]. When the form opens I would like the textfield to have focus. I would think it has focus because that is where the cursor is when the form opens. But, I have code in the OnFocus, LostFocus, of the ProjectNo textfield and that code is not working, which is why I wrote the code below, to see if I could figure out why.

Here is my code. When the form is open I get the "Don't have focus" message. Do you see the obvious problem that I am missing?

Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
Forms![FRM-InsApQ16]![ProjectNo].SetFocus
If GotFocus = [ProjectNo] Then
MsgBox "I have focus"
Else
MsgBox "I don't have focus"
End If

Any help would be appreciated. Thanks, JL
 
After some experimenting, it appears that the focus is always at first the zeroth control. When the form is opened it moves to the control pointed to by the setfocus command. Seems do operate as one might expect. Make yourself a form with four textboxes and place a msgbox with each action at the getfocus and lostfocus event and you will see what happens.

rollie@bwsys.net
 
Rollie, I did what you said, and everything worked fine. Since ProjectNo is the first textbox it should have focus and the If should be true. Is that not correct?

Thanks for your help, JL
 
JL,

I could not get an "if" statement to work with focus. Not sure why. You have to sneak around to get the answer. I am sure there is a way using the control object but it does not appear worth the effort as I see it.

Rollie
 
Okay, let's scrap the If, I only used it to try and solve my original problem. So, since we can't do that, here is the original problem. At one point this worked, and I am not sure why it doesn't now, but when the form opened it goes to the textfield "ProjectNo" (see FormCurrent below) and as you go through the records in the InsAp table, if Textfield "Insurance" is not null, a label with the word "Complete" becomes visible. The visible code is in the GotFocus and Lost Focus of the field ProjectNo.

Private Sub ProjectNo___GotFocus()
MsgBox "I have focus"
Dim varTemp As Variant
varTemp = DLookup("[16Projects]", "[TBL-InsAp]", _
"[ProjectNo]=Forms!FRM-InsApQ16![ProjectNo]")
If varTemp > 0 Then
Label124.Visible = True
End If
End Sub

Private Sub ProjectNo___LostFocus()
Label124.Visible = False
End Sub

Private Sub Form_Current()
Me![Text140] = DLookup("[ClientOrg]& ', ' & [ProjectBldg]& ', ' & [ProjectDesc]& ', ' & [CityState]", "TBL-ProjectNumberTitle", "[ProjectNo]= '" & Me![ProjectNo] & "'")
[ProjectNo].SetFocus
Line28.Visible = False
Label29.Visible = False
Line53.Visible = False
Label52.Visible = False
Line63.Visible = False
Label62.Visible = False
Line76.Visible = False
Label75.Visible = False
Frame18.Visible = False
Frame54.Visible = False
Frame66.Visible = False
Frame90.Visible = False
End Sub

Why does this code not worked. The FormCurrent seems to be the code not working. As a test, I put a message box as the first line of the FormCurrent of "This worked." But the message did not come up. I verified that the OnFocus and LostFocus of the properties fields has "Event Procedure" so that's not it.

Any ideas? Thanks again Rollie, JL
 
janetlyn,

I wish you had posted that first. Let me suggest that you solve your problem with code something like this in your form's on_current event. It works great. I plan to use it now that it in the future. It requires a label control named lblComplete with the caption "COMPLETE" and

If Not IsNull(Me.Insurance) Then Me.lblComplete.Visible = True Else Me.lblComplete.Visible = False

rollie@bwsys.net
 
Rollie, thank you. You are an angel. Works perfectly. Stars to ya. Have a wonderful day. Janet Lyn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top