How to select 2 fields to name form with code? - InfoPath Dev
in

InfoPath Dev

How to select 2 fields to name form with code?

Last post 02-24-2011 02:25 PM by Hilary Stoupa. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 02-14-2011 07:26 AM

    • natchos
    • Not Ranked
    • Joined on 07-26-2010
    • Alberta
    • Posts 9

    How to select 2 fields to name form with code?

    Hello everyone.

    This site has been very handy for me. I found lots of great ideas for my forms, thank you!

    I have a question that I cannot find an answer for, perhaps one of you can help me?

    I want to be able to chose field7 and field10 on one of my forms to create the name of my file when using a custom button with code.

    Here is the code I have, but it will only work for field7, I do not know if it is possible to chose 2 fields to name a file.  If it is, could you please help me with the syntax please?

    string Filename = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field7", this.NamespaceManager).Value;

    this.Print();

    this.CurrentView.Export(@"\\fps1\es\" + Filename + ".pdf", Microsoft.Office.InfoPath.ExportFormat.Pdf);

     

    Thank you very much!

  • 02-14-2011 05:33 PM In reply to

    Re: How to select 2 fields to name form with code?

    Hi:

    You can concatenate your two field values together.

    So, like this:

    string field10 = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field10", this.NamespaceManager).Value;
    string field7 = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field7", this.NamespaceManager).Value;
    string Filename = field7 + " - " + field10;

    Of course, you could also add another field to your form that uses the concatenated values as a default, then just reference that field in your code.

    Now, I would be remiss if I didn't mention to you that you really, really, really, if at all possible, should rename your fields. If your form is not currently in production, you can rename these without too much trouble, and then, in a few months, when you open this template to work on it again, you'll be so happy you did. If you give all your fields and groups meaningful names, future you will thank present you (as will any other designer who ever needs to work on your form).

    If this form is already in production, things get more complicated, because you don't want to risk losing data, and writing a custom upgrade transform is probably a bit advanced. But for next time, be sure to name your fields right from the start.

    Hilary Stoupa

  • 02-16-2011 08:25 AM In reply to

    • natchos
    • Not Ranked
    • Joined on 07-26-2010
    • Alberta
    • Posts 9

    Re: How to select 2 fields to name form with code?

    Good morning, just a quick note to say thank you so much for your help!

    And also, for your tips! Much appreciated! I shall do that in the future.

    Thanks again and have a great day!

    Natacha

  • 02-24-2011 09:40 AM In reply to

    • ashoke
    • Not Ranked
      Male
    • Joined on 02-24-2011
    • Toronto, Canada
    • Posts 21

    Re: How to select 2 fields to name form with code?

    Hi Hilary - Good morning! I have a similar yet simpler situation for a form connected to Access db. I need to get the filename from an input in a node named "PrgYear". Following code isn't working. Can you please have a look and help me figure out the problem? Thanks, Ash

    string filename = this.MainDataSource.CreateNavigator().SelectSingleNode("dfs:queryFields/q:top/@PrgYear", this.NamespaceManager).Value;

    this.CurrentView.Export(@"G:\02 Dept Folders\Clinical Registries\43130 CORR\43132 Transplants\07 Statistics\Transplant Quarterly Reports\ASH\input_form&db\" + filename + "_Table1A.xls", ExportFormat.Mht);

    The error msg:

    System.NullReferenceException
    Object reference not set to an instance of an object.
       at Table_123_exp.FormCode.CTRL830_7_Clicked(Object sender, ClickedEventArgs e)
       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

  • 02-24-2011 12:14 PM In reply to

    • ashoke
    • Not Ranked
      Male
    • Joined on 02-24-2011
    • Toronto, Canada
    • Posts 21

    Re: How to select 2 fields to name form with code?

    Hi - I just have fixed the issue. It was my xpath problem. It should be "/dfs:myFields/dfs:queryFields/q:top/@PrgYear". It's now working perfect. Thanks, Ash

  • 02-24-2011 02:25 PM In reply to

    Re: How to select 2 fields to name form with code?

    Glad you figured it out -- and I apologize for missing your post!

    Hilary Stoupa

Page 1 of 1 (6 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.