PC Review


Reply
Thread Tools Rate Thread

add multiple controls

 
 
stewart
Guest
Posts: n/a
 
      11th Sep 2007
i have a form with a Button (commandbutton1) and a textbox (tb2). The
button procedure takes the input in the textbox and uses it to
determine the number of text boxes it should create. The code appears
to work but I wanted to know if anyone can foresee any problems with
the way I have written the procedure. I am teaching myself VB so it
may not be the most effective way of completing the process. here is
the code, any help would be appreciated.

Private Sub CommandButton1_Click()


Dim i As Integer
For i = 1 To tb2.Value Step 1

With Me.Controls.Add("Forms.textbox.1")
.Top = 200 + (20 * i)
.Left = 15
.Height = 20
.Width = 50
.Name = "txt" & i
End With
Next i
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      11th Sep 2007
I don't see anything obvious.

--
Regards,
Tom Ogilvy


"stewart" wrote:

> i have a form with a Button (commandbutton1) and a textbox (tb2). The
> button procedure takes the input in the textbox and uses it to
> determine the number of text boxes it should create. The code appears
> to work but I wanted to know if anyone can foresee any problems with
> the way I have written the procedure. I am teaching myself VB so it
> may not be the most effective way of completing the process. here is
> the code, any help would be appreciated.
>
> Private Sub CommandButton1_Click()
>
>
> Dim i As Integer
> For i = 1 To tb2.Value Step 1
>
> With Me.Controls.Add("Forms.textbox.1")
> .Top = 200 + (20 * i)
> .Left = 15
> .Height = 20
> .Width = 50
> .Name = "txt" & i
> End With
> Next i
> End Sub
>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Sep 2007
I think I'd check to see that TB2.value was really numeric and maybe even add a
sanity check--If you know that you never want to add more than 20, then check
for that.

And what happens if they click that button a second time?

Did you want to remove the existing textboxes or come up with unique names?



stewart wrote:
>
> i have a form with a Button (commandbutton1) and a textbox (tb2). The
> button procedure takes the input in the textbox and uses it to
> determine the number of text boxes it should create. The code appears
> to work but I wanted to know if anyone can foresee any problems with
> the way I have written the procedure. I am teaching myself VB so it
> may not be the most effective way of completing the process. here is
> the code, any help would be appreciated.
>
> Private Sub CommandButton1_Click()
>
> Dim i As Integer
> For i = 1 To tb2.Value Step 1
>
> With Me.Controls.Add("Forms.textbox.1")
> .Top = 200 + (20 * i)
> .Left = 15
> .Height = 20
> .Width = 50
> .Name = "txt" & i
> End With
> Next i
> End Sub


--

Dave Peterson
 
Reply With Quote
 
stewart
Guest
Posts: n/a
 
      11th Sep 2007
On Sep 10, 10:37 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> I think I'd check to see that TB2.value was really numeric and maybe even add a
> sanity check--If you know that you never want to add more than 20, then check
> for that.
>
> And what happens if they click that button a second time?
>
> Did you want to remove the existing textboxes or come up with unique names?
>
>
>
> stewart wrote:
>
> > i have a form with a Button (commandbutton1) and a textbox (tb2). The
> > button procedure takes the input in the textbox and uses it to
> > determine the number of text boxes it should create. The code appears
> > to work but I wanted to know if anyone can foresee any problems with
> > the way I have written the procedure. I am teaching myself VB so it
> > may not be the most effective way of completing the process. here is
> > the code, any help would be appreciated.

>
> > Private Sub CommandButton1_Click()

>
> > Dim i As Integer
> > For i = 1 To tb2.Value Step 1

>
> > With Me.Controls.Add("Forms.textbox.1")
> > .Top = 200 + (20 * i)
> > .Left = 15
> > .Height = 20
> > .Width = 50
> > .Name = "txt" & i
> > End With
> > Next i
> > End Sub

>
> --
>
> Dave Peterson





Thank you dave for pointing those things out. I think I have taken
care of most of it in the code below. Is there code a can run through
a button click that can tell me the names of the controls on my
userform. For testing purposes I'd like to verify that the added
textboxes are being named properly so that i can begin to write code
for processing the data entered into them. THank you for any
suggestions.

Private Sub CommandButton1_Click()

Dim i As Integer

If Not IsNumeric(tb2.Value) Then
MsgBox "Please enter a number"
Exit Sub

