How to a populate text field from drop-down list box.

F

Fred Lopez

I want to create a list in a drop-down list box and when a user selects an
item from the list it puts the item in a separate text box and when
subsequent items are selected from the drop-down list it ADDs them to the
text box.

Example:
Drop-down list box has the following data: item1, item2, item3, item4.

The user first selects item1. item1 appears in a text box below the
drop-down list(this much I've figured out). but if the user goes back and
selects item3 for example it changes the text box to item3. I want it to
*add* item3 to the text box so the text box would now read "item1, item3."

Any help would be appreciated.
 
S

Scott L. Heim [MSFT]

Hi Fred,

Here is some sample VBScript that should do what you need - I have this in
the "OnAfterChange" event for my drop-down list:

** NOTE: "field2" is my text box

If eventObj.Operation = "Insert" Then
Dim objField2
Set objField2 = XDocument.DOM.selectSingleNode("//my:myFields/my:field2")

If objField2.text = "" Then
objField2.text = eventObj.Site.text
Else
objField2.text = objField2.text & ", " & eventObj.Site.text
End If
Set objField2 = nothing
End If

Best regards,

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Scott L. Heim [MSFT]

Hi Fred,

Complete these steps so you can see how this works - then you can work on
implementing this in your solution.

- Create a new, blank InfoPath solution
- From the Tools menu, choose Form Options
- Select the Advanced tab
- Make a note of what Programming Language is specified so you can use the
appropriate code sample below
- Click OK or Cancel to close the window
- Display the Controls Task Pane
- Add a Drop-down List Box and a Text Box to the form (they should be named
"field1" and "field2" respectively)
- Right-click on the Drop-down List Box and choose Properties
- Click the Add button to add entries to the list box - add some entries to
this control and then leave this screen open
- Click the Data Validation button
- From the "Events" box select OnAfterChange
- Click the Edit button
- If you noted the Programming Language as VBScript, then add the following
code just before the "End Sub" line:

If eventObj.Operation = "Insert" Then
Dim objField2
Set objField2 = XDocument.DOM.selectSingleNode("//my:myFields/my:field2")

If objField2.text = "" Then
objField2.text = eventObj.Site.text
Else
objField2.text = objField2.text & ", " & eventObj.Site.text
End If
Set objField2 = nothing
End If

If you noted the Programming Language as JScript, then add the following
code immediately before the last "closing brace" ("}"):

if(eventObj.Operation == "Insert")
{
var objField2 = XDocument.DOM.selectSingleNode("//my:myFields/my:field2");

if(objField2.text == "")
{
objField2.text = eventObj.Site.text;
}
else
{
objField2.text = objField2.text + ", " + eventObj.Site.text;
}
objField2 = null;
}

- Save and close the Script Editor window (this should put you back to your
InfoPath form)
- Preview the form and make a selection from the drop-down box. The first
time, it should add the text to the box - subsequent times it should
"append" the selection to the existing text.

I hope this helps!

Best Regards,

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

Fred Lopez

Scott, that worked. I totally missed the onafterchage box the first time!
Thanks so much!
 
K

Kenneth

Hi Scott,

Really admire your patience when I read your answers. It is in such details
that for sure a big help for novice.
 
K

KLaw

Hi Scott:

I am trying to use this solution that you recommended last year and it's
exactly what I need. However, I keep getting a error: I'm using VScript and
this line is giving me an issue: if(objField2.text == "") The error says
I'm missing an object. I am not a programmer, hence my confusion. Should
there be two equal signs? Do I need to put something in between the quotes?
Do I need to replace Field2 with my field name in every line? I've tried all
of these options and still get an error.

Any help is surely appreciated.

Kammy
 
G

Greg Collins [InfoPath MVP]

You said you are using VBScript, yet the syntax you show is for JScript.

JScript: if(objField2.text == "")
VBScript: If objField2.text = "" Then
 
S

S.Y.M. Wong-A-Ton

If the error says that you're missing an object, probably "object required",
objField2 is not an object. I'm assuming that you have an XPath somewhere to
retrieve the node corresponding to objField2. Check to see whether this XPath
is correct.

if(objField2.text == "")

should be in VBScript

If objField2.text = "" Then
....
End If
 
B

BostonCC

Man! - that was some great technical help - Awesome Job Scott.

I have one question to add to this:

where in that code can I insert (and what code for that matter) something to
be able to separate the value/list that the person chooses?

For example, in your code, as the person chooses different values, in the
text box, it runs them continuosly, i.e., Value1, Value2, Value3, (like a
sentence) etc...

I need mine to be listed vertically.....i.e.,

value1
value2
value3, etc.

Ideally, if each value chosen would start with a bullet or a dash, that
would be even better

: )

Just from hanging around this forum I've learn so much - it's great that its
available.

To all of you who provide us "non Tech" folks the answers - THANK YOU!!!! -
It's greatly appreciated......
 
B

BostonCC

Does anyone have an answer? Thanks

BostonCC said:
Man! - that was some great technical help - Awesome Job Scott.

I have one question to add to this:

where in that code can I insert (and what code for that matter) something to
be able to separate the value/list that the person chooses?

For example, in your code, as the person chooses different values, in the
text box, it runs them continuosly, i.e., Value1, Value2, Value3, (like a
sentence) etc...

I need mine to be listed vertically.....i.e.,

value1
value2
value3, etc.

Ideally, if each value chosen would start with a bullet or a dash, that
would be even better

: )

Just from hanging around this forum I've learn so much - it's great that its
available.

To all of you who provide us "non Tech" folks the answers - THANK YOU!!!! -
It's greatly appreciated......
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top