[My suggestion followed by an answer to your question, choose whichever suits your need]
Would you be better making the form bound but editing the RecordSource
for the form to be your strSQL
? Adds more scope/ ease to editing data, which I assume you intend to do by the use of DB_OPEN_DYNASET
, though your current approach would not keep the locks on the data beyond the rs.Close
.
This method would need an alternate way of gathering the ID
, unless txt_ID
is not bound, I would use an InputBox
via a CommandButton
:
Sub cmdRetrieveRecord()
Dim strSQL as String, strID as string
EnterID:
strID = InputBox("Enter the required ID:")
If StrPtr(strID) = 0 Then 'Cancel was pressed
ExitSub
ElseIf Len(strID) = 0 Then 'OK was pressed with nothing entered
MsgBox "You must enter an ID."
GoTo EnterID
End If
If Not IsNumeric(strID) Then
MsgBox "You must enter only a numeric ID"
GoTo EnterID
End If
strSQL = "SELECT Table1.ID, Table1.Color, Table1.Make, Table1.Model, Table2.FName, " _
& "Table2.LName FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE"
strSQL = strSQL & "[Table1].ID = """ & strID & """"
Me.RecordSource = strSQL
End Sub
NOTE if there are no records for the entered ID then the form will be blank, make sure you put the cmdRetrieveRecord
button in the form header or footer so it is still visible, alternatively handle such a situation in your code.
You can then control whether the data is Snapshot
/ Dynaset
, editable etc from the form properties.
If you wish to proceed with your intended approach I think you should be able to do it something like the below by iterating through the Fields
property of the recordset, though you have to be very careful in your naming of fields and controls and I imagine sooner or later it will trip you up.
Dim x As Integer
Set rs = Currentdb.OpenRecordset(strSQL, DB_OPEN_DYNASET)
If rs.EOF And rs.BOF Then
' No record for this ID, handle accordingly and ExitSub
End If
rs.MoveFirst
For x = 0 To rs.Fields.Count - 1
Me.Controls(rs(x).Name) = rs(x)
Next x
NOTE This is untested, I've just written it from memory but it should get you on the right track.