Solved

writting access database records to dynamic text boxes at run time.

Posted on 2004-10-05
3
194 Views
Last Modified: 2010-04-23
Hi All !!
How can i dynamically and at run time create n (n > 0 ) number of textboxes (maybe using dynamic control arrays) and write to those text boxes n records i just retrieve from my access database.

I need this as soon as you experts can otherwise i will get fired...:-)

Thanks

0
Comment
Question by:komlaaa
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
123654789987 earned 500 total points
ID: 12234798
To add Dynamic textboxes to your form, use a function like this.

 Public Function DynamicControlCreation(ByVal firstInt As Integer, _
        ByVal secondInt As Integer, ByVal newPoint As Integer) As Boolean
        Dim i As Integer
        Dim newTextBox As Integer
        Dim k As Integer
        Dim newLabel As Integer
        'The X coordinate where the first TextBox will appear.
        newTextBox = 425
        'The X coordinate where the first Label will appear.
        newLabel = 210
        'The Y coordinate of the Controls
        k = newPoint
        Try
            For i = firstInt To secondInt
                x = gbObservation.Controls.IndexOf(labelCollection(i))
                'Before new controls are added to the screen the existing controls
                'should be removed.
                If (x >= 0) Then
                    gbObservation.Controls.Remove(labelCollection(i))
                    gbObservation.Controls.Remove(textboxCollection(i))
                End If
                labelCollection(i) = New Label
                Me.labelCollection(i).Location = New System.Drawing.Point(newLabel, k)
                Me.labelCollection(i).Size = New System.Drawing.Size(215, 22)
                Me.labelCollection(i).TextAlign = ContentAlignment.MiddleRight
                Me.labelCollection(i).Font = New System.Drawing.Font _
                    ("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, _
                     System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                Me.labelCollection(i).Text = frmSelectTraits.ReturnTraits(i)
                'Adding the dyanmically created labelCollection to the GroupBox
                gbObservation.Controls.Add(labelCollection(i))
                'The X coordinate of the point where the next label should appear.
                newLabel = newLabel + 285
            Next

            For i = firstInt To secondInt
                textboxCollection(i) = New TextBox
                Me.textboxCollection(i).Location = New System.Drawing.Point(newTextBox, k)
                Me.textboxCollection(i).Size = New System.Drawing.Size(70, 22)
                Me.textboxCollection(i).MaxLength = 12
                'adding the dynamically created textbox to the GroupBox.
                gbObservation.Controls.Add(textboxCollection(i))
                'The X coordinate of the point where the next textbox should appear.
                newTextBox = newTextBox + 285
            Next
            If Not Me.labelCollection(secondInt + 1) Is Nothing Then
                For i = secondInt + 1 To Me.labelCollection.Length - 1
                    gbObservation.Controls.Remove(labelCollection(i))
                    gbObservation.Controls.Remove(textboxCollection(i))
                Next
            End If
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function

U can call such function at form_load or on the click of a button.

Then u can retrieve the record from the database to a dataset.

then u can loop through the textboxes and insert the record into them

for i = 0 to textboxCollection.Count
textboxCollection(i) = dtTablr.Rows(0)(i).Tostring()
Next i
0
 

Author Comment

by:komlaaa
ID: 12237594
Thanks for your prompt reply!! ----

How do i get the count of number of fields in my table?

I haven't test this yet but eveything looks neat to me.

Thanks
0
 

Author Comment

by:komlaaa
ID: 12260170

What does this means in the code you provided?

Me.labelCollection(i).Text = frmSelectTraits.ReturnTraits(i)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now