Populate a form with a recordset

K

Ken

I've got three books on programming ADO, and I don't see a clear cut and
dry way to populate a form with a recordset.

I have populated a test form. but I'm not sure how it happened. I used:
For Each Ctl In Me
vartemp = Rst.Fields(Ctl.Name).Name
If Ctl.Enabled Then
Ctl.Value = Rst.Fields(Ctl.Name).Value
End If
Next
from one of the books but I'm not sure what "Rst.Fields(Ctl.Name).Name"
does. I know that the Rst is the record set I opened, but when I put the
period after fields, it isn't in the list.
Any pointers are greatly appreciated.
 
D

Douglas J. Steele

It would appear to be assuming that the controls on the form will be named
the same as the fields in the recordset, which despite the fact that it's
the default behaviour when you create a form in Access, I don't consider a
good practice.

That warning aside, what your code is doing is looping through every control
on the form. Each control has a Name property. It's checking to see whether
or not there's a field in the underlying recordset with that same name (I'm
assuming you've got On Error Resume Next in the code, otherwise an error
would be raised on the vartemp = .... line for those controls which don't
exist in the recordset. Either that, or you don't have any controls on the
form other than the text boxes you want the values put in: no labels, no
buttons to close the form, etc.)

Why can't you just create a bound form?
 
Top