+ Reply to Thread
Results 1 to 5 of 5

Get textbox value from custom/dynamically generated form

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2010
    Location
    High Wycombe, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Get textbox value from custom/dynamically generated form

    I have a custom/dynamically generated form with X numbers of textboxes where X is number if elements from WorkSheet1. It also generates an Update button and a Cancel button.

    The form is generating fine. For instance if the number of cells with value in WorkSheet1 is 10, then the form generates 10 textboxes and it puts cell value as the value of the textboxes.
    Dim MilestoneSheet As Worksheet Set MilestoneSheet = Worksheets("MIilestoneDates") Set MilestoneDate = milestoneSheet.Range("A1") While (MilestoneDate.Value <> "")
    Dim textbox As Control Set textbox = MilestoneForm.Controls.Add("Forms.TextBox.1") Dim MilestoneDate As Date
    'Set textbox properties With textbox
    .Height = 20 .Left = 2 .Top = 6 .Width = 90
    'Set the name and value of the Textbox
    .Name = "TextBox" & MilestoneDate.Row .Value = MilestoneDate
    End With Set MilestoneDate = MilestoneDate.EntireColumn.Rows(MilestoneDate.Row + 1)
    Wend
    It starts with A1 of WorkSheet1 and goes down one cell a time and adds that cell value to the textbox value till it reaches an empty cell.

    What I need to do now is on the form the value of any textbox can be changed by the user and once when they click the Update button it should then change only those cells in WorkSheet1 that had its textbox value changed by the user.

    For instances if there are 10 cell with values and the form generated 10 textboxes, it should then change the value of cell 3 and 7 when the value in textbox 3 and 7 are changed by the user in the form.

    Can I get some in solving this issue. Thanks in advance.
    Last edited by dominicb; 04-16-2010 at 07:01 AM.

  2. #2
    Registered User
    Join Date
    04-16-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Get textbox value from custom/dynamically generated form

    Another option you could use is take advantage of the change procedure to set a bit if the value has changed (off-course this would also require dynamically writing code as far as I know) and then with the update it only updates (and resets the bit) of the boxes were the value has changed (I do not know if change considers change and then changing back as change though, I would imagine so). I do not know though how this would compare to just rewriting all the data or comparing to the original data.

  3. #3
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile Re: Get textbox value from custom/dynamically generated form

    Good morning sonyidicula

    You could set up an array and fill it with all values that are entered dynamically into your textboxes, then before you write the values back to the worksheet do a check to see which ones have changed.

    However, given the amount of time it's going to take to write 10 values to a worksheet (ie, not much) why not just loop through all controls and write them all back to the sheet.

    I know it's cheating, but you could spend ages coding the check, to gain milliseconds in terms of time.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  4. #4
    Registered User
    Join Date
    04-16-2010
    Location
    High Wycombe, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Get textbox value from custom/dynamically generated form

    How do I loop through all controls and write them all back to the sheet. How do I get the value of those textboxes. I used the code below, but its not working.

    Dim cCont As Control
    Dim newDate As Range
        For Each cCont In MilestoneForm.Controls
              If TypeName(cCont) = "TextBox" Then
    
               'The line below is wrong
                newDate.Value = TypeName(cCont).Value
                
                'Set newDate array with values the textbox have after clicking Update
                Set newDate = newDate.EntireColumn.Rows(newDate.Row + 1)
                
            End If
        Next cCont
    End Sub
    Last edited by sonyidicula; 04-16-2010 at 07:30 AM.

  5. #5
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile Re: Get textbox value from custom/dynamically generated form

    Hi sonyidicula
    Quote Originally Posted by sonyidicula View Post
    How do I loop through all controls and write them all back to the sheet. How do I get the value of those textboxes.
    Thanks for remembering to wrap your code.
    I set up a userform with 10 TextBoxes (named (as in your example) TextBox1 to TextBox10), with one button (CommandButton1).

    This code worked to loop through all textboxes and tranfer the values to the active worksheet :
    Private Sub CommandButton1_Click()
    For n = 1 To 10
    Cells(n, 1).Value = UserForm1.Controls("Textbox" & n).Value
    Next n
    End Sub
    Obviously, n may change each time, but you can trap that easy enough from the row number.

    HTH

    DominicB

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1