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

avoiding GotFocus to call itself when finding a record. 1

Status
Not open for further replies.

vanleurth

Programmer
Sep 1, 2001
156
US
Hi happy programmers,

I'm trying to use gotfocus event of a text object to go through a record set find a record without calling itself in the middle of the gotfocus event procedure.

Private Sub Text78_GotFocus()

' Check if the control receives the focus for first time
If flgfocusfirst = True Then

' Set the recordset to the variable
Set rstsub = Me.Recordset

' Find the recordset in the datasheet subform and make it current.
If IsNull(Forms![frm training schedule]![frm training schedule calendar sub]!SchedID) Then
' Add a new record
MsgBox ("There are no more records")
Exit Sub
End If

' Find in the subform the current record
rstsub.FindFirst ("SchedID = " & Forms![frm training schedule]![frm training schedule calendar sub]!SchedID)

' If this record doesn't exist tell the user
'If rstsub.NoMatch Then
' MsgBox "This record doesn't exist"
'End If
End If

' Mark the control has been focused on already
flgfocusfirst = False

End Sub

The instruction rstsub.FindFirst calls the Gotfocus subroutine instead of continuing with the next line.
I've tried to disable the event but can't do while it has the focus.

Thanks for any help,
Vanleurth
 
What task do you intend to perform here?

Perhaps there's a better event to use than 'GotFocus'
 
I'm trying to connect two subforms that are in different tabs on a tab controls. The first subform has a datasheet form and the second one has a single form. The problem is that you can't use parent/ child feature to link this two. Therefore, I'm trying the second form to whenever the first control get focused it will look for the record that is highlighted on the first (datasheet) form.

Thanks in advance for any help,

Vanleurth
 
Try using the OnCurrent event (a Form event rather than a control event) to sync the forms.

As a starting point, just move the code you currently have in GotFocus to OnCurrent, and we'll go from there.

HTH
 
To stop recursion in ANY Event, try using a Static Boolean variable.

Public Sub ....
Static blnBusy as boolean
if blnBusy then exit sub
blnBusy = true
....... NO Exit Sub without blnBusy = false first
if .....
......
blnBusy = false
exit sub
end if
.........
blnBusy = false
End Sub Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
BeeTee,

Thanks so much for your advise. I have tried this before but my code went into a never ending loop and then crashed.
Let me explain,

When On Current Event in a subform happens, it will launch the find first function to happen in the second form (or the form that I want to syncronize it with). But, on the other side, the second form has the same code in the On CurrentEvent fucntion. Therefore, when the primary form performs a findfirst on the second form, the second form performs a search on the primary form going into a loop leading to crashing the program.

This is why I started considering gotfocus event as an option. Something else, I'm considering is to use LostFocus event but I'm working on how, as we speak ... I mean as we write.
;)

Thanks again for your time and useful advices, please feel free to further comment on this matter.
Vanleurth
 
From the way you are setting this up, it seems like no matter what you do, you are going to get into a loop.

The reason is, since A is always forcing a seek on B, which in turn does a seek on A...

I'd suggest you keep the event in FormCurrent, with this change: if, on form current, the record ID is the same record ID as in the last form current, do nothing. You will have to use some sort of primary key or perhaps a bookmark property to check for 'sameness'. Now, this could get interesting if form a causes form b to seek, which causes form a to seek to a *different* record, which causes form b to seek to a different record...
In which case, you really have to examine what you are doing in the first place.

of course John's suggestion was a good way to stop recursion as well; but, and I think John would agree, it's better to get at the cause of a problem to solve it; to design the algorithm so it works correctly in the first place.
 
Ok Everybody !

After three cup of coffee, betting my boss that I'll finish this today and if I win I'll go to Puerto Rico during spring break ... here it is !!

The following code is considered Vanleurth Technology and can be use freely. Just send people to my website which I'm trying to build
Private Sub Form_Current()

'Dimension variables.
Dim formname As String, SyncCriteria As String
Dim frm As Form, rs As DAO.Recordset

' If call made by form 1 then exit
'If childcall Then

'If the ProductName is blank, then exit the Sub.
If IsNull(Me![SchedID]) Then
Exit Sub
End If

'Define the form object and Recordset object for the Products form.
Set frm = Forms![frm training schedule].[frm training schedule calendar sub].Form
Set rs = frm.RecordsetClone

'Synchronize the corresponding record in the Products form to the current record in the subform.
rs.FindFirst ("SchedID = " & Me!SchedID)

'If a record exists in the Products form, find the matching record.
If rs.NoMatch Then
MsgBox "No match exists!", 64, formname
Else
frm.Bookmark = rs.Bookmark
End If
'End If

End Sub

Make a copy of this code and paste it in your child OnCurrent Event. It works like a charm.
So, I guess I'm going to Puerto Rico, Yeah!!

Thanks for all your help, BeeTee and John.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top