VBA user, first time on Vbasic. Need help referencing a control
VBA user, first time on Vbasic. Need help referencing a control
am 11.10.2007 05:26:06 von adam_kroger
BRIEF EXPLANATION:
I have 6 TextBoxes named LIS1, LIS2, LIS3, ... LIS6. I want to be
able to reference them using a For/Next loop and either read ot write
to them. In VBA I would use something like this:
for i = 1 to 6
me.controls("LIS" & i).Value = ""
next i
Nedless to say, the Controls("LIS" & i).text doesn't work...
What do I need to do differently?
ALSO, in VBA if I wanted an expression to be resolved before being
addressed by the rest of the line I could enclose it in [ ] or use
evaluate(). Is there something similar in VBasic, or is this limited
to script based languages?
I am trying to turn processes I have written in Excel using VBA and
UserForms into stand-alone programs. I am using VisualBasic 2005
Express Edition.
---
MORE DETAILED EXPLANATION ON PROJECT:
The program determines the gaps between dates and gives the results in
months. The interface has 22 Masked TextBoxes (forcing Date entries)
in groupings of 6,5,5,2,2 and 2 singles, also 31 Labels (for reporting
results) in groups of 5 + 1 for the total; using naming conventions as
specified above. I need to be able to cycle through them to either
read the data into array variables for manipulations and comparisons,
or to "reset" them to blank. I could probably get by on this project
with naming them absolutely, but in my next project, it won't be
anywhere near as straightforward.
Re: VBA user, first time on Vbasic. Need help referencing a control
am 11.10.2007 05:54:09 von notmyfirstname
Adam,
Will you first give us an impression what you mean by Vbasic, it does not
exist like that. I assume you mean a Visual Basic variant, however for that
there are even more variants then for the C language.
The answer on your question is in fact yes, however it is by instance in
Visual Basic for Net completeley different from Visual Basic 6.
The best newsgroup for the first is:
news:\\Microsoft.public.VB.general.discussions
While for the latter it is:
news:\\Microsoft.public.dotnet.languages.vb
Cor
Re: VBA user, first time on Vbasic. Need help referencing a control
am 11.10.2007 06:43:58 von s-mar
First, you must understand that VB .NET is not just "the new version of VB
6". It is entirely new and opererates very differently from the VB of days
gone by. Also, just to get your terminology right, VBA is not a scripting
language, VBScript is.
The reason your attempt doesn't work is because in .NET, it's all about
"types", so this code:
("LIS" & i)
would be processed as the String type, who's variable pointer is "LIS"
should be concatenated with the Integer type, who's variable is 1 - 6 (based
on your code).
Well, you probably don't have a string variable called "LIS" and
concatenating (which, by definition is for String types), it with a number,
would return a new String type reference called, say, "LIS1", now you do
have a Textbox type with a variable of "LIS1", but you don't have a String
with a variable of "LIS1", so your code fails. Even if you did happen to
have a String type with a variable of "LIS1", strings don't have a .value
property, so the rest of your code would cause the line to fail.
There are advanced ways of making your code work using the same concept as
what you are trying here, but why not just go down an eaiser path:
Dim myTextBox As Textbox
For Each myTextBox In Me.Controls
myTextBox.Text = ""
Next
Also, notice that I'm not trying to access the data in the Textbox via the
"value" property? That's becaue, in VBA and in .NET, textboxes have a Text
property (as do pretty much everything that displays text (labels, buttons,
etc.) - - The "caption" property is gone. The only time you'd use "value"
is in client-side code referring to HTML form fields.
Good luck & stick with it - - .NET is worth the time it takes to understand
it. Just remember that you'll need to "unlearn" much of what you have
learned!
-Scott
wrote in message
news:1192073166.994164.300130@o80g2000hse.googlegroups.com.. .
> BRIEF EXPLANATION:
> I have 6 TextBoxes named LIS1, LIS2, LIS3, ... LIS6. I want to be
> able to reference them using a For/Next loop and either read ot write
> to them. In VBA I would use something like this:
> for i = 1 to 6
> me.controls("LIS" & i).Value = ""
> next i
>
> Nedless to say, the Controls("LIS" & i).text doesn't work...
>
> What do I need to do differently?
>
> ALSO, in VBA if I wanted an expression to be resolved before being
> addressed by the rest of the line I could enclose it in [ ] or use
> evaluate(). Is there something similar in VBasic, or is this limited
> to script based languages?
>
> I am trying to turn processes I have written in Excel using VBA and
> UserForms into stand-alone programs. I am using VisualBasic 2005
> Express Edition.
>
> ---
>
> MORE DETAILED EXPLANATION ON PROJECT:
> The program determines the gaps between dates and gives the results in
> months. The interface has 22 Masked TextBoxes (forcing Date entries)
> in groupings of 6,5,5,2,2 and 2 singles, also 31 Labels (for reporting
> results) in groups of 5 + 1 for the total; using naming conventions as
> specified above. I need to be able to cycle through them to either
> read the data into array variables for manipulations and comparisons,
> or to "reset" them to blank. I could probably get by on this project
> with naming them absolutely, but in my next project, it won't be
> anywhere near as straightforward.
>
Re: VBA user, first time on Vbasic. Need help referencing a control
am 11.10.2007 07:21:30 von s-mar
Just a correction...
> ("LIS" & i)
>
> would be processed as the String literal type, who's value is "LIS"
> should be concatenated with an Integer type, who's variable is 1 - 6
> (based
> on your code).
Re: VBA user, first time on Vbasic. Need help referencing a control
am 11.10.2007 13:26:42 von adam_kroger
Scott:
Thank you for your answer. Unfortunately that method won't solve all
my difficulties. In addition to the Masked TextBoxes I also need to
be able to find 31 Labels out of 55 and alter their .Text value. The
controls that need to be altered all have the same naming convention
# where would be one of the followinng (LIS, ENRS, ENRE,
ESS, ESE, DAYS, MONTHS, or GAP). would something like this work
(though it seems inefficient):
dim ctrl as control, i as integer
for i = 1 to 6
for each ctrl in me.controls
if right(ctrl.name, 1) = i then
ctrl.txt = ""
endif
next
next
Is there not a way to locate the control without having to loop
through every control on the form, every time? Your right I am goinng
to have to unlearn severral thought processes, this seems like it
should be such a simple task, to programicly determine which control
needs to be accessed and then do so.
On Oct 10, 11:43 pm, "Scott M." wrote:
<>
>There are advanced ways of making your code work using the same concept as
>what you are trying here, but why not just go down an eaiser path:
> Dim myTextBox As Textbox
> For Each myTextBox In Me.Controls
> myTextBox.Text = ""
> Next
>
> Also, notice that I'm not trying to access the data in the Textbox via the
> "value" property? That's becaue, in VBA and in .NET, textboxes have a Text
> property (as do pretty much everything that displays text (labels, buttons,
> etc.) - - The "caption" property is gone. The only time you'd use "value"
> is in client-side code referring to HTML form fields.
>
> Good luck & stick with it - - .NET is worth the time it takes to understand
> it. Just remember that you'll need to "unlearn" much of what you have
> learned!
>
> -Scott
>
> wrote in message
>
> news:1192073166.994164.300130@o80g2000hse.googlegroups.com.. .
>
<<>>
> > MORE DETAILED EXPLANATION ON PROJECT:
> > The program determines the gaps between dates and gives the results in
> > months. The interface has 22 Masked TextBoxes (forcing Date entries)
> > in groupings of 6,5,5,2,2 and 2 singles, also 31 Labels (for reporting
> > results) in groups of 5 + 1 for the total; using naming conventions as
> > specified above. I need to be able to cycle through them to either
> > read the data into array variables for manipulations and comparisons,
> > or to "reset" them to blank. I could probably get by on this project
> > with naming them absolutely, but in my next project, it won't be
> > anywhere near as straightforward.- Hide quoted text -
>
> - Show quoted text -
Re: VBA user, first time on Vbasic. Need help referencing a control
am 11.10.2007 17:22:38 von s-mar
Hi Adam,
Your approach *can* work, but you are still using legacy VB 6 techniques,
rather than a .NET OO approach with the use of the "right()" function. In
..NET, most of the VB 6 functions have been replaced with a more OO way of
doing things.
When you refer to: ctrl.Name, you are getting back a String object and
String objects (like any other object) have properties and methods. Using
these properties and methods, you can extract any portion of a string you
need to.
Will the .NET approach require more code compared to the VB 6 approach which
you have learned and are already comfortable with? Yes, and this makes
using VB .NET counter-intuitive and cumbersome at first, but once you make
the mental "leap" to OOP (VB 6 was NOT OOP!), you begin to understand how
much more sense your code makes and how much more powerfull you can be as a
programmer.
Using this OO approach, if the lables you need to find will always be the
same group, why not put them all into a collection object early on in your
code, then you could just loop through your collection, rather than
me.controls and you wouldn't have to test to see if they are the ones you
want. This would give you comprable results to a VB 6 control array.
-Scott
wrote in message
news:1192102002.373378.186350@o80g2000hse.googlegroups.com.. .
> Scott:
> Thank you for your answer. Unfortunately that method won't solve all
> my difficulties. In addition to the Masked TextBoxes I also need to
> be able to find 31 Labels out of 55 and alter their .Text value. The
> controls that need to be altered all have the same naming convention
> # where would be one of the followinng (LIS, ENRS, ENRE,
> ESS, ESE, DAYS, MONTHS, or GAP). would something like this work
> (though it seems inefficient):
>
> dim ctrl as control, i as integer
> for i = 1 to 6
> for each ctrl in me.controls
> if right(ctrl.name, 1) = i then
> ctrl.txt = ""
> endif
> next
> next
>
>
> Is there not a way to locate the control without having to loop
> through every control on the form, every time? Your right I am goinng
> to have to unlearn severral thought processes, this seems like it
> should be such a simple task, to programicly determine which control
> needs to be accessed and then do so.
>
>
> On Oct 10, 11:43 pm, "Scott M." wrote:
>
> <>
>
>>There are advanced ways of making your code work using the same concept as
>>what you are trying here, but why not just go down an eaiser path:
>> Dim myTextBox As Textbox
>> For Each myTextBox In Me.Controls
>> myTextBox.Text = ""
>> Next
>>
>> Also, notice that I'm not trying to access the data in the Textbox via
>> the
>> "value" property? That's becaue, in VBA and in .NET, textboxes have a
>> Text
>> property (as do pretty much everything that displays text (labels,
>> buttons,
>> etc.) - - The "caption" property is gone. The only time you'd use
>> "value"
>> is in client-side code referring to HTML form fields.
>>
>> Good luck & stick with it - - .NET is worth the time it takes to
>> understand
>> it. Just remember that you'll need to "unlearn" much of what you have
>> learned!
>>
>> -Scott
>>
>> wrote in message
>>
>> news:1192073166.994164.300130@o80g2000hse.googlegroups.com.. .
>>
>
> <<>>
>
>
>> > MORE DETAILED EXPLANATION ON PROJECT:
>> > The program determines the gaps between dates and gives the results in
>> > months. The interface has 22 Masked TextBoxes (forcing Date entries)
>> > in groupings of 6,5,5,2,2 and 2 singles, also 31 Labels (for reporting
>> > results) in groups of 5 + 1 for the total; using naming conventions as
>> > specified above. I need to be able to cycle through them to either
>> > read the data into array variables for manipulations and comparisons,
>> > or to "reset" them to blank. I could probably get by on this project
>> > with naming them absolutely, but in my next project, it won't be
>> > anywhere near as straightforward.- Hide quoted text -
>>
>> - Show quoted text -
>
>