473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Reference a control through a string?

Hi all,

I have a sub-form that has 196 controls on it. The name for each control is a nubmer from 1 to 196.

I'd like to generate a loop that assigns a value to each control.

Is there a way to reference a control through a variable?

This is what I have now, without trying to attempt to reference through string:
(arrLayoutArray is a 28 row by 7 columns array that was generated in a seperate module)

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Me!sfmSubform.Form![1].Value = arrLayoutArray(1, 1)
  3.         Me!sfmSubform.Form![2].Value = arrLayoutArray(1, 7)
  4.         Me!sfmSubform.Form![3].Value = arrLayoutArray(1, 2)
  5.         Me!sfmSubform.Form![4].Value = arrLayoutArray(1, 3)
  6.         Me!sfmSubform.Form![5].Value = arrLayoutArray(1, 4)
  7.         Me!sfmSubform.Form![6].Value = arrLayoutArray(1, 5)
  8.         Me!sfmSubform.Form![7].Value = arrLayoutArray(1, 6)
  9.  
  10.  
I could do this 196 times, but I will be using this for a lot of variations and that isn't very practical. I tried doing this below:

Expand|Select|Wrap|Line Numbers
  1.     Dim counter As Variant
  2.     Dim counter2 As Integer
  3.  
  4.     counter2 = 0
  5.  
  6.     For counter = 1 To 28
  7.  
  8.         Me!sfmSubform.Form![counter2].Value = arrLayoutArray(1, 1)
  9.         counter2 = counter2 + 1
  10.         Me!sfmSubform.Form![counter2].Value = arrLayoutArray(1, 7)
  11.         counter2 = counter2 + 1
  12.         Me!sfmSubform.Form![counter2].Value = arrLayoutArray(1, 2)
  13.         counter2 = counter2 + 1
  14.         Me!sfmSubform.Form![counter2].Value = arrLayoutArray(1, 3)
  15.         counter2 = counter2 + 1
  16.         Me!sfmSubform.Form![counter2].Value = arrLayoutArray(1, 4)
  17.         counter2 = counter2 + 1
  18.         Me!sfmSubform.Form![counter2].Value = arrLayoutArray(1, 5)
  19.         counter2 = counter2 + 1
  20.         Me!sfmSubform.Form![counter2].Value = arrLayoutArray(1, 6)
  21.         counter2 = counter2 + 1
  22.  
  23.     Next
  24.  
  25.  
But I get an error because it's looking for counter2 for the name of the control, where I want it to look at the value. I also tried declaring counter2 as a string and variant and that didn't work either.

Please help!
Jul 25 '07 #1
4 1263
Killer42
8,435 Expert 8TB
Considering that you refer to a "subform", this is in MS Access, right?
Jul 26 '07 #2
Killer42
8,435 Expert 8TB
Considering that you refer to a "subform", this is in MS Access, right?
I believe you can use the Controls collection. In your example, that could make your code something like this...

Expand|Select|Wrap|Line Numbers
  1. Dim c As Long
  2.  
  3. For c = 0 To 27 Step 7
  4.   Me!sfmSubform.Form.Controls(Format$(c)).Value = arrLayoutArray(1, 1)
  5.   Me!sfmSubform.Form.Controls(Format$(c + 1)).Value = arrLayoutArray(1, 7)
  6.   Me!sfmSubform.Form.Controls(Format$(c + 2)).Value = arrLayoutArray(1, 2)
  7.   Me!sfmSubform.Form.Controls(Format$(c + 3)).Value = arrLayoutArray(1, 3)
  8.   Me!sfmSubform.Form.Controls(Format$(c + 4)).Value = arrLayoutArray(1, 4)
  9.   Me!sfmSubform.Form.Controls(Format$(c + 5)).Value = arrLayoutArray(1, 5)
  10.   Me!sfmSubform.Form.Controls(Format$(c + 6)).Value = arrLayoutArray(1, 6)
  11. Next
  12.  
My syntax may not be 100% right, but it should demonstrate the idea well enough. The form's Controls collection allows you to access all the controls by supplying the name as a string. However, there is something you need to be aware of. The controls in the collection can be accessed not only by name, but also by number (a sequential index number assigned automatically, presumably in the order they were created.)

So these two expressions would not return the same control, except by coincidence...
Controls(12)
Controls("12")
This is an unfortunate side-effect of using numbers as the names of your controls. And that's why I have used the old Format$ function, in an attempt to force VB to see the numbers as strings.

Hope you followed all that. I do tend to ramble on a bit.

P.S. I just changed counter to c to overcome some formatting problems in the message. TheScripts has a bit of a problem at the moment with certain long strings, and inserts weird spaces. This change avoided it.)
Jul 26 '07 #3
Alright, i'll give that a try.

Thanks for your help

It worked! Thanks again

I will visit this forum more often now...
Jul 26 '07 #4
Killer42
8,435 Expert 8TB
Alright, i'll give that a try.

Thanks for your help

It worked! Thanks again

I will visit this forum more often now...
Glad to hear it. :)

Before you know it, you'll be answering other people's questions. That's how we all got hooked.
Jul 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bob Cannistraci | last post by:
A three-tier user authentication system was running without a problem for almost a year and now is suddenly dysfunctional. We don't know of any changes to any of the servers. It's quite maddening....
6
by: michaelkatsilis | last post by:
Hi, Are there any issues with returning a const reference value for public "get" accessor methods? eg. const string & getVal(); // or const MyString & getVal();
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
2
by: Zippy | last post by:
Some months ago, we requested help from this newsgroup on how to replace the library reference of a database with another library reference, prior to creating an MDE. I got the following answer...
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
3
by: Dave | last post by:
Greetings, I have a user control in my web form. I cannot reference it in code. My HTML File has the header: <%@ Register TagPrefix="PLB" Tagname="PIPSidebar" src="ProjectListSidebar.ascx"...
3
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server...
3
by: SAL | last post by:
I am getting the following ERROR in my WebApp on line 30: Server Error in '/TestWebApp' Application. -------------------------------------------------------------------------------- Object...
1
by: Nathan Sokalski | last post by:
I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this UserControl looks like the following: Partial Public...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.