p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 (http://p2p.wrox.com/forumdisplay.php?f=329)
-   -   Chapter 19: Creating the Recordset (http://p2p.wrox.com/showthread.php?t=74263)

NuovoData May 5th, 2009 08:16 PM

Chapter 19: Creating the Recordset
This code comes from Chapt 19, p 686 in the Creating the Recordset section and the sub PopulateFields():


If Not g_rsFormSource.BOF And Not g_rsFormSource.EOF Then
                      Me!txtCompanyName.Text = g_rsFormSource.Fields("CompanyName")
                      Me!txtContactName.Text = g_rsFormSource.Fields("ContactName")
                      Me!txtContactTitle.Text = g_rsFormSource.Fields("ContactTitle")
                      Me!txtCustomerID.Text = g_rsFormSource.Fields("CustomerID")
                      Me!txtFax.Text = g_rsFormSource.Fields("Fax")
                      Me!txtPhone.Text = g_rsFormSource.Fields("Phone")
                      ' Throw an error
    End If

That seemed pretty bulky to me and not very flexible, so I played around for a while and came up this this:


Dim ctlC As Control
Dim strC As String
    If Not g_rsFormSource.BOF And Not g_rsFormSource.EOF Then
            ' For each control.
    For Each ctlC In Me.Controls
                    If ctlC.ControlType = acTextBox Then
                                ' Get the name of the current control
                                strC = ctlC.Name
                                ctlC.Value = Nz(g_rsFormSource.Fields(strC), "")
                    End If
            Next ctlC
    End If

It saves you the trouble of setting the focus on each control and allows you to populate controls that are not enabled. It also means that you don't have to modify the VBA every time you modify the form.

It seems to work pretty well, but if anybody has any reasons why I shouldn't take this approach, I'd like to hear it. I will add in error processing later.


All times are GMT -4. The time now is 05:32 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.