ElseIf tb2.Value > 10 Then
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "You chose " & tb2.Value & " cashiers. Is this correct?" '
Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Verify Input" ' Define title.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbNo Then
MsgBox "Please re-enter number of cashiers"
Exit Sub
Else: GoTo x
End If
Else

x: For i = 1 To tb2.Value Step 1

With Me.Controls.Add("Forms.textbox.1")
.Top = 200 + (20 * i)
.Left = 15
.Height = 20
.Width = 50
.Name = "txtNum" & i
End With
Next i

CommandButton1.Enabled = False
End If

End Sub

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Sep 2007
Option Explicit
Sub testme01()
Dim OLEObj As OLEObject
For Each OLEObj In ActiveSheet.OLEObjects
If TypeOf OLEObj.Object Is MSForms.TextBox Then
MsgBox OLEObj.Name
End If
Next OLEObj
End Sub

Ron de Bruin has lots of information for working with shapes/controls:
http://www.rondebruin.nl/controlsobjectsworksheet.htm

stewart wrote:
>
> On Sep 10, 10:37 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> > I think I'd check to see that TB2.value was really numeric and maybe even add a
> > sanity check--If you know that you never want to add more than 20, then check
> > for that.
> >
> > And what happens if they click that button a second time?
> >
> > Did you want to remove the existing textboxes or come up with unique names?
> >
> >
> >
> > stewart wrote:
> >
> > > i have a form with a Button (commandbutton1) and a textbox (tb2). The
> > > button procedure takes the input in the textbox and uses it to
> > > determine the number of text boxes it should create. The code appears
> > > to work but I wanted to know if anyone can foresee any problems with
> > > the way I have written the procedure. I am teaching myself VB so it
> > > may not be the most effective way of completing the process. here is
> > > the code, any help would be appreciated.

> >
> > > Private Sub CommandButton1_Click()

> >
> > > Dim i As Integer
> > > For i = 1 To tb2.Value Step 1

> >
> > > With Me.Controls.Add("Forms.textbox.1")
> > > .Top = 200 + (20 * i)
> > > .Left = 15
> > > .Height = 20
> > > .Width = 50
> > > .Name = "txt" & i
> > > End With
> > > Next i
> > > End Sub

> >
> > --
> >
> > Dave Peterson

>
> Thank you dave for pointing those things out. I think I have taken
> care of most of it in the code below. Is there code a can run through
> a button click that can tell me the names of the controls on my
> userform. For testing purposes I'd like to verify that the added
> textboxes are being named properly so that i can begin to write code
> for processing the data entered into them. THank you for any
> suggestions.
>
> Private Sub CommandButton1_Click()
>
> Dim i As Integer
>
> If Not IsNumeric(tb2.Value) Then
> MsgBox "Please enter a number"
> Exit Sub
>
> ElseIf tb2.Value > 10 Then
> Dim Msg, Style, Title, Help, Ctxt, Response, MyString
> Msg = "You chose " & tb2.Value & " cashiers. Is this correct?" '
> Define message.
> Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
> Title = "Verify Input" ' Define title.
> Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> If Response = vbNo Then
> MsgBox "Please re-enter number of cashiers"
> Exit Sub
> Else: GoTo x
> End If
> Else
>
> x: For i = 1 To tb2.Value Step 1
>
> With Me.Controls.Add("Forms.textbox.1")
> .Top = 200 + (20 * i)
> .Left = 15
> .Height = 20
> .Width = 50
> .Name = "txtNum" & i
> End With
> Next i
>
> CommandButton1.Enabled = False
> End If
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
stewart
Guest
Posts: n/a
 
      12th Sep 2007
On Sep 11, 6:19 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> Option Explicit
> Sub testme01()
> Dim OLEObj As OLEObject
> For Each OLEObj In ActiveSheet.OLEObjects
> If TypeOf OLEObj.Object Is MSForms.TextBox Then
> MsgBox OLEObj.Name
> End If
> Next OLEObj
> End Sub
>
> Ron de Bruin has lots of information for working with shapes/controls:http://www.rondebruin.nl/controlsobjectsworksheet.htm
>
>
>
> stewart wrote:
>
> > On Sep 10, 10:37 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> > > I think I'd check to see that TB2.value was really numeric and maybe even add a
> > > sanity check--If you know that you never want to add more than 20, then check
> > > for that.

>
> > > And what happens if they click that button a second time?

>
> > > Did you want to remove the existing textboxes or come up with unique names?

