Wrox Programmer Forums
|
BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3
This is the forum to discuss the Wrox book Access 2007 VBA Programmer's Reference by Teresa Hennig, Rob Cooper, Geoffrey L. Griffith, Armen Stein; ISBN: 9780470047033
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2009, 08:16 PM
Registered User
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Post Chapter 19: Creating the Recordset

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

Code:
If Not g_rsFormSource.BOF And Not g_rsFormSource.EOF Then
              Me.txtCompanyName.SetFocus
                      Me!txtCompanyName.Text = g_rsFormSource.Fields("CompanyName")
                       Me.txtContactName.SetFocus
                       Me!txtContactName.Text = g_rsFormSource.Fields("ContactName")
                       Me.txtContactTitle.SetFocus
                       Me!txtContactTitle.Text = g_rsFormSource.Fields("ContactTitle")
                       Me.txtCustomerID.SetFocus
                       Me!txtCustomerID.Text = g_rsFormSource.Fields("CustomerID")
                       Me.txtFax.SetFocus
                       Me!txtFax.Text = g_rsFormSource.Fields("Fax")
                       Me.txtPhone.SetFocus
                       Me!txtPhone.Text = g_rsFormSource.Fields("Phone")
    Else
                       ' 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:

Code:
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.

Thanks,
Victor

Last edited by NuovoData; May 5th, 2009 at 09:19 PM.. Reason: Changed some of the var names in my code to match that of the book.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 19 johanyu BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 3 November 12th, 2007 10:50 AM
Chapter 19 XML kistaw BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 0 November 12th, 2007 03:56 AM
Scripts for chapter 19 ValF BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 3 March 10th, 2006 12:03 AM
Chapter 19 Exercise CharlieChan BOOK: Beginning Visual C++ 6 0 June 19th, 2005 05:15 PM
Chapter 19 johanyu BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 October 10th, 2004 11:30 PM





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