Creating a Unique Key with VBScript

R

ridawg

I would like to create a unique key like the one in the asset template but I
would like to do it using VBScript. I'm not familiar at all with JScript so
I'm not sure how to translate the code I see in the asset template to
VBScript.

Thanks!
 
S

Scott L. Heim [MSFT]

Hi,

Here is the basic code from that sample converted to VBScript; however,
this does not test for duplicates if this is used elsewhere in your
solution.

** NOTE: Some of the information (i.e. the XPATH to groups, fields, etc. is
specific to this sample! **

- Create a new, blank InfoPath form
- Add a repeating table with 2 columns
- From the Tools menu choose Programming and then select On Load event -
you should now see the following:

XDocument.DOM.setProperty "SelectionNamespaces",
"xmlns:my=""http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-08-
19T13:31:30"""
'</namespacesDefinition>


'=======
' The following function handler is created by Microsoft Office InfoPath.
' Do not modify the name of the function, or the name and number of
arguments.
'=======
Sub XDocument_OnLoad(eventObj)
' Write your code here
End Sub

- Add the following line of code between the "namespaces" declaration and
the comment for the OnLoad event:

Dim g_iCounter

- Your code should now appear as follows:

XDocument.DOM.setProperty "SelectionNamespaces",
"xmlns:my=""http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-08-
19T13:31:30"""
'</namespacesDefinition>

Dim g_iCounter

'=======
' The following function handler is created by Microsoft Office InfoPath.
' Do not modify the name of the function, or the name and number of
arguments.
'=======
Sub XDocument_OnLoad(eventObj)
' Write your code here
End Sub

- Add the following code to the OnLoad event - immediately before the "End
Sub" line:

Dim xmlKeys
Dim xmlKey
Dim i

If not Xdocument.IsNew Then
Set xmlKeys =
XDocument.DOM.SelectNodes("//my:myFields/my:group1/my:group2")

g_iCounter = xmlKeys.length

for i = 0 to xmlKeys.length - 1
Set xmlKey = xmlKeys(i)
If xmlKey.selectSingleNode("my:field2").text = "" Then
createUniqueKey xmlKey
End if
next
End If

- Immediately after the "End Sub" line for the OnLoad event, add the
following code:

Function createUniqueKey(xmlKey)
'Avoid side effects when DOM is read only.
If XDocument.IsDOMReadOnly Then
Exit Function
Else
Dim strUniqueKey
g_iCounter = g_iCounter + 1
strUniqueKey = "key" & g_iCounter & "-" & Date
createUniqueKey = strUniqueKey
End if
End Function

- Display the Data Source Task Pane on your InfoPath form
- Right-click on "group2" and choose Properties (this assumes you did not
rename anything in this sample and you simply placed a Repeating Table
control on the form as per the steps)
- Select the Validation and Event Handlers tab
- From the Events box choose OnAfterChange and click the Edit button - your
code should appear like this:

Sub msoxd_my_group2_OnAfterChange(eventObj)

' Write code here to restore the global state.

If eventObj.IsUndoRedo Then
' An undo or redo operation has occurred and the DOM is read-only.
Exit Sub
End If

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

End Sub

- Immediately before the "End Sub" line, add the following code:

If eventObj.Operation = "Insert" Then
Dim objField2
Set objField2 = eventObj.Site.SelectSingleNode("my:field2")
If objField2.text = "" Then
objField2.text = CreateUniqueKey(objField2)
End if
End If

- Close the code window, Preview your form and test by adding records to
the table - you should see "Field2" get a unique value each time!

I hope this helps!!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top