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 December 19th, 2006, 02:23 PM
Authorized User
 
Join Date: Dec 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to FalseParadigm
Default Using Controls within a For...Next loop

I have ran into an interesting situation. Using a Form, the user will enter up to 20 records at a time (like an order form where there will be 20 part numbers, 20 descriptions, 20 unit proces, etc). I named them all with the same convention: txtQty0, txtQty1, txtQty2, and so on.

Because there is some math involved, I have arrays for the data to avoid writing the same formula (which is quite large and complex) over and over. However, I found that I need to reference the values of the controls themselves in my formula.

Is there a way to handle controls in the same way as other variables (such as arrays) so that I can use them in my For...Next loops? I would love to do something like this:

Code:
Dim x As Integer
Dim aQty(19) As Integer

For x = 0 to 19
   aQty(x) = txtQty(x).Value
Next x
But I know that this isn't possible. Does anyone know if there is a another way to pull this off?

I tried to explain what I'm trying to do as best as I could, so if anyone needs any clarification, please let me know.

--------------------------------
Ben

"It's my way, or the Hemingway."
--------------------------------
__________________
<hr noshade size=\"1\">\"We do things my way, or the Hemingway...\"
 
Old December 20th, 2006, 05:42 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

Yeah it is possible...

You need to build the control name in a string and then identify the control using the string, so something like...

strTempControlName = txtQty & x
aQty(x) = me.Controls(strTempControlName).Value

to replace your line

aQty(x) = txtQty(x).Value

Should do the trick, I think.
 
Old December 20th, 2006, 09:55 AM
Authorized User
 
Join Date: Dec 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to FalseParadigm
Default

Thank you, this works perfectly. I was unaware that Me.Controls could be used in such a way. This will certainly help me out in other areas of my app. Thank you again. Its like an early Christmas present!

--------------------------------
Ben

"It's my way, or the Hemingway."
--------------------------------
 
Old December 20th, 2006, 10:01 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

This is especially useful in reporting, you can build control arrays and really do some on-the-fly formatting... Merry Xmas!





Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
loop through controls and master page fraperz ASP.NET 2.0 Professional 2 June 19th, 2007 01:25 PM
For...Next Loop msmagied Classic ASP Components 1 August 2nd, 2004 06:01 PM
Loop thru controls Mitch Access 2 December 23rd, 2003 02:14 PM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





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