PDA

View Full Version : Solved: Combining Multiple Textboxes



Iamaterp
01-15-2005, 10:53 AM
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:
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

Jacob Hilderbrand
01-15-2005, 05:23 PM
Are you tring to do this?

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

Iamaterp
01-16-2005, 05:01 PM
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:
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

Jacob Hilderbrand
01-16-2005, 05:18 PM
So they are all backwards in the loop? We can try something like this to force the order.

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

Iamaterp
01-16-2005, 05:38 PM
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

Jacob Hilderbrand
01-16-2005, 05:45 PM
You're Welcome :beerchug:

Take Care

Iamaterp
01-16-2005, 07:40 PM
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!

Jacob Hilderbrand
01-16-2005, 08:16 PM
Ok, try this.

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

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.

Iamaterp
01-16-2005, 08:23 PM
Unbelievable!!!!!!!!!!!! Thanks, worked like a charm!!!! I've spent soo much time working on this, thanks for your prompt response!