PDA

View Full Version : How to get the text of dynamic created objects in a UserForm



kerb
01-20-2013, 10:17 PM
Hi,
I'm trying to create a "Wizard" kind of form that will get data from the user and once its complete, will write the document formating its contents and tables.

I'm stuck on passing values from the first step to the second (Forms)
Option Explicit
Private Sub CommandButton1_Click()
step1.Hide
Step2.Show
End Sub
Private Sub TextBox3_Change()
Dim ctl As Control
Dim ctl1 As Control
Dim i As Integer
For i = 1 To CInt(TextBox3.Text)
Set ctl = Me.Controls.Add("Forms.Textbox.1")
With ctl

.Visible = True
.Width = 100
.Height = 16.5
.Left = 50
.Top = Me.Height - 129
.Name = "Server"
.Text = ""

End With
Me.Height = Me.Height + 20

Set ctl1 = Me.Controls.Add("Forms.Label.1")
With ctl1

.Visible = True
.Caption = "Server" & " " & i
.Width = 40
.Height = 16.5
.Left = 6
.Top = Me.Height - 145
.Name = "server" & i

End With
Next i

End Sub

I'm able to successfully create the objects with the label I wantted absed on the input on the step1 form but I dont know how can I pass the value (text) of each text field created to a Frame.Caption on the second step (next Form).

I will need to loop on the next form as well ot create separated Frames with their names (Captions) as in each text value entered on this first form.

Any help will be appreciated.

thanks,

Kerb

fumei
01-21-2013, 03:39 PM
The code you posted has nothing at all to do with what you seem to be asking. Why did you post it? To show that you can create userform objects? OK, that you can.

The problem is (among others) primarily one of focus. Only one userform can have focus at one time.

Can you explain why it is you want to do this?

You could get the text you want, dump it into a variable, then when you shift to the other userform check its value, and apply it (if correct) to the frame.caption.

kerb
01-21-2013, 10:34 PM
Hello Fumei,

This is the code on the first form (there are 3) that generates the multiple objects out of a number entered by the user so, if the user enters 2, I will have 2 TextBoxes with 2 labels in front of them.

It works ok but when I click next button that opens the second form:

I need to bring the value (TextBoxY.i) into my other objects.
where Y = the Name created and i the current position on the loop.

It works great with the ones not within the loop but I cant get the values inserted on the dynamically created objects and tahts the help I need now.

I would like also, to be able to delete the value and change the number but when I do that the app crashes on me.

Please let me know if you still need more info, thanks for your asistance,

Kerb,

fumei
01-21-2013, 11:59 PM
I am not understanding what you are looping.

Again, it is a matter of focus. When you bring the second userform up, it has the focus. And again, if you have some values determined on the first userform, put them into global variables. Then use those on the second userform.

You will have to describe EXACTLY what you do when Word crashes. Changing or deleting a "value" should not crash anything. What did you do EXACTLY?

kerb
01-22-2013, 12:14 AM
Fumei,

you keep saying that is a matter of focus but I belive you are not understanding this at all.

there is nothing complicated about creating multiple objects and I have already explained that 2 times.

I need to have sevreral text boxes and get their values thats it.

what is so complicated about it and what focus has to do with anything?

this is not selecting any object, Im creating them so the user can insert their information and later I will get that from another place.

why is that so complicated?

the loop is done and the objects are being cretated
if the user enters 3 I will hve 3 text boxes and so on. what is that you have focus in mind for that?

I only need to know how to get their inserted values thats all.

the .NAME clause is not working so I cant

textbox2.text = step1.textbox4.text being 4 the 4th text box created by the loop.

what do you have in mind that can add to the problem?

only saying its a focus issue is adding nothing to the solution and I have exposed you to my problem, I appreciate your time but if you dont know what I'm asking please let someone else reply with a sugestion.

this is just a forum, we are here to ask and exchange, I appreciate your analyses but it has been not constructive so far and I'm pretty sure I made extra clear what I'm doing here.

again thank you,

Kerb,

fumei
01-22-2013, 06:07 PM
The only reason you are not finding it constructive is because you are not reading very well. I have given you a solution that will work...use global variables.

"there is nothing complicated about creating multiple objects and I have already explained that 2 times. "

I never asked you to repeat anything about the creation of the objects. The ONLY reason you "explained" it twice is because you are not paying attention. I fully understand about creating objects. You can not pass the value from the textbox to a different userform - which is what you are asking, NOT about creating multiple objects (so why repeat it?) - is because once the second userform has focus you can NOT get the value from the first userform (not in focus).

I will repeat the solution: put the textbox value into a global variable. As it is global you can use it on the second userform.

"what is so complicated about it and what focus has to do with anything?"

If you read well, you would know the answer to that.

"but I dont know how can I pass the value (text) of each text field created to a Frame.Caption on the second step (next Form)."

