Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA 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 November 12th, 2006, 09:59 AM
Authorized User
 
Join Date: Oct 2006
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default Referencing Unbound fields with variables

I have 10 fields on a subform, named TP1, TP2, .... thru TP10.

I want to sum the value of each of the 10 fields.

I would like to use a loop to do this, like the following:

Dim p As Integer
Dim PalletCount As Single

For p = 1 To 10
    PalletCount = PalletCount + Eval("TP" & p & ".Value")
    Debug.Print PalletCount
Next p
I get the message "Microsoft Access can't find the name 'TP1' you entered in the expression.

I would like to avoid the following more explicit method:

PalletCount = TP1 + TP2 + TP3 + TP4 + TP5 .... + TP10



 
Old November 12th, 2006, 11:11 AM
Registered User
 
Join Date: Nov 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the code is not actually referencing the textbox on the form.

try the following code which will set a reference to the textbox based on the name each time you go around the loop. then you can get the value:

Dim p As Integer
Dim PalletCount As Single
Dim t As TextBox

For p = 1 To 10
    Set t = Me.Controls("TP" & p)
    PalletCount = PalletCount + Eval(t.Value)
    Debug.Print PalletCount
Next p



 
Old November 12th, 2006, 11:55 AM
Authorized User
 
Join Date: Oct 2006
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow, thanks. Amazing how such a simple little thing can make the difference, I had been struggling with this for a while.

Now I have to go back and see what other routines should be utilizing this.

Thanks a ton!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Number of variables referencing an Object Muyavil Java Basics 3 April 11th, 2009 11:02 PM
Referencing Fields using VBScript in Outlook 2000 stelhead All Other Wrox Books 0 January 3rd, 2008 12:04 PM
Referencing Fields within Tab Control Pages -Help TomW Access VBA 1 October 30th, 2006 01:31 PM
Unbound Fields on a continuous form phil.t Access 0 October 5th, 2004 07:44 PM
can anybody help me in using unbound fields in CR prasan BOOK: Professional Crystal Reports for VS.NET 0 July 20th, 2004 12:41 AM





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