I've got a continuous form that has three unbound textboxes, named "linked_swimmer1", "linked_swimmer2" and "linked_swimmer3" on. The form is a list of contact names and I am looking to display the people these contacts are linked to and but only show the unbound textboxes where there are matching records. I've cobbled together the below code from various things I've found on the intertubes, but what I am missing is how to reference those unbound textboxes on the form, based on the current record in the recordsetclone.
The various debug.print's shotgunned throughout that show that both loops are incrementing correctly and are returning the correct data for each iteration, so the only thing I am missing is how to actually get the data onto the form. Any help or pointers appreciated!
Code:
Private Sub Form_Load()
Dim db As Database
Dim rs As DAO.Recordset
Dim rsc As DAO.Recordset
Dim i As Long
Dim ctrl As Control
Dim strSQL As String
Dim IDX As Integer
' Empty and hide all of the linked_swimmer* text boxes
For Each ctrl In Me.Controls
If Left(ctrl.Name, 14) = "linked_swimmer" Then
ctrl.Visible = False
ctrl.Value = ""
End If
Next ctrl
' Because we are a continuous form we need to loop twice, once through the forms recordset as a clone (rsc) and then for each entry in the record itself (rs)
Set rsc = Me.RecordsetClone
rsc.MoveLast
rsc.MoveFirst
For IDX = 1 To rsc.RecordCount
' Debug.Print IDX
' Debug.Print rsc.Fields("contact_id")
'inner loop
Set db = CurrentDb
' Get the swimmer names linked to that contact ID
strSQL = "select swimmers.name from swimmers where contact_id = " & rsc.Fields("Contact_id") & ""
' Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
' Check we aren't in an empty record or at the end of the recordset
If rs.EOF And rs.BOF Then
Else
i = 1
Do While Not rs.EOF
' Debug.Print rs.Fields("name")
'Set the value of each textbox to the swimmers name in the recordset
' Me.Controls("linked_swimmer" & i).Value = rs.Fields("Name")
'unhide textbox
' Me.Controls("linked_swimmer" & i).Visible = True
'iterate the counter that steps up to the next textbox
i = i + 1
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
Set db = Nothing
' end of inner loop
' goto next record in clone
rsc.MoveNext
Next IDX
End Sub
The various debug.print's shotgunned throughout that show that both loops are incrementing correctly and are returning the correct data for each iteration, so the only thing I am missing is how to actually get the data onto the form. Any help or pointers appreciated!