Told you how (global variable) - AND the reason why you had problems (focus).

Try the demo attached. The first userform comes up on Open. Type 2 into the textbox. Just 2. Do not use any other number...this is a simplistic demo.

New textboxes are created, with labels.

Click the commandbutton. The second userform shows, and as you can see, the frames have the captions from the created textboxes on the first userform

This is what you seem to be asking for. But then, as you point out "I belive you are not understanding this at all. " You probably right...I do not understand at all. I likely missed something from your superb and very clear descriptions, including of course the excellent and full description of what happened when you crashed things.

kerb
01-22-2013, 11:04 PM
Hi Fumei,

I really thought you could understand and help but your condescending and sarcastic commentary adds absolutely nothing to the problem.

Forums lately have become this, a please where those who may know something use to lash out their frustration on others asking for help.

At no point on my emails you saw any sarcasm or demeaning of your words, you, in the other hand, have chosen this path.

I'm really not here to fight or create any arguments, I simply asked a question, posted what I had and you are treating me as if I was a complete idiot bagging for your time.

I wont engage on any further discussions with you, I believe you truly know what you are saying but is lousy understanding other people's comments.

thank you very much for your time and I will search somewhere else, I don't want to be a reason of a conflict, was just looking for help.

Peace.

Kerb

gmaxey
01-23-2013, 06:55 AM
Kerb,

Not to add injury to insult, but IMHO the description of your proplem and what you are trying to do here is about as clear as a mud puddle.

Part of the reason why ".NAME Clause" (property) isn't working could be because you are giving all of the textbox controls that you add dynamically the same name.

Maybe .Name = "Server" & i
rather than .Name = "Server"

I'm just guessing here because like I say I don't really understand what you are trying to do. I think you are trying to:


1. Add a number of text boxes in your Form "Step1" based on user input.
2. The user adds text into these text boxes.
3. The user clicks the command button on the Step1 form which hides Step1 and displays Step2
4. Step 2 displays the same text boxes that you added in Step1 that display the text that the user put in those text boxes in Step1.

Am I even close? If so then:

Step1 code:

Private Sub CommandButton1_Click()
Step1.Hide
Step2.Show
End Sub
Private Sub TextBox3_Change()
Dim ctl As Control
Dim ctl1 As Control
Dim i As Integer
For i = 1 To CInt(TextBox3.Text)
Set ctl = Me.Controls.Add("Forms.Textbox.1")
With ctl

.Visible = True
.Width = 100
.Height = 16.5
.Left = 50
.Top = Me.Height - 129
.Name = "Server" & i
.Text = ""

End With
Me.Height = Me.Height + 20

Set ctl1 = Me.Controls.Add("Forms.Label.1")
With ctl1

.Visible = True
.Caption = "Server" & " " & i
.Width = 40
.Height = 16.5
.Left = 6
.Top = Me.Height - 145
.Name = "server" & i

End With
Next i

End Sub

Step2 code:
Private Sub UserForm_Initialize()
Dim ctl As Control
Dim ctl1 As Control
Dim i As Integer
For i = 1 To CInt(Step1.TextBox3.Text)
Set ctl = Me.Controls.Add("Forms.Textbox.1")
With ctl

.Visible = True
.Width = 100
.Height = 16.5
.Left = 50
.Top = Me.Height - 129
.Name = "Server" & i
.Text = Step1.Controls("Server" & i).Text

End With
Me.Height = Me.Height + 20

Set ctl1 = Me.Controls.Add("Forms.Label.1")
With ctl1

.Visible = True
.Caption = "Server" & " " & i
.Width = 40
.Height = 16.5
.Left = 6
.Top = Me.Height - 145
.Name = "server" & i

End With
Next i

End Sub





Fumei,

you keep saying that is a matter of focus but I belive you are not understanding this at all.

there is nothing complicated about creating multiple objects and I have already explained that 2 times.

I need to have sevreral text boxes and get their values thats it.

what is so complicated about it and what focus has to do with anything?

this is not selecting any object, Im creating them so the user can insert their information and later I will get that from another place.

why is that so complicated?

the loop is done and the objects are being cretated
if the user enters 3 I will hve 3 text boxes and so on. what is that you have focus in mind for that?

I only need to know how to get their inserted values thats all.

the .NAME clause is not working so I cant

textbox2.text = step1.textbox4.text being 4 the 4th text box created by the loop.

what do you have in mind that can add to the problem?

only saying its a focus issue is adding nothing to the solution and I have exposed you to my problem, I appreciate your time but if you dont know what I'm asking please let someone else reply with a sugestion.

this is just a forum, we are here to ask and exchange, I appreciate your analyses but it has been not constructive so far and I'm pretty sure I made extra clear what I'm doing here.

again thank you,