>
> > > stewart wrote:

>
> > > > i have a form with a Button (commandbutton1) and a textbox (tb2). The
> > > > button procedure takes the input in the textbox and uses it to
> > > > determine the number of text boxes it should create. The code appears
> > > > to work but I wanted to know if anyone can foresee any problems with
> > > > the way I have written the procedure. I am teaching myself VB so it
> > > > may not be the most effective way of completing the process. here is
> > > > the code, any help would be appreciated.

>
> > > > Private Sub CommandButton1_Click()

>
> > > > Dim i As Integer
> > > > For i = 1 To tb2.Value Step 1

>
> > > > With Me.Controls.Add("Forms.textbox.1")
> > > > .Top = 200 + (20 * i)
> > > > .Left = 15
> > > > .Height = 20
> > > > .Width = 50
> > > > .Name = "txt" & i
> > > > End With
> > > > Next i
> > > > End Sub

>
> > > --

>
> > > Dave Peterson

>
> > Thank you dave for pointing those things out. I think I have taken
> > care of most of it in the code below. Is there code a can run through
> > a button click that can tell me the names of the controls on my
> > userform. For testing purposes I'd like to verify that the added
> > textboxes are being named properly so that i can begin to write code
> > for processing the data entered into them. THank you for any
> > suggestions.

>
> > Private Sub CommandButton1_Click()

>
> > Dim i As Integer

>
> > If Not IsNumeric(tb2.Value) Then
> > MsgBox "Please enter a number"
> > Exit Sub

>
> > ElseIf tb2.Value > 10 Then
> > Dim Msg, Style, Title, Help, Ctxt, Response, MyString
> > Msg = "You chose " & tb2.Value & " cashiers. Is this correct?" '
> > Define message.
> > Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
> > Title = "Verify Input" ' Define title.
> > Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> > If Response = vbNo Then
> > MsgBox "Please re-enter number of cashiers"
> > Exit Sub
> > Else: GoTo x
> > End If
> > Else

>
> > x: For i = 1 To tb2.Value Step 1

>
> > With Me.Controls.Add("Forms.textbox.1")
> > .Top = 200 + (20 * i)
> > .Left = 15
> > .Height = 20
> > .Width = 50
> > .Name = "txtNum" & i
> > End With
> > Next i

>
> > CommandButton1.Enabled = False
> > End If

>
> > End Sub

>
> --
>
> Dave Peterson


is that code designed for use in a userform? I can't seem to get it
to work. I do believe there is something wrong with the naming
portion of my code. I setup the test below

this put in a text box with a specific name of txtNum1
Private Sub CommandButton5_Click()
With Me.Controls.Add("Forms.textbox.1")
.Top = 300
.Left = 100
.Height = 15
.Width = 50
.Name = "txtNum1"
End With
End Sub

then i tried to place the value entered into hat textbox to a
worksheet with this

Private Sub CommandButton3_Click()
Cells(1, 1).Value = textnum1.Value
End Sub

and i get a message that says
"compile error variable not defined" and it highlights
"textnum1.value" in the code for button3


 
Reply With Quote
 
stewart
Guest
Posts: n/a
 
      12th Sep 2007
On Sep 12, 7:21 am, stewart <smccallis...@gmail.com> wrote:
> On Sep 11, 6:19 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
>
>
>
> > Option Explicit
> > Sub testme01()
> > Dim OLEObj As OLEObject
> > For Each OLEObj In ActiveSheet.OLEObjects
> > If TypeOf OLEObj.Object Is MSForms.TextBox Then
> > MsgBox OLEObj.Name
> > End If
> > Next OLEObj
> > End Sub

>
> > Ron de Bruin has lots of information for working with shapes/controls:http://www.rondebruin.nl/controlsobjectsworksheet.htm

>
> > stewart wrote:

>
> > > On Sep 10, 10:37 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> > > > I think I'd check to see that TB2.value was really numeric and maybe even add a
> > > > sanity check--If you know that you never want to add more than 20, then check
> > > > for that.

>
> > > > And what happens if they click that button a second time?

>
> > > > Did you want to remove the existing textboxes or come up with unique names?

>
> > > > stewart wrote:

>
> > > > > i have a form with a Button (commandbutton1) and a textbox (tb2). The
> > > > > button procedure takes the input in the textbox and uses it to
> > > > > determine the number of text boxes it should create. The code appears
> > > > > to work but I wanted to know if anyone can foresee any problems with
> > > > > the way I have written the procedure. I am teaching myself VB so it
> > > > > may not be the most effective way of completing the process. here is
> > > > > the code, any help would be appreciated.

