Extract text box value in code - InfoPath Dev
in

InfoPath Dev

Extract text box value in code

Last post 07-03-2012 06:00 AM by Gennady Vanin (Геннадий Ванин, Новосибирск). 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 07-02-2012 12:22 PM

    Extract text box value in code

    I am trying to write a string of code with vbs which will extract the value from a text box. I will ultimately run an if-then statement with that value (don't think this actually matters but thought I'd put it in here). However, I am having trouble getting the code to actually reference the text box value. I attached the code below if anyone has any ideas. The trxCount text box is an equation that acts as a counter for the number of instances, thus it is an integer if that matters. Any help is appreciated. Dim fieldValue Set fieldValue = XDocument.DOM.selectSingleNode("/my:myFields/my:productSelectionSection/my:productSelectionGroup/my:countGroup/my:trxCount)
    Filed under:
  • 07-03-2012 01:52 AM In reply to

    Re: Extract text box value in code

    shep123:
    I am trying to write a string of code with vbs which will extract the value from a text box. I will ultimately run an if-then statement with that value (don't think this actually matters but thought I'd put it in here). However, I am having trouble getting the code to actually reference the text box value. I attached the code below if anyone has any ideas. The trxCount text box is an equation that acts as a counter for the number of instances, thus it is an integer if that matters. Any help is appreciated. 

     Dim fieldValue 

    Set fieldValue = XDocument.DOM.selectSingleNode("/my:myFields/my:productSelectionSection/my:productSelectionGroup/my:countGroup/my:trxCount)

     

    This should be really easy if to formulate and do WHAT you want instead of HOW

    So, I've done it in Visual Basic or VB.NET 2.0 aka 2005, not sure why you gone fancy with VBS

    Created an Infopath 2010  client (Filler) form, place there TextBox and, in order to check the code. a button

    In "Form Options" (in backstage, having pressed File on ribbon) --> Security and Trust -->  I changed Security Level from Domain to Full Trust

    Having chosen a textbox, on ribbon clicked Developer and then Changed Event button

    This quite helpfully answered you question by inserting into opened in VSTA InternalStartup() method the line:
    • AddHandler EventManager.XmlEvents("/my:myFields/my:field1").Changed, AddressOf field1_Changed

    I declared your fieldValue outside of methods
    • Dim fieldValue As String

    In generated 
    • Public Sub field1_Changed(ByVal sender As Object, ByVal e As XmlEventArgs)

    I inserted (or rather chosen from Intellisense pop-up upon entering dot after parameter e)
    • fieldValue = e.NewValue

    That's it
    Double-checking it

    In generated from pressing in Infopath Designer 2010 "Edit Form Code..." button in form's button properties method
    • Public Sub CTRL2_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
    I inserted 
    • MessageBox.Show(fieldValue)
    For me it worked like a charm, though I do not even know VB.NET, I am a C#/ASP.NET guy!


    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 07-03-2012 04:12 AM In reply to

    Re: Extract text box value in code

    Hello Shep,

     Firstly, it looks like you're missing a closing quotation mark at the end of the XPath:

    ......my:trxCount)   -->   ......my:trxCount")

    Next, assuming that the XPath you have there is correct, that will return an actual object for that node.  To access the field's actual value, you'll need to access the text property:

        Set fieldValue = XDocument.DOM.selectSingleNode("/my:myFields/my:productSelectionSection/my:productSelectionGroup/my:countGroup/my:trxCount").text

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 07-03-2012 04:19 AM In reply to

    Re: Extract text box value in code

    First, I have infopath 2003 so I don't if that affects the code. Second, I can't use VB, infopath only let me choose between Jscript and VBS. Third, I wanted to avoid and was advised against using full trust since my company would have to worry about the certification every year. I know this makes it harder, which is why I've been struggling. I would also like to avoid using event handlers because I won't be managing this after it is created and if someone needs to make a change I want it as simple as possible. If anyone has any ideas that would be great and if it isn't really possible under these conditions that's fine. I'll just have to come up with a roundabout way of achieving the same result.

  • 07-03-2012 04:29 AM In reply to

    Re: Extract text box value in code

    Just saw your post Jimmy. That helped in that I can actually open the preview. However, when it tries to execute I get an error in form code message. It may just be that my XPath is wrong but I attached the whole sequence anyway:

    Sub productSelectionNextButton_OnClick(eventObj)

    Dim fieldValue

    Set fieldValue = XDocument.DOM.selectSingleNode("/my:myFields/my:productSelectionSection/my:productSelectionGroup/my:countGroup/my:trxCount").text

    If fieldValue =  "1" Then   <----- I also tried without quotations around the 1 
    x = MsgBox("Prompt", 1 , "Title")
         If x = 1 Then XDocument.View.SwitchView("View 3")
         End If
    Else XDocument.View.SwitchView("View 3")
    End If

    End Sub

  • 07-03-2012 04:56 AM In reply to

    Re: Extract text box value in code

    It apprears my issue, or at least 1 of them is with the set aspect. I started adding a single quotation to change parts to comments to find where the quote wasn't executing and when the Set sequence becomes part of the code I get an error. I copied the XPath so I'm a bit baffled on where the issue is.

  • 07-03-2012 05:25 AM In reply to

    Re: Extract text box value in code

    I got the code to work. I just had to remove the word Set.

  • 07-03-2012 05:42 AM In reply to

    Re: Extract text box value in code

    Jimmy:
    To access the field's actual value, you'll need to access the text property:
    Isn't it value property in VBS?
    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 07-03-2012 05:48 AM In reply to

    Re: Extract text box value in code

    shep123:
    I got the code to work. I just had to remove the word Set

    We have never had it in this thread

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 07-03-2012 05:52 AM In reply to

    Re: Extract text box value in code

    It was in the code Jimmy gave me. Look at the the post below your original one.

  • 07-03-2012 06:00 AM In reply to

    Re: Extract text box value in code

    Never mind, I found what Jimmy gave you:

    shep123:
    Dim fieldValue

    Set fieldValue = XDocument.DOM.selectSingleNode("/my:myFields/my:productSelectionSection/my:productSelectionGroup/my:countGroup/my:trxCount)

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
Page 1 of 1 (11 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.