PDA

View Full Version : Solved: Transfering the result from a combobox to multiple textboxes



efto
11-08-2005, 05:14 AM
I am making a template that is using both user forms and form fields. Since there can be only 25 items in a list box i am using a textbox which runs a macro on entry. That macro shows a user form with one combobox inside (supports more than 25 items). When changing the value of the combobox this code is executed:

Private Sub ComboBox1_Change()
ActiveDocument.FormFields("Text1").Result = ComboBox1.Value
End Sub

Currently it changes the value of Text1. I want this user form to be trigered from several different textboxes and to change only the value of that particular textbox.

gibbo1715
11-08-2005, 05:37 AM
Why do you think a listbox can only contain 25 items?

This works for me

Private Sub UserForm_Activate()
For i = 1 To 50
Me.ListBox1.AddItem i
Next
End Sub

gibbo1715
11-08-2005, 06:27 AM
If you want to use a list box and you are opening a userform from a formfield clicking on the text entry and using the following code on the double click of the listbox should work for you I think

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim FieldResult As String
If Selection.Bookmarks.Count >= 1 Then
FieldResult = Selection.Bookmarks(1).Name
End If
ActiveDocument.FormFields(FieldResult).Result = ListBox1.Value
Unload Me
End Sub

'Change this line for a combobox
ActiveDocument.FormFields(FieldResult).Result = ComboBox1.Value



Cheers

Gibbo

efto
11-08-2005, 08:20 AM
It would be beter if I upload an example.

I want to use Combobox1 in frmcombo (user form) to ender data in all text form fields independently. In this case it can be done only for text form field "Text1"

Please advise http://vbaexpress.com/forum/images/smilies/100.gif

fumei
11-08-2005, 10:25 AM
I want to use Combobox1 in frmcombo (user form) to ender data in all text form fields independently.

What is the problem???? Your combobox can put its value into any textbox you want. Simply write the logic to do so.

You state "it can be done only for text form field "Text1". Nonsense. It only puts it in Text1 because you wrote the code to put it in Text1. So...write the code to make it put it into whatever textbox you want.

Generally speaking, you should rename your object explicitly. Keeping the default objects name is not a good idea. ESPECIALLY formfields! Word assigns names by the order they appear in the document. Text1 refers to the FIRST text formfield, NOT a specific formfield. If you move Text1 so it is the second formfield in the document, its name is automatically changed to Text2 - without telling you.

Again, if you have code that makes a .Result in Text1, that means it puts the result into the FIRST text formfield, NOT any specific one.

Other comments: The userform is initiated by entry into the formfield. Are you sure you want to do this? What if the user never does?

Write out - on paper preferably - exactly the logic you want. EXACTLY the steps you want to happen.

Do you want the userform choices to happen as a result of the Item selection you have in the left column? Do you want it fire for EVERY selection? Do you want it to fire for every item?

This is strictly a logic issue. You are the one to make the logic decisions. Do so and then we can help with the code.

efto
11-09-2005, 02:00 AM
Thank you for your suggestions. I will try to explain more clearly.

Currently the userform is initiated by entry into the formfield Text1 (in the final template i will rename all objects explicitly). Then it puts the result of the combobox in the textbox.

Private Sub ComboBox1_Change()
ActiveDocument.FormFields("Text1").Result = ComboBox1.Value
End Sub
One solution is to write another macro which runs on entry for textbox Text2
Private Sub ComboBox1_Change()
ActiveDocument.FormFields("Text2").Result = ComboBox1.Value
End Sub
Then another macro which runs on entry for textbox Text3
Private Sub ComboBox1_Change()
ActiveDocument.FormFields("Text3").Result = ComboBox1.Value
End Sub
and so on.
Maybe i will have more than 30 such textboxes. Instead of doing that i need a simplier solution. Some quasi macro like this:
Private Sub ComboBox1_Change()
ActiveDocument.FormFields("Textbox that triggered this macro").Result = ComboBox1.Value
End Sub
The results of the other textboxes should stay unchanged. Only the result of the selected textbox should change.

The combobox contains description of colors. I need to describe more then 30 items in the document using colors. The final combobox "Colors" ( Combobox1) will contain every single color. So i want to make one combobox and use it many times with different textboxes.

Thank you for your patience

gibbo1715
11-09-2005, 03:45 AM
Not sure if im missing something here but from what you ve said my original suggestion will work for you as follows

Private Sub ComboBox1_Change()
Dim FieldResult As String ' set string to hold bookmark name
If Selection.Bookmarks.Count >= 1 Then
FieldResult = Selection.Bookmarks(1).Name ' get name of textform
End If
' this line will only update the formfield that is active i.e text1, text2, text3 etc
ActiveDocument.FormFields(FieldResult).Result = ComboBox1.Value
Unload Me
End Sub


Then just set up your on entry macro as follows for every textfield you want to make call the userform to run the combobox change code

Sub EntryCode()
UserFrom1.Show ' your userform name
End Sub


Hope this time i got it right:)

Gibbo

efto
11-09-2005, 05:11 AM
Thanks gibbo!
That is solution. I apologize because probably i did something wrong at the beginning and the code wasn't running. But now I did it carefully and everything is OK.
Again sorry and many thanks for all of you!

gibbo1715
11-09-2005, 05:25 AM
No problem,

Glad we got it working for you

Gibbo