Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / General MS InfoPath Questions / April 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fred Lopez - 05 Aug 2005 15:31 GMT
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.
Scott L. Heim [MSFT] - 05 Aug 2005 16:17 GMT
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
Fred Lopez - 05 Aug 2005 17:12 GMT
Hi Scott, unfortunatly I'm a novice at this stuff, where exactly do I input
this script? I don't see an "OnAfterChange" anywhere in the properties for
the drop-down list. I searched help and it brought up something about editing
VB script, I opened up the vb script window and pasted what you posted into
it and changed the "field2" to match the field name I created on my form but
I don't know how to "make it work" for lack of a better term.

Thanks
Scott L. Heim [MSFT] - 05 Aug 2005 18:32 GMT
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
Fred Lopez - 05 Aug 2005 18:50 GMT
Scott, that worked. I totally missed the onafterchage box the first time!
Thanks so much!
Kenneth - 05 Aug 2005 22:41 GMT
Hi Scott,

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

> Hi Fred,
>
[quoted text clipped - 63 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights
KLaw - 10 Aug 2006 17:17 GMT
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

> Hi Fred,
>
[quoted text clipped - 63 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights
Greg Collins [InfoPath MVP] - 10 Aug 2006 21:02 GMT
You said you are using VBScript, yet the syntax you show is for JScript.

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

Signature

Greg Collins [Microsoft MVP]
Visit Brain Trove ( http://www.BrainTrove.com )
Visit InfoPathDev ( http://www.InfoPathDev.com )

S.Y.M. Wong-A-Ton - 11 Aug 2006 06:29 GMT
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
---
S.Y.M. Wong-A-Ton

> Hi Scott:
>
[quoted text clipped - 77 lines]
> >
> > This posting is provided "AS IS" with no warranties, and confers no rights
BostonCC - 25 Apr 2007 16:26 GMT
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......

> Hi Fred,
>
[quoted text clipped - 63 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights
BostonCC - 26 Apr 2007 13:00 GMT
Does anyone have an answer?  Thanks

> Man! - that was some great technical help - Awesome Job Scott.
>
[quoted text clipped - 91 lines]
> >
> > This posting is provided "AS IS" with no warranties, and confers no rights
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.