Kerb,

fumei
01-23-2013, 02:27 PM
kerb,

only saying its a focus issue is adding nothing to the solution and I have exposed you to my problem, I appreciate your time but if you dont know what I'm asking please let someone else reply with a sugestion.

this is just a forum, we are here to ask and exchange, I appreciate your analyses but it has been not constructive so far and I'm pretty sure I made extra clear what I'm doing here.

I am sorry I was sarcastic (as indeed I admit I was), and yes I was frustrated.

1. you were NOT clear.
2. you did NOT answer direct questions.
3. I posted a real suggestion, a solution that actually works.

You did not bother to even ask about that solution. Let me repeat, it was a possible solution that works and you did not even bother to try or ask about it.

But you made disparaging remarks. You made comments about me not knowing what you are asking. Well, I do know, and I did offer a possible solution. And you choose to ignore it and make negative comments, and you go away from this...I am OK with that.

Greg, I am pretty sure you do not have it corrrectly. I think the OP wants to get the values from Form1 into the FRAME.CAPTION on Foms2. However, as much as the OP has a high opinion of their descriptive abilities, it is unclear whether that values is the TextBoxx LABEL into the Frame.Caption...or the TextBox.Text into the Frame.Caption.

Or heck yeah, I could have it all wrong myself.

gmaxey
01-23-2013, 02:33 PM
Gerry,

Your guess is as good as mine.

gmaxey
01-23-2013, 04:18 PM
Kerb,

At risk of offending you further, may I ask why you have cobbled together a process that involves three userforms? Why not one UserForm with one multi-page control consisting of three pages?

Your choice of a textbox control to enter a numeric value is not very practical. What happens if the user types in "four" or "IV" or "6-2" "6,000,000,000,000 or their mother's maiden name?

As listbox or spinbutton control might be better.

What is the expected range of user input 2 or 3, more, much more? Instead of creating the needed textboxes dynamically and then having to deal with them if you then "change" the number, it might be better put them all on the form and then show or hide them based on the user input. This way "change" is much easier to deal with.



Hi,
I'm trying to create a "Wizard" kind of form that will get data from the user and once its complete, will write the document formating its contents and tables.

I'm stuck on passing values from the first step to the second (Forms)
Option Explicit
Private Sub CommandButton1_Click()
step1.Hide
Step2.Show
End Sub
Private Sub TextBox3_Change()
Dim ctl As Control
Dim ctl1 As Control
Dim i As Integer
For i = 1 To CInt(TextBox3.Text)
Set ctl = Me.Controls.Add("Forms.Textbox.1")
With ctl

.Visible = True
.Width = 100
.Height = 16.5
.Left = 50
.Top = Me.Height - 129
.Name = "Server"
.Text = ""

End With
Me.Height = Me.Height + 20

Set ctl1 = Me.Controls.Add("Forms.Label.1")
With ctl1

.Visible = True
.Caption = "Server" & " " & i
.Width = 40
.Height = 16.5
.Left = 6
.Top = Me.Height - 145
.Name = "server" & i

End With
Next i

End Sub

I'm able to successfully create the objects with the label I wantted absed on the input on the step1 form but I dont know how can I pass the value (text) of each text field created to a Frame.Caption on the second step (next Form).

I will need to loop on the next form as well ot create separated Frames with their names (Captions) as in each text value entered on this first form.

Any help will be appreciated.

thanks,

Kerb

fumei
01-23-2013, 05:43 PM
To others...

Please pay attention to Greg's suggestion. In all my years of using userforms I can count on one hand the number of times multiple userforms were truly required.

I have created many userforms that changed dynamically, both in size, placement, AND number of controls. True, it takes a bit more work up-front to get the userform design right. BUT, it is well worth it. It keeps all code in one place, all controls in one place and completely eliminates any focus issues.

kerb
01-23-2013, 11:29 PM
rating my post higher in order to better explain the prob.

kerb
01-23-2013, 11:30 PM
gmaxey,

Thanks for your suggestion and clear intention to help.

I will try again make myself clear, and again, I don’t have all the experience you guys have, I'm here simply trying to learn something that may help other people.

The project consist on a single Word Doc that will be the final document delivered. In this document I will have several bookmaks that I intend to populate with the values capture by each form.
I will use basically 3 UserForms that I’m naming here as Step1, Step2 and Step3.

Step1 form will capture data that no one knows how many items will have.


http://serversup.com/vba/step1.png
The main values on this form are the 2 questions:
How many servers will be installed? ___ This answer will create the number of fields as necessary for the user provide us with the server names.

The Second questions is the F2 NAME: with the value above of NAMEOFTHETHING (as sampled)

this is why I need multiple objects and my problem here is to get the value of these dynamic created TextBoxes, even when I user the .NAME = Server & i. I can’t get these values separately from the next Form.

