CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2000
    Location
    Rawalpindi, Pakistan
    Posts
    3

    How can I assign value of a variable as part of control name?

    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

    [email protected]


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: How can I assign value of a variable as part of control name?

    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


  3. #3
    Join Date
    Jan 2000
    Location
    Rawalpindi, Pakistan
    Posts
    3

    Re: How can I assign value of a variable as part of control name?

    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


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: How can I assign value of a variable as part of control name?

    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






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured