473,386 Members | 1,969 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,386 software developers and data experts.

How to replace an object name with a variable?

2
I am very new to object oriented programming, I used to program in a variety of languages. I stopped about 15 years ago as my needs diminished. I last programmed in clipper5. I am trying to learn VB and am using VB6. I used to replace names of variables with other variables that I could change in a loop

I have a list of check boxes named “check1” through “check5”, I want to replace the object name with a variable that I can change in a loop

var = “check”

i = 1
do while i < 6
var = var & str$(i)
var.Caption = Trim(adoCustRecordset("Name"))
adoCustRecordset.MoveNext
i = i + 1
loop


I have tried to Dim a variable as an object and then Set the object but to no avail, I could list all the check boxes but the above method was a standard for me in clipper5 and other languages. I am wondering if a method exists in VB6?
May 28 '07 #1
7 7478
Killer42
8,435 Expert 8TB
In VB6 you have two options. You can build a name in a string and use it as an index in the Control collection. Or, by far the better option, just use a control array. What you're doing with the names is sort of simulating an array, but in VB6 you can create a real one.

For a demonstration, just create a form, place a control on it (doesn't really matter what sort) then copy the control and paste it back into the form. You'll be asked whether you want to create a control array - say "yes". These work just like "normal" controls, except that they have an index, just like an array of variables.
May 28 '07 #2
pmcarl
2
a control array , of course!

Thanks Killer42, I am dough head.
May 28 '07 #3
Killer42
8,435 Expert 8TB
Glad we could help. :)
May 28 '07 #4
btoyer
2
Could you please explain the first method as I already have 81 text boxes on a form with code references to them and would not like to start over with a control array?
Jun 5 '07 #5
Killer42
8,435 Expert 8TB
Could you please explain the first method as I already have 81 text boxes on a form with code references to them and would not like to start over with a control array?
The form's Controls collection is indexed by the control name, I believe. So for example, the control Text1 could also be referenced as Me.Controls("Text1").

Since the index is a string, it follows that you can build the string any way you like, including by sticking a number together with a standard name. In this way, you can produce similar functionality to a control array.

For example...

Expand|Select|Wrap|Line Numbers
  1. Dim I As Long, Ctl As Control
  2. For I = 1 To 5
  3.   Set Ctl = Me.Controls("Text" & format(I))
  4.   Debug.? Ctl.Name, Ctl.Text
  5. Next
Note, I haven't tested this - just typed it straight in here. But it should be fairly close.
Jun 6 '07 #6
btoyer
2
I didn't need the format(), but the Me.Controls worked wonders. Thanks.
Jun 6 '07 #7
Killer42
8,435 Expert 8TB
I didn't need the format(), but the Me.Controls worked wonders. Thanks.
Glad to help. :)

I'm a bit surprised about the Format() though. In my experience, when convertoing numbers to strings VB usually leaves a space. But I can never remember whether it's on the left or right, so maybe a space on the right didn't bother it.
Jun 7 '07 #8

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

Similar topics

11
by: Larry Woods | last post by:
I have a "homegrown" dll that I am using in IIS (VBScript). I need to replace it on a production server, but I don't want to stop/restart server in order to replace the dll. Possible? TIA,...
3
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the following: Some text<br/> here with</br> lots...
5
by: K.Simon | last post by:
Hello, it's very often neccessary to replace strings or a single character in my stylesheets. My solution looks awful and very long. Now i thought to solve this with an array like structure but...
1
by: Luke Dalessandro | last post by:
I have an application where there is a primary XML data file. I'll use the following as an example: <data> <item id="a"> <name>A</name> <price>$10</price> </item> <item id="b">...
8
by: Peter van Schie | last post by:
Hi all, Give an xml document that looks something like this: <?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xsl"...
2
by: scorpion53061 | last post by:
This excel find/replace code works great under Excel 2003 but bombs with an error (pasted below the code) in Excel 2000. Can anyone suggest an alternative to make both happy? I am using vb.net...
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
2
by: X l e c t r i c | last post by:
Here: http://bigbangfodder.fileave.com/res/sandr.html I'm trying to use string.replace() for a basic search and replace form using textarea values as the regexp and replacement values for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.