Hi Jeff,
Here are sample steps to get this working using the Orders table from the
SQL Server Northwind sample database:
- Create a new InfoPath solution using the Orders table from the SQL Server
Northwind database (for this sample, you only need the fields: OrderID,
CustomerID, EmployeeID, OrderDate, ShipVia & Freight)
- From the Tools menu choose Form Options, select the Advanced Tab and be
sure the Form Code Language is set to: VBScript
- Display the Data Source Task Pane - you should see both queryFields and
dataFields
- Right-click on "myFields" and choose Add
- In the Name field enter: txtStartDate and click OK
- Right-click on "myFields" and choose Add
- In the Name field enter: txtEndDate and click OK
- Drag the txtStartDate and txtEndDate fields to the query section of your
View
- Expand dataFields in the Data Source Task Pane, drag Orders to the data
section of your View and choose Repeating Section with Controls
- Right-click on the Run Query button and choose Properties
- Change the Action to: Rules and Custom Code
- Click the Edit Form Code button - you should then see the following:
Sub CTRL2_8_OnClick(eventObj)
' Write your code here
End Sub
- Immediately below the "Write Your Code Here" line, add the following:
'Store the original command text of the query adapter
Dim strOriginalCommand
strOriginalCommand = XDocument.QueryAdapter.Command
'Get the query node that you want to modify.
Dim queryOrders
Set queryOrders =
XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:queryFields/q:Orders")
'Get the start and end dates entered by the user
Dim objStart
Set objStart =
XDocument.DOM.selectSingleNode("//dfs:myFields/my:txtStartDate")
Dim objEnd
Set objEnd = XDocument.DOM.selectSingleNode("//dfs:myFields/my:txtEndDate")
'Build the new SQL command
Dim strSQL
strSQL = strOriginalCommand & " WHERE OrderDate >= '" & objStart.text & "'
And OrderDate <= '" & objEnd.text & "'"
'Set the query adapter command to the new SQL text
XDocument.QueryAdapter.Command = strSQL
'Execute the query
XDocument.Query
'Reset the query adapter back to the original command so it is ready for
the next query
XDocument.QueryAdapter.Command = strOriginalCommand
- From the File menu choose Save
- From the File menu choose Exit - you should be back to your InfoPath form
in Design View
- Preview the form
- In the txtStartDate box, enter: 7/1/1996
- In the txtEndDate box, enter: 7/31/1996
- Click the Rules and Custom Code button - result: you should only have
orders returned where the OrderDate is between 7/1/1996 and 7/31/1996!
Now as you can see, the code above is needed to make this work. This sample
only uses one table - when you begin to introduce additional tables (i.e.
Customers so you can show customer information) this gets significantly
more difficult because of the way the SQL command is built by InfoPath. You
get into needing to "parse" that command text so you insert the "where"
condition at the appropriate location.
So, if you only have one table you are using it's not too difficult to
query on a date range; however, if that is not the case you may want to
think about whether this condition is truly necessary.
I hope this helps!
Scott L. Heim
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.