Question

I am New to infopath form. I have created a infopath form like below

My Columns in the form are below

START DATE :

Happens Every: *textbox* drop down Weeks (Mon,thue,---sun)

Due Date :

Now when the user selects Start date as 7/2/2013, In the text box if he gives 3 and from drop down he selects Thuesday.

Now in Due Date it should display Next week Thursday date i.e (7/9/2013).

Similarly for Months. Can you please help me with a fromule in infopath form

url of my form( http://i.stack.imgur.com/PJAze.png)

Thanks, Sandy


Dave ,My Web method Looks like this I got stuck in Assigning Parameters to it.Not sure how do i assign parameters(EnteredDate,Number,DayofWeek) in info path form. If you can help that will be great.

 [WebMethod]

    public String GetNthWeekdayOfMonth(DateTime EnteredDate, int Number, DayOfWeek weekday)
    {
        String error = "";

        var days = Enumerable.Range(1, DateTime.DaysInMonth(EnteredDate.Year, EnteredDate.Month)).Select(day => new DateTime(EnteredDate.Year, EnteredDate.Month, day));

        var weekdays = from day in days
                       where day.DayOfWeek == weekday
                       orderby day.Day ascending
                       select day;

        int index = Number - 1;

        if (index >= 0 && index < weekdays.Count())
            return Convert.ToString(weekdays.ElementAt(index));

        else

            error = "The specified day does not exist in this ";

        return error;
    }
Était-ce utile?

La solution

I believe what you want to achieve would actually be quite difficult even for an advanced InfoPath user even with code it is not trivial.

You have a few options:

  1. Someone spends a long time writing extremely complex XPath for you. You don't really understand how it works and if you ever need to change anything you will be stuck.

  2. Someone spends less time writing more readable code you still don't understand but it would be easier to learn and change later.

  3. You learn some code or XPath and come back to the problem later when you have more understanding.

  4. You re-evaluate the need for this functionality and decide wether or not it is really worthwhile or if there is an alternative or better way to accomplish it.

Now as to an answer:

If you have any familiarity with code you know it is possible to add code behind to influence fields on the form. If I were you I would learn as much of this as you need in this scenario for now and continue to improve later.

You could do as follows:

  1. Add code behind Tools > Form Options > Programming > Choose "C#" from drop down

  2. Hook your code behind to the changed event of all three inputs (startDate, dayOfWeek, and nth value). Right Click > Programming > Changed Event

Each of these blocks can call the same code. e.g.

public void StartDate_Changed(object sender, XmlEventArgs e)
{
    ChangeDueDate();
}

And the ChangeDueDate code would be as follows:

private void ChangeDueDate()
{
    DateTime StartDate = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:StartDate", NamespaceManager).ValueAsDateTime;
    int DayOfWeek = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:DayOfWeek", NamespaceManager).ValueAsInt;
    int n = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:n", NamespaceManager).ValueAsInt;

    // If StarDate.DayOfWeek is not the same as DayOfWeek 
    // then add the amount of days necessary.
    StartDate = StartDate.AddDays((7 - (int)StartDate.DayOfWeek + DayOfWeek) % 7);
    // Add weeks until you reach the nth value.
    while (((StartDate.Day - 1) / 7) != n - 1)
        StartDate = StartDate.AddDays(7);
    // Finally set the DueDate field to the calculated date.
    MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:DueDate", NamespaceManager).InnerXml = StartDate.ToString("dddd dd MMMM yyyy");
}

I have tested this and it seems to work well. However there will still be one problem that you currently have no way to know if there are actually 4 or 5 of each day per month?

Anyway try the above and go from there if you still need help ask some more questions.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top