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 / August 2005

Tip: Looking for answers? Try searching our database.

Creating a Unique Key with VBScript

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ridawg - 18 Aug 2005 18:31 GMT
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!
Signature

-ridawg

Scott L. Heim [MSFT] - 19 Aug 2005 14:50 GMT
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
ridawg - 19 Aug 2005 19:45 GMT
Scott,

Thanks for the help!! Works great!
Signature

-ridawg

> Hi,
>
[quoted text clipped - 124 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights
 
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.