Consulting

Results 1 to 9 of 9

Thread: Solved: Combining Multiple Textboxes

  1. #1
    VBAX Regular
    Joined
    Jan 2005
    Posts
    10
    Location

    Solved: Combining Multiple Textboxes

    I found the code for taking the text in multiple textboxes, and combining it into one textbox. I was able to successfully do that however, I am now trying to also copy in the label in front of the textbox as well.

    Please see my example below, and let me know if you could be of any help. Thanks!

    desired results in 1 textbox (both the label and textbox contents combined):
    field1: my
    field2: name
    field3: is

    my results:
    field1: my
    field1: name
    field2: name
    field2: is
    field3: is


    my code:
    [VBA] Private Sub Command9_Click()
    Dim PText As Control
    Dim Temp As String
    Dim label As String
    Dim combine As String
    Dim combine2 As String


    For Each PText In Me.Controls
    If InStr(1, PText.Name, "lbl") > 0 Then
    label = PText.Caption
    ElseIf InStr(1, PText.Name, "field") > 0 Then
    PText.SetFocus
    Temp = PText.Text
    End If
    combine = label & Temp & vbCrLf
    combine2 = combine2 & combine
    Next PText

    Text6.SetFocus
    Text6.Text = combine2

    End Sub[/VBA]

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Are you tring to do this?
    [vba]
    For Each PText In Me.Controls
    If InStr(1, PText.Name, "lbl") > 0 Then
    combine = conbine & PText.Caption & vbNewLine
    ElseIf InStr(1, PText.Name, "field") > 0 Then
    combine = combine & PText.Text & vbNewLine
    End If
    Next PText
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Jan 2005
    Posts
    10
    Location
    Jacob,

    This helped alot!!! This did put the label and the textbox information into 1 textbox on the same line! 1 last problem though, I am now getting the textbox information and than the label, instead of label and textbox information. Please see the example below, and my code...hope you can be of assistance. Thanks!

    Output:
    David Name
    August 22 DOB
    5 Test Road Address

    Code:
    [VBA] For Each PText In Me.Controls
    If InStr(1, PText.Name, "lbl") > 0 Then
    combine = combine & PText.Caption & vbNewLine
    ElseIf InStr(1, PText.Name, "field") > 0 Then
    PText.SetFocus
    combine = combine & PText.Text & " "
    End If
    Next PText

    Text7.SetFocus
    Text7.Text = combine[/VBA]

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    So they are all backwards in the loop? We can try something like this to force the order.
    [vba]
    Dim LblText As String
    Dim TextBoxText As String

    For Each PText In Me.Controls
    If InStr(1, PText.Name, "lbl") > 0 Then
    LblText = PText.Caption
    ElseIf InStr(1, PText.Name, "field") > 0 Then
    TextBoxText = PText.Text
    End If
    If LblText <> "" And TextBoxText <> "" Then
    combine = combine & LblText & " " & TextBoxText & vbNewLine
    LblText = ""
    TextBoxText = ""
    End If
    Next PText

    Text7.SetFocus
    Text7.Text = combine
    [/vba]

  5. #5
    VBAX Regular
    Joined
    Jan 2005
    Posts
    10
    Location
    Wow worked!!!!! Thanks so much for your help, I really appreciate your time and effort!!!!!

    Once again, thanks for helping me through this!

    Results:
    Name David
    DOB August 22
    Address 5 Test Road

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

  7. #7
    VBAX Regular
    Joined
    Jan 2005
    Posts
    10
    Location
    Ahhh, I was playing with the code and found something. I've tried to fix it, but have been unsuccessful, and it looks like your code accounts for the scenario....but here is what I am seeing. If a user does not enter in text in one of the textboxes, I wouldn't want that label to show, since the text is blank...however, what I see happening is the label for the blank textbox is getting used for the next textbox. Please see my example below, as this is critical to using the code! thanks for all your help once again!

    Example if no Date of Birth is entered, I get the address in the DOB row, and than no address caption...I'd like to see no DOB caption, and the address in the correct row:
    Name: David
    DOB: 55 Test Steet

    Thanks again!

  8. #8
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Ok, try this.
    [vba]
    Dim LblText() As String
    Dim TextBoxText() As String
    Dim i As Long
    Dim j As Long

    For Each PText In Me.Controls
    If InStr(1, PText.Name, "lbl") > 0 Then
    i = i + 1
    ReDim Preserve LblText(1 To i)
    LblText(i) = PText.Caption
    ElseIf InStr(1, PText.Name, "field") > 0 Then
    j = j + 1
    ReDim Preserve TextBoxText(1 To j)
    TextBoxText(j) = PText.Text
    End If
    Next PText
    For i = 1 To j
    If TextBoxText(i) <> "" Then
    combine = combine & LblText(i) & " " & TextBoxText(i) & vbNewLine
    End If
    Next i

    Text7.SetFocus
    Text7.Text = combine
    [/vba]
    What we do is make an array of the text. Then we can run whatever checks on the text we want before making the final string.

  9. #9
    VBAX Regular
    Joined
    Jan 2005
    Posts
    10
    Location
    Unbelievable!!!!!!!!!!!! Thanks, worked like a charm!!!! I've spent soo much time working on this, thanks for your prompt response!

Posting Permissions

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