Click to See Complete Forum and Search --> : How can I assign value of a variable as part of control name?
Nadeem Shahzad
January 30th, 2000, 11:01 PM
Hi.
I have ten text boxes in a form named as txt1 - txt10. I want to add all of them to a table but I don't want to use grid. Now the question is that how can I make increment in the name of text boxes using a variable. I have done it in FoxPro but I have not been able to figure a way to do it in VB.
Thanks
Nadeem Shahzad
nsmalik@usa.net
Lothar Haensler
January 31st, 2000, 01:03 AM
in VB 6 you can use the Controls.Add method to dynamically create controls.
dim i as integer
for i = 1 to 10
Controls.Add "txt" & i, ...
next i
Nadeem Shahzad
January 31st, 2000, 01:47 AM
Thank you very much for your reply.
Perhaps I couldn't make myself clear. I dont want to add controls. I already have controls on the form now when I am going to add data to table either I can:
Repeat this code for every control
private sub cmd1_click()
mytable.addnew
mytable.fields(1)=txt1.text
mytable.update
end Sub
or
use a for loop to increment control's name
I used the method you specified as:
for i = 1 to 10
mytable.fields(1)="txt"&i
next
This works but instead of adding original text of text boxes "txt 1","txt 2".... is added to table
what is wrong?
I appriciate for your hep
Thanx a lot
Nadeem
Lothar Haensler
January 31st, 2000, 01:52 AM
ok, you are trying to assign the Text property of textboxes in a control array to the fields of a recordset?
Dim i as Integer
for i = 0 to Controls.Count - 1
If TypeName(Controls(i)) = "TextBox" then
Debug.print Controls(i).Text
' yourRS.fields(i)=controls(i).Text
End If
next i
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.