>
> > > > > Private Sub CommandButton1_Click()

>
> > > > > Dim i As Integer
> > > > > For i = 1 To tb2.Value Step 1

>
> > > > > With Me.Controls.Add("Forms.textbox.1")
> > > > > .Top = 200 + (20 * i)
> > > > > .Left = 15
> > > > > .Height = 20
> > > > > .Width = 50
> > > > > .Name = "txt" & i
> > > > > End With
> > > > > Next i
> > > > > End Sub

>
> > > > --

>
> > > > Dave Peterson

>
> > > Thank you dave for pointing those things out. I think I have taken
> > > care of most of it in the code below. Is there code a can run through
> > > a button click that can tell me the names of the controls on my
> > > userform. For testing purposes I'd like to verify that the added
> > > textboxes are being named properly so that i can begin to write code
> > > for processing the data entered into them. THank you for any
> > > suggestions.

>
> > > Private Sub CommandButton1_Click()

>
> > > Dim i As Integer

>
> > > If Not IsNumeric(tb2.Value) Then
> > > MsgBox "Please enter a number"
> > > Exit Sub

>
> > > ElseIf tb2.Value > 10 Then
> > > Dim Msg, Style, Title, Help, Ctxt, Response, MyString
> > > Msg = "You chose " & tb2.Value & " cashiers. Is this correct?" '
> > > Define message.
> > > Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
> > > Title = "Verify Input" ' Define title.
> > > Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> > > If Response = vbNo Then
> > > MsgBox "Please re-enter number of cashiers"
> > > Exit Sub
> > > Else: GoTo x
> > > End If
> > > Else

>
> > > x: For i = 1 To tb2.Value Step 1

>
> > > With Me.Controls.Add("Forms.textbox.1")
> > > .Top = 200 + (20 * i)
> > > .Left = 15
> > > .Height = 20
> > > .Width = 50
> > > .Name = "txtNum" & i
> > > End With
> > > Next i

>
> > > CommandButton1.Enabled = False
> > > End If

>
> > > End Sub

>
> > --

>
> > Dave Peterson

>
> is that code designed for use in a userform? I can't seem to get it
> to work. I do believe there is something wrong with the naming
> portion of my code. I setup the test below
>
> this put in a text box with a specific name of txtNum1
> Private Sub CommandButton5_Click()
> With Me.Controls.Add("Forms.textbox.1")
> .Top = 300
> .Left = 100
> .Height = 15
> .Width = 50
> .Name = "txtNum1"
> End With
> End Sub
>
> then i tried to place the value entered into hat textbox to a
> worksheet with this
>
> Private Sub CommandButton3_Click()
> Cells(1, 1).Value = textnum1.Value
> End Sub
>
> and i get a message that says
> "compile error variable not defined" and it highlights
> "textnum1.value" in the code for button3


I did find an example of another code to check the name of my created
textboxes and it is properly naming them. However I still come up
with the error variable not defined when i try to access the data in
that box. ANy suggestions?

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      12th Sep 2007
I used textbox1 as the name of my textbox:

Option Explicit
Private Sub CommandButton3_Click()
Me.Cells(1, 1).Value = Me.OLEObjects("textbox1").Object.Value
End Sub

stewart wrote:
>
>
> I did find an example of another code to check the name of my created
> textboxes and it is properly naming them. However I still come up
> with the error variable not defined when i try to access the data in
> that box. ANy suggestions?


--

Dave Peterson
 
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
Multiple AddHandler for multiple Controls on a form GAZ Microsoft Dot NET 2 20th Jul 2007 11:06 AM
Multiple AddHandler for multiple Controls on a form GAZ Microsoft VB .NET 2 20th Jul 2007 11:06 AM
Validation Server Controls used with multiple HtmlInputButton Controls nzanella@cs.mun.ca Microsoft C# .NET 0 30th Jan 2005 04:22 AM
Linking Multiple Tables to Multiple Controls Anthony Law Microsoft Access Database Table Design 1 21st Feb 2004 06:22 PM
Multiple controls with the same ID '_ctl24' were found. Trace requires that controls have unique IDs Jeff Tolman Microsoft ASP .NET 0 4th Nov 2003 09:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:02 AM.