I was selected to be a guest blogger on the official Microsoft Access blog! You can read the article here:
Please read and come back here for some final thoughts on the technique.
Another alternative: DAO
When I wrote the guest post I automatically selected ADODB since that’s what I use almost always in my code. Here’s the same concept using DAO:
Private Sub OrderStatus_MouseMove( _
Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single _
) Â Â Â Â Â Â Â Â Â
Dim strSQL As String
         Dim rst As DAO.Recordset
Dim db As DAO.Database
10Â Â Â Â Â Â Â If IsNull(Me.OrderID) Or Me.OrderID = "" Then
20Â Â Â Â Â Â Â Â Â Â Â Exit Sub
30Â Â Â Â Â Â Â End If
32Â Â Â Â Â Â Â strSQL = "Select ItemNum from tblOrderItems Where OrderID = " & Me.OrderID
33 Set db = CurrentDB
34Â Â Â Â Â Â Set rs = db.OpenRecordset(strSQL)
60Â Â Â Â Â Â Â With rs
70Â Â Â Â Â Â Â Â Â Â Â If .RecordCount > 0 Then
80Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â strSQL = ""
90Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Do While .EOF = False
100Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â strSQL = strSQL & !ItemNum & vbCrLf
110Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â .MoveNext
120Â Â Â Â Â Â Â Â Â Â Â Â Â Â Loop
130Â Â Â Â Â Â Â Â Â Â End If
140Â Â Â Â Â Â End With
150Â Â Â Â Â Â Me.OrderStatus.ControlTipText = strSQL
160Â Â Â Â Â Â Set rs = Nothing
End Sub
Persistent Recordset
If you find yourself constantly opening and closing the recordset, you can choose to create a module level variable, move out the line 30-32 into form’s load or current event and not closing the recordset by commenting out line 160 above. Â If you do go down this route then you would need to retrieve all related records in one trip. You also may want to open the recordset as a snapshot-type recordset to ensure the request is pulled only once. If it’s a lot of records in memory it may not be worth it.
After all these years I’m continually amazed at the capabilities of Access and how we can stretch the envelope of what can be done.