Creating an InfoPath Form


This article will discuss upon how Microsoft Office InfoPath Forms can be used. InfoPath forms can be used whenever there is a need of using custom template, such as Leave Application Form or Compensation Form etc.

Pre-requisites for InfoPath Form: 

  1. On the Server:
    1. Microsoft Office InfoPath 2007
  2. On the Client:
    1. Internet Browser 

Developing an InfoPath Form: 

To develop an InfoPath Form Template, follow the following steps:
  1. Open Microsoft Office InfoPath Form from Start-> All Programs-> Microsoft Office-> Microsoft Office InfoPath 2007.
     
  2. In the "Getting Started" dialog box that appears, choose "Design a Form Template".

    Getting Started.JPG
     
  3. In the next dialog box that appears, choose a Blank Form Template. Also make sure that "Enable browser-compatible features only" check box is checked and click OK.

    Form Template.JPG
     
  4. A blank form template screen will appear. In the right hand side of the template screen, you will see Design Tasks Pane, if it doesn't appear, go to View-> Design Tasks. And click on "Layout".

    Design Tasks- Layout.JPG
     
  5. In the next pane that appears, select the Table layout. For this example, I will choose a Two- Column Table.
     
  6. A table with one row and two columns will be added in the Design Pane. Adjust the height and width of the table. To add a new row, select the existing row and in the right side pane, click on "Add Table row".
     
  7. Add the text to Left Column of the table and Controls to right column of the table. To add controls, click on the Controls option in the main Design Tasks Pane.

    Design Tasks- Controls.JPG
     
  8. Finally, the Form will resemble as follows:

    Final Form.JPG
     
  9. To change the name of the controls, select the control, right click on the Green Button and go to the Properties of the Control.

    PropertiesMenu.JPG
     
  10. In the dialog box that follows, put the desired field name.

    Properties.JPG
  11. For Button Controls, select the Action as "Rules and Custom Code" and in Label, provide the desired name.

    Button Properties.JPG
     
  12. Before starting any code for the Form Template, choose the language you want to code in VB or C#.
     
  13. To do so, go to Tools->Form Options, in the Category, select Programming and set the desired language and path where you want to save your InfoPath project.

    Form Options- Programming.JPG
     
  14. Now save the form before starting any code.

    Note: To code the InfoPath Forms, you should have the "Microsoft Visual Studio Tools for Applications" options enabled. To do so, go to Control Panel->Add/Remove Programs->Microsoft Office Infopath 2007. Click on Change and enable the .NET Programmability Support.

    Activate VSTA.JPG
     
  15. Now to code on the Button Click event, in the Button Properties Dialog Box, click on "Edit Form Codeā€¦". A Visual studio project will open, with the Button Click Event. 
     
  16. Code it in the same way as you code for any other C# project. The Submit_Clicked Event will resemble the following code:

    public void Submit_Clicked(object sender, ClickedEventArgs e)
    {

        string
    _position = MainDataSource.CreateNavigator().SelectSingleNode                     
        ("/my:myFields/my:Position", NamespaceManager).Value;               
        string _description = MainDataSource.CreateNavigator).SelectSingleNode
        ("/my:myFields/my:Description", NamespaceManager).Value;
        string _category = MainDataSource.CreateNavigator().SelectSingleNode
        ("/my:myFields/my:Category", NamespaceManager).Value;

        string
    _location = MainDataSource.CreateNavigator().SelectSingleNode     
        ("/my:myFields/my:Location", NamespaceManager).Value;

       string
    _connstring;
        SqlConnection
    _oConnection;

          SqlCommand _oCommand = new SqlCommand();
          _connstring = @"DataSource=./SQLExpress;Initial  Catalog=My_DB;Integrated Security=SSPI";
          _oConnection = new SqlConnection(_connstring);
          _oCommand.Connection = _oConnection;
          _oCommand.CommandType = CommandType.Text;
          _oCommand.CommandText =
          "INSERT INTO tbl_Position (Position,Description,Category,Location) values
          ('"
    + _position + "','" + _description + "','" + _category + "','" + _location + "')";
         _oConnection.Open();
         _oCommand.ExecuteNonQuery();
         _oConnection.Close();           
    }
     
  17. The first parameter of the SelectSingleNode is an XPath value. To get the XPath value of a control, select "Data Source" from Design Tasks Pane.

    Design Tasks- DataSource.JPG
     
  18. In the next pane that appears, right click on the name of the control, select "Copy XPath", and paste it in the Visual Studio Code.

    XPath.JPG
     
  19. Build the project, when the status bar shows "Build Succeeded", close Visual Studio.
In my next article I will discuss on how can we publish an InfoPath Form to a SharePoint Server 2007.

Summary

In this article we learnt how we can create an InfoPath Form.  


Similar Articles