Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / General MS InfoPath Questions / November 2005

Tip: Looking for answers? Try searching our database.

SQL datetime date range query from datepickers --sample code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RickH - 02 Nov 2005 07:25 GMT
Date Range from DateTime SQL column in VB.NET, converted from Joel
Alley's original C#
Not cleaned up, but works.
Might have minor errors since I didn't run exhaustive testing on it.
Painful finding all the gotchas. The 'order by' bug took me a while to
figure out --took pasting in Enterprise Manager to spot the problem.

<InfoPathEventHandler(MatchPath:="QueryToEdit",
EventType:=InfoPathEventType.OnClick)> _
       Public Sub QueryToEdit_OnClick(ByVal e As DocActionEvent)
        'thisXDocument.Query()
        ' thisXDocument.View.SwitchView("Data")
        Dim oldCommand As String
        Dim whereClause As String
        Dim queryFieldNode As IXMLDOMNode
        Dim curQueryFieldAttribute As IXMLDOMNode
        Dim queryFieldAttributes As IXMLDOMNamedNodeMap
        Dim adapter As ADOAdapter
        adapter = thisXDocument.QueryAdapter

        queryFieldNode =
thisXDocument.DOM.selectSingleNode("dfs:myFields/dfs:queryFields/q:Fuel_Received")

        'The QueryFields are empty.
        If (whereClause = Nothing) Then whereClause = String.Empty
        'save orig
        oldCommand = adapter.Command

        'modify query
        Dim myDateFields As IXMLDOMNode =
thisXDocument.DOM.selectSingleNode("/dfs:myFields")
        Dim strStartDate =
myDateFields.selectSingleNode("/dfs:myFields/my:StartDate").text
        Dim strEndDate =
myDateFields.selectSingleNode("/dfs:myFields/my:EndDate").text
        Dim strSQLQuery, strReport_Date As String
        Dim strOrderBy As String
        Dim ModOldCommand As String = oldCommand

        If strStartDate <> "" And strEndDate <> "" Then
           If whereClause = String.Empty Then
              whereClause = " WHERE (Report_Date >= '" &
CStr(CDate(strStartDate)) & " 00:00:00 AM') AND (Report_Date <= '" &
CStr(CDate(strEndDate)) & " 12:59:59 PM')"
              'Need to remove Order by section
              Dim intBadStart As Integer
              If ModOldCommand.IndexOf("order by") <> -1 Then
                 intBadStart = ModOldCommand.IndexOf("order by")
                 Dim intBadEnd As Integer = ModOldCommand.Length
                 Dim intBadLength As Integer = (intBadEnd -
intBadStart)
                 strOrderBy = ModOldCommand.Substring(intBadStart,
intBadLength)
                 ModOldCommand = ModOldCommand.Remove(intBadStart,
intBadLength)
              End If
           End If
        End If
        If (whereClause <> "") Then
           adapter.Command = ModOldCommand & whereClause & " " &
strOrderBy
           System.Windows.Forms.Clipboard.SetDataObject(ModOldCommand
& whereClause, 1)
           ' Clear the QueryFields so the WHERE clause isn't
           ' automatically generated.
           ' If you do tho, you can't select other query fields!!!
           queryFieldAttributes = queryFieldNode.attributes
           curQueryFieldAttribute = queryFieldAttributes.nextNode()
           While Not (curQueryFieldAttribute Is Nothing)
              'curQueryFieldAttribute.text = ""    'prevents other
fields if uncommented
              curQueryFieldAttribute = queryFieldAttributes.nextNode()
           End While
        End If
        ' Perform the query.
        Try
           thisXDocument.Query()
        Catch ex As Exception
           thisXDocument.UI.Alert("Failed to query.\n\n" + ex.Message)
        End Try
        ' Reset the command so that subsequent queries are based on
        ' the correct SQL command text string.
        adapter.Command = oldCommand
        thisXDocument.View.SwitchView("Data")
       End Sub
Franck Dauché - 02 Nov 2005 19:25 GMT
Hi Rick,

Thanks for taking the time to share with the community!

Franck Dauché

> Date Range from DateTime SQL column in VB.NET, converted from Joel
> Alley's original C#
[quoted text clipped - 81 lines]
>          thisXDocument.View.SwitchView("Data")
>         End Sub
RickH - 02 Nov 2005 21:39 GMT
No problem, been looking for how to do that since InfoPath shipped.
And I really hate jscript samples, and C# is nice for programmers, but
not for less technical users.

CORRECTION: (records on end date not showing up)
whereClause = " WHERE (Report_Date >= '" &
CStr(CDate(strStartDate)) & " 00:00:00 AM') AND (Report_Date <= '" &
CStr(CDate(strEndDate)) & " 12:59:59 PM')"

SHOULD BE: (24hr time needed)
whereClause = " WHERE (Report_Date >= '" &
CStr(CDate(strStartDate)) & " 00:00:00') AND (Report_Date <= '" &
CStr(CDate(strEndDate)) & " 23:59:59')"
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.