The Step2 form is where I will, am at least trying to learn how, to get these values and loop again but in a different form:


http://serversup.com/vba/step2.png

As you can see, I need the value of each .NAME from the dynamic created textboxes before to fill my Fram2.Caption values and separate them in another loop, this time by the number of servers previously entered on Step1 form.

The values are present on any of the forms when I reference them, either from step2 or step3 I can’t get Textbox1.text with no problems and I have no focus issues as mentioned above, all I'm trying to do is to have these dynamic created objects to be reachable by the next forms.

once all is done and properly formatted on Step2, new fields will be created and another set will be passed to Step3 that will list a visual confirmation and one control will then print the values of all fields into the Doc and its bookmarks.

there will be several tables that will follow a specific order (depending on the CheckBox selections made by the user).

this is all I need and this is the best I can do to explain, if still is not clear I apologize for my inconvenience and will stop bugging you all here.

again any assistance is appreciated,

Thanks,

Kerb,

gmaxey
01-24-2013, 03:51 AM
Kerb,

The line of code to reach them is:
Frame2.Caption = step1.Controls("Server" & i).Text
where i is the index in the loop.

For what it is worth, and despite your diffferences over focus, Fumei has already shown you one way to make this work so you could move on to something else. That is assign the Textbox values to a global variable in step1 and use that variable value in step2.

Regardless, attached is a file that:

1. Step1 lets you enter a numerical value.
2. Creates a series of textboxes based on the value entered.
3. When the CB is clicked, step1 form is hidden and step2 form opens.
4. A series of frames are created on step2 captioned based on userinput in step1.

It is crude and without error handling but should demonstrate what you are after.

Good luck.

gmaxey
01-24-2013, 07:41 AM
Kerb,

As with Fumei, you seem determined not to answer questions that I've asked in an effort to assist you. I asked you what was the expected range of the user defined "number" (2 or 3, more , much more)? Assuming the six is the upper limit, in the attached, I've illustrated how you might use a multipage form instead of the three separate forms as you are using now.

Call the UserForm1 in the attached file.




gmaxey,

Thanks for your suggestion and clear intention to help.

I will try again make myself clear, and again, I don’t have all the experience you guys have, I'm here simply trying to learn something that may help other people.

The project consist on a single Word Doc that will be the final document delivered. In this document I will have several bookmaks that I intend to populate with the values capture by each form.
I will use basically 3 UserForms that I’m naming here as Step1, Step2 and Step3.

Step1 form will capture data that no one knows how many items will have.


http://serversup.com/vba/step1.png
The main values on this form are the 2 questions:
How many servers will be installed? ___ This answer will create the number of fields as necessary for the user provide us with the server names.

The Second questions is the F2 NAME: with the value above of NAMEOFTHETHING (as sampled)

this is why I need multiple objects and my problem here is to get the value of these dynamic created TextBoxes, even when I user the .NAME = Server & i. I can’t get these values separately from the next Form.

The Step2 form is where I will, am at least trying to learn how, to get these values and loop again but in a different form:


http://serversup.com/vba/step2.png

As you can see, I need the value of each .NAME from the dynamic created textboxes before to fill my Fram2.Caption values and separate them in another loop, this time by the number of servers previously entered on Step1 form.

The values are present on any of the forms when I reference them, either from step2 or step3 I can’t get Textbox1.text with no problems and I have no focus issues as mentioned above, all I'm trying to do is to have these dynamic created objects to be reachable by the next forms.

once all is done and properly formatted on Step2, new fields will be created and another set will be passed to Step3 that will list a visual confirmation and one control will then print the values of all fields into the Doc and its bookmarks.

there will be several tables that will follow a specific order (depending on the CheckBox selections made by the user).

this is all I need and this is the best I can do to explain, if still is not clear I apologize for my inconvenience and will stop bugging you all here.

again any assistance is appreciated,

Thanks,

Kerb,

fumei
01-24-2013, 01:08 PM
I know you think I am not trying to help, but in fact I am.

I don’t have all the experience you guys have, I'm here simply trying to learn something that may help other people.
No, you do not. I hope you will realize that to learn from those who have experience it is helpful to answer questions asked of you.

So here are some simple direct questions using your PNG examples.

In PNG #1, you have an example of inputing 2 into the number textbox. OK. Two textboxes are created. And labels. Good so far.

The values in the textboxes are: SERVERLALA and SERVERLALOW.

The labels for the textboxes are: Server 1 and Server 2.

In PNG #2 you have Frame2. You want something in the caption. OK.

But what? The VALUE of a textbox? If so, which one? Do you want the Frame.Caption to be SERVERLALA? SERVERLALOW?

The VALUE of a label? If so, which one? Do you want the Frame2.Caption to be Server 1? Server 2?

Simple question.