Hi,
Here are some sample steps I created to get a number from a SQL Server
table:
- Create the following table:
Name: tblMyID
Field: MyID, int, Primary Key (NOT an Identity Field!)
- Create the following Stored Procedure:
CREATE PROCEDURE dbo.mysp_NewID
AS UPDATE dbo.tblMyID
SET MyID = MyID + 1
- Create the InfoPath form:
- Add a field on the form named: txtNew with Conditional Formatting to hide
the field
- Add another field named: field1
- Add a Secondary Data Connection to the table noted above named: NewID
- On the OnLoad event, use the following code:
Dim CN
If Xdocument.IsNew Then
Set CN = CreateObject("ADODB.Connection")
With CN
.Provider = "SQLOLEDB"
.ConnectionString = "Data Source=<your SQL Server>;Initial
Catalog=<Your Database>;Integrated Security=SSPI"
.Open
.Execute "mysp_NewID"
End With
XDocument.DataAdapters("NewID").Query
End If
This simply checks to see if the document is "New" and if so, creates a
connection to the database and executes the stored procedure to update the
number.
- Lastly, add "Rule" on the Open of the document (Tools|Form Options|Open
and Save tab|Rules) that checks to see if the hidden field (txtNew) is
blank. If it is, there is an action to set "field1" to the value returned
by the secondary data connection and another action to set some generic
text in the txtNew field so it is no longer blank.
So basically there are 2 pieces to this solution: if the document is New,
the code kicks in to execute the stored procedure. Because the document is
New, the hidden field "txtNew" is blank so the Rule kicks in to set the
value.
Obviously you could create the entire process in code but I wanted to keep
the custom code to a minimum.
I hope this helps!
Scott L. Heim
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.