PC Review


Reply
Thread Tools Rate Thread

How do I call a dynamically created String in a VBA Sub

 
 
izimmer@gmail.com
Guest
Posts: n/a
 
      25th Dec 2006
Hi,

I have 50 Check Boxes and I'm trying to create a Sub that accepts an
Integer parameter (1-50) and then changes the value of the check box to
true.

This is the code that I'm trying to do

Sub chngVal(x)
Dim thestring As String
thestring = "CheckBox" & x & ".Value = True"
thestring
End Sub

So if I called chngVal(25), it would call CheckBox25.Value = True.

This code doesn't work but is there a way to create a String or some
form of Object and then call the line?

Thanks!

Ilan

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFydGluIEZpc2hsb2Nr?=
Guest
Posts: n/a
 
      25th Dec 2006
Try using me.controls("CheckBox" & x ).Value = True

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"(E-Mail Removed)" wrote:

> Hi,
>
> I have 50 Check Boxes and I'm trying to create a Sub that accepts an
> Integer parameter (1-50) and then changes the value of the check box to
> true.
>
> This is the code that I'm trying to do
>
> Sub chngVal(x)
> Dim thestring As String
> thestring = "CheckBox" & x & ".Value = True"
> thestring
> End Sub
>
> So if I called chngVal(25), it would call CheckBox25.Value = True.
>
> This code doesn't work but is there a way to create a String or some
> form of Object and then call the line?
>
> Thanks!
>
> Ilan
>
>

 
Reply With Quote
 
izimmer@gmail.com
Guest
Posts: n/a
 
      25th Dec 2006
Hi Martin,

Thanks for your help.

Unfortunately the code didn't seem to work.

I'm getting an error with the Controls part saying "Compile Error -
Method or Data member not Found"

Is there any other way in which to call the string? This would now also
be helpful as for some stages I need to make all 50 Check Boxes not
visible and so would like to use a while or for statement instead of
hand coding all 50 statements of CheckBoxxx.Visible = False.

Thanks for your help again.

Ilan

Martin Fishlock wrote:
> Try using me.controls("CheckBox" & x ).Value = True
>
> --
> Hope this helps
> Martin Fishlock, Bangkok, Thailand
> Please do not forget to rate this reply.
>
>
> "(E-Mail Removed)" wrote:
>
> > Hi,
> >
> > I have 50 Check Boxes and I'm trying to create a Sub that accepts an
> > Integer parameter (1-50) and then changes the value of the check box to
> > true.
> >
> > This is the code that I'm trying to do
> >
> > Sub chngVal(x)
> > Dim thestring As String
> > thestring = "CheckBox" & x & ".Value = True"
> > thestring
> > End Sub
> >
> > So if I called chngVal(25), it would call CheckBox25.Value = True.
> >
> > This code doesn't work but is there a way to create a String or some
> > form of Object and then call the line?
> >
> > Thanks!
> >
> > Ilan
> >
> >


 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      25th Dec 2006
Good chance that with 50 checkboxes, these are on a worksheet rather than a
userform. In that case, you would use

Sub chngVal(x as Long)
Worksheets("Sheet1").OleObjects("CheckBox" & x).Object.Value = True
end sub

Assumes checkboxes from the control toolbox toolbar located on a worksheet
named Sheet1 with names like CheckBox1 to CheckBox50

--
Regards,
Tom Ogilvy


"Martin Fishlock" <(E-Mail Removed)> wrote in message
news:FB12D578-B0EA-499C-9E76-(E-Mail Removed)...
> Try using me.controls("CheckBox" & x ).Value = True
>
> --
> Hope this helps
> Martin Fishlock, Bangkok, Thailand
> Please do not forget to rate this reply.
>
>
> "(E-Mail Removed)" wrote:
>
>> Hi,
>>
>> I have 50 Check Boxes and I'm trying to create a Sub that accepts an
>> Integer parameter (1-50) and then changes the value of the check box to
>> true.
>>
>> This is the code that I'm trying to do
>>
>> Sub chngVal(x)
>> Dim thestring As String
>> thestring = "CheckBox" & x & ".Value = True"
>> thestring
>> End Sub
>>
>> So if I called chngVal(25), it would call CheckBox25.Value = True.
>>
>> This code doesn't work but is there a way to create a String or some
>> form of Object and then call the line?
>>
>> Thanks!
>>
>> Ilan
>>
>>



 
Reply With Quote
 
izimmer@gmail.com
Guest
Posts: n/a
 
      25th Dec 2006
Thanks Tom.

You're a superstar. It worked.

Tom Ogilvy wrote:
> Good chance that with 50 checkboxes, these are on a worksheet rather than a
> userform. In that case, you would use
>
> Sub chngVal(x as Long)
> Worksheets("Sheet1").OleObjects("CheckBox" & x).Object.Value = True
> end sub
>
> Assumes checkboxes from the control toolbox toolbar located on a worksheet
> named Sheet1 with names like CheckBox1 to CheckBox50
>
> --
> Regards,
> Tom Ogilvy
>
>
> "Martin Fishlock" <(E-Mail Removed)> wrote in message
> news:FB12D578-B0EA-499C-9E76-(E-Mail Removed)...
> > Try using me.controls("CheckBox" & x ).Value = True
> >
> > --
> > Hope this helps
> > Martin Fishlock, Bangkok, Thailand
> > Please do not forget to rate this reply.
> >
> >
> > "(E-Mail Removed)" wrote:
> >
> >> Hi,
> >>
> >> I have 50 Check Boxes and I'm trying to create a Sub that accepts an
> >> Integer parameter (1-50) and then changes the value of the check box to
> >> true.
> >>
> >> This is the code that I'm trying to do
> >>
> >> Sub chngVal(x)
> >> Dim thestring As String
> >> thestring = "CheckBox" & x & ".Value = True"
> >> thestring
> >> End Sub
> >>
> >> So if I called chngVal(25), it would call CheckBox25.Value = True.
> >>
> >> This code doesn't work but is there a way to create a String or some
> >> form of Object and then call the line?
> >>
> >> Thanks!
> >>
> >> Ilan
> >>
> >>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing a control property using a dynamically created string Rick Schneider Microsoft Access 2 12th Oct 2009 09:20 PM
Call a function from a dynamically created ImageButton =?Utf-8?B?UGF0cmljayBNYXJ0aQ==?= Microsoft Dot NET 1 18th Feb 2005 01:12 AM
Do a call from a dynamically created button? Gary Kahrau Microsoft VB .NET 3 14th Dec 2004 09:17 AM
call methods via a string name (dynamically) at runtime? K. Shier Microsoft VB .NET 14 15th Oct 2003 03:41 PM
dynamically created controls Konrad Microsoft Dot NET Framework Forms 2 13th Oct 2003 02:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:16 AM.