Extract Week Number from Date - InfoPath Dev
in

InfoPath Dev

Extract Week Number from Date

Last post 10-23-2006 11:35 AM by kurtd. 19 replies.
Page 1 of 2 (20 items) 1 2 Next >
Sort Posts: Previous Next
  • 10-09-2006 09:54 AM

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Extract Week Number from Date

    Hello,

    I have two fields that get submitted to a database. One is a date field with the date picker. The other is a normal text box for the week number. When someone picks a date in the date field, I would like the week number to appear in the other text box as the default value. How can I do this?
  • 10-09-2006 11:35 AM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Extract Week Number from Date

    Hi kurtd Welcome to the forums,

    By using on after change event on date field you can set the week number to another field. Depends on your setting date format (eg: dd/mm/yyyy) get the complete date value. By using substring functions you can get the ‘dd#%92 value in C#. By getting ‘dd#%92 value you need to calculate ((dd / 7) + 1) is week number of that date.

    After finding the week number of the date, set this value to another field.

    Otherwise you can try to keep the rule on that field but you should do above calculation on setting the value.

    Hope this Helps you.
  • 10-09-2006 01:12 PM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    I have two fields called dteDate and numWeek. I have the default value of numWeek set to @dteDate and I also have the check box for "Update this value when the result of the formula is recalculated" checked. Now when I select a date in the dteDate field it also inserts the date in the numWeek field. Now how do I format the result to display the week number instead of the date? I'm not sure where to insert the code you suggested. I'm not a programmer and just started using InfoPath. Thanks, Kurt
  • 10-10-2006 12:58 AM In reply to

    Re: Extract Week Number from Date

    Hi Kurtd,

    1. Double click on text filed and click on data validation button
    2. Go Event drop down filed and select ‘on after change#%92 and click on edit button
    3. Write the code.

    Hope this will help you.

  • 10-10-2006 06:37 AM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    Ok... How would I modify this to get the week number displayed instead of the date.

    function msoxd__tblTasks_numWeek_attr::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.

    if (eventObj.IsUndoRedo)
    {
    // An undo or redo operation has occurred and the DOM is read-only.
    return;
    }

    // A field change has occurred and the DOM is writable. Write code here to respond to the changes.

    }
  • 10-10-2006 12:53 PM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    I switched to vbscript and I have this so far. But I keep getting a run time error "object required: 'objDate' .

    If eventObj.Operation = "Insert" Then
    Dim objDate
    Dim objWeek

    Set objDate = XDocument.DOM.selectSingleNode("//my:myFields/my:dteDate")
    Set objWeek = XDocument.DOM.selectSingleNode("//my:myFields/my:txtWeek")


    objWeek.text = DatePart("ww", objDate.text)
    End If
  • 10-10-2006 11:13 PM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Extract Week Number from Date

    Hi kurtd,

    You can write below this code to in your on after change event.

    if (e.IsUndoRedo || e.Operation != "Insert")
    {
    return;
    }

    string dateString = e.Source.text;

    int date = Convert.ToInt32(dateString.Substring(8,2));

    int numberOfWeek = (date / 7) + 1;

    thisXDocument.DOM.selectSingleNode("/my:myFields/my:txtWeek").text = numberOfWeek.ToString();

    Note: Above code in C#.
  • 10-11-2006 06:00 AM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    How do I know if this line is correct? "//my:myFields/my:txtWeek" I do not think that is correct. I extraced the files and found this in the xml file. So what should my line read?

    <dfs:queryFields>
    <q:tblTasks taskID="" txtName="" dteDate="" txtWeek="" txtProject="" txtTask=""></q:tblTasks>
    </dfs:queryFields>
    <dfs:dataFields>
    <d:tblTasks taskID="" txtName="" dteDate="" txtWeek="" txtProject="" txtTask=""></d:tblTasks>
    </dfs:dataFields>
    </dfs:myFields>
  • 10-11-2006 06:18 AM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Extract Week Number from Date

    Hi Kurtd,

    You should give your xpath of where do you want to set that number of week value. In previously you had given one xpath like "//my:myFields/my:txtWeek". Because of I had used that xpath only here. But that is wrong for your requirement. Here is the change.

    I think you are using secondary DOM for setting number of week value. For that one you need to use below code.

    //Get the secondary DOM
    IXMLDOMDocument2 secondaryDOM = (IXMLDOMDocument2)thisXDocument.GetDOM(“Secondary DOM Name”);
    //Set the property of secondary DOM
    secondaryDOM.setProperty("SelectionNamespaces", “Name space of the Secondary DOM”);
    //Set the number of week value to specified path
    secondaryDOM.selectSingleNode(“XPath of your field”).text = numberOfWeek.ToString();

    Hope this Helps you.
  • 10-12-2006 06:30 AM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    How do I find the xpath of my field?
  • 10-12-2006 10:46 AM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Extract Week Number from Date

    Hi kurtd,

    Please check out the following link, you can get the more information on xpath:

    http://www.w3schools.com/xpath/default.asp
  • 10-12-2006 11:36 AM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    THis is my xml document so what should my xpath be?

    <?xml version="1.0" encoding="UTF-8"?>
    <?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:document:-dataFormSolution" href="manifest.xsf" solutionVersion="1.0.0.10" initialView="View 1" productVersion="11.0.8034" PIVersion="1.0.0.0" ?>
    <?mso-application progid="InfoPath.Document"?>
    <dfs:myFields my:nameDropdown="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:q="http://schemas.microsoft.com/office/infopath/2003/ado/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-10-09T13:35:05" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003">
    <dfs:queryFields>
    <q:tblTasks taskID="" txtName="" dteDate="" txtWeek="" txtProject="" txtTask=""></q:tblTasks>
    </dfs:queryFields>
    <dfs:dataFields>
    <d:tblTasks taskID="" txtName="" dteDate="" txtWeek="" txtProject="" txtTask=""></d:tblTasks>
    </dfs:dataFields>
    </dfs:myFields>
  • 10-12-2006 11:46 AM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Extract Week Number from Date

    Hi kurtd,

    Your xpath should be like,

    "/dfs:myFields/dfs:dataFields/d:tblTasks/@txtWeek"
  • 10-12-2006 01:57 PM In reply to

    • kurtd
    • Not Ranked
    • Joined on 10-09-2006
    • USA
    • Posts 13

    Re: Extract Week Number from Date

    Hello,

    I'm using a Repeating Table. The below vbscript code works great on the first item. When I select a date it fills in the week field with the week number. This only works on the first record. If I click insert record, the code does not work on that record and on. How do I modify the code so it works in my repeating table?


    If eventObj.Operation = "Insert" Then
    Dim objDate
    Dim objWeek

    Set objDate = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTasks/@dteDate")
    Set objWeek = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTasks/@txtWeek")

    objWeek.text = DatePart("ww", objDate.text)
    End If
  • 10-13-2006 09:26 PM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Extract Week Number from Date

    Hi kurtd,
    Use eventObj.Source.selectSingleNode("attributename")instead of
    thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTasks/@dteDate")

    Using eventObj.Source lets you access the fields in the current row where the event has occurred.

    One important point to note is that when you use eventObj.Source, you have to specify only the field name and not the full XPath of the field.
Page 1 of 2 (20 items) 1 2 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.