2012-02-02 36 views
0

有人可以提供一些幫助嗎?嘗試使用userName變量作爲關鍵字搜索視圖時,出現'無效鍵值類型'錯誤。LotusScript:無效的鍵值類型

這是因爲它是NotesName類型嗎?有什麼辦法可以解決這個問題嗎?任何幫助讚賞。謝謝!

%REM 
Sub aiv_checkPersonIsValid 
Description: 
Checks that the person(s) that have been entered into the location database are valid 
members of the CCL Address Book 

Function calls: N/A 
Sub calls: N/A 
Called in event: onChange (to populate fields) 
Called in action: Save and Close, Save, Save And Add Another 
Called by: co_countValuesAndPopulate() 

%END REM 
Public Function aiv_checkPersonIsValid (userName As NotesName) As Boolean 

Dim s As New NotesSession 
Dim w As New NotesUIWorkspace 
Dim peopleDb As NotesDatabase 
Dim peopleView As NotesView 
Dim peopleDoc As NotesDocument 
Dim thisDoc As NotesDocument 
Dim uidoc As NotesUIDocument 
Dim key(0) As Variant 
Dim noMatchFound As String 
Let noMatchFound = "No match found for this name. Please choose a name from the  menu provided." 
Dim PersonNameField As NotesItem   
'Dim userName As NotesDocument 
Set uidoc = w.CurrentDocument 
Set thisDoc = uidoc.Document   

    'get handle to People database and check we've found the database 
    Set peopleDb=s.GetDatabase("****", "ccl\development\LocSysPeople.nsf") 
    If Not peopleDb Is Nothing Then 

      'get handle to view in People database and check we've found the view 
      Set peopleView = peopleDb.GetView("All\LocSysLookup") 
      If Not peopleView Is Nothing Then 

       'Make the PersonNameField variable equal to the value of the first item in the Person Name Field 
       Set PersonNameField = thisDoc.GetFirstItem("PersonName") 

     ForAll pName In PersonNameField.Values 
      'Start to loop through the PersonNameField and check that the names chosen exist in the people database 
      'lookup record in People database which matches the name in the PersonName field on this document 

      Set userName = New NotesName(pName) 

      Set key(0) = userName 

      'Set peopleDoc = peopleView.GetDocumentByKey(uidoc.Document.GetItemValue("PersonName")(0), True) 
      Set peopleDoc = peopleView.GetDocumentByKey(key, True) 

        'If there is no match found to the document in the peopleDb, show a MsgBox 
        If peopleDoc Is Nothing Then 
           MsgBox "No match found in the CCL People Database for '" + pName.Common + "'. Please choose a valid name.", 0, "Error: No match found" 
        aiv_checkPersonIsValid=False 
        Exit Function 
        End If 

     'End Loop 
     End ForAll 

    End If 
End If 

End Function 
+0

您在調試時會得到什麼值。 – 2012-02-02 16:34:02

回答

3

你似乎使用用戶名變量多次爲不同的目的,一旦在功能定義,然後在後面的代碼改變類型。該代碼看起來像是從不同的函數中剝離出來的,並且因爲傳入的值不是您在代碼後面比較的那個值,所以也沒有設置函數的返回值,這是另一個錯誤

下面是我將如何編寫該功能

Public Function aiv_checkPersonIsValid As Boolean 

    aiv_checkPersonIsValid=False 

    Dim s As New NotesSession 

    Dim w As New NotesUIWorkspace 

    Dim uidoc As NotesUIDocument 
    Set uidoc = w.CurrentDocument 

    Dim thisDoc As NotesDocument 
    Set thisDoc = uidoc.Document 

    Dim userName As NotesName 
    Dim peopleDoc As NotesDocument 
    Dim PersonNameField As NotesItem   

    'get handle to People database and check we've found the database 
    Dim peopleDb As NotesDatabase 
    Set peopleDb=s.GetDatabase("****", "ccl\development\LocSysPeople.nsf") 
    If Not peopleDb Is Nothing Then 

     'get handle to view in People database and check we've found the view 
     Dim peopleView As NotesView 
     Set peopleView = peopleDb.GetView("All\LocSysLookup") 
     If Not peopleView Is Nothing Then 

      'Make the PersonNameField variable equal to the value of the first item in the Person Name Field 
      Set PersonNameField = thisDoc.GetFirstItem("PersonName") 

      ForAll pName In PersonNameField.Values 
       'Start to loop through the PersonNameField and check that the names chosen exist in the people database 
       'lookup record in People database which matches the name in the PersonName field on this document 

       Set userName = New NotesName(pName) 
       Set peopleDoc = peopleView.GetDocumentByKey(userName.Abbreviated, True) 

       'If there is no match found to the document in the peopleDb, show a MsgBox 
       If peopleDoc Is Nothing Then 
        MsgBox "No match found in the CCL People Database for '" + pName.Common + "'. Please choose a valid name.", 0, "Error: No match found" 
        Exit Function 
       End If 
      End ForAll 
      aiv_checkPersonIsValid = true 
     End If 
    End If 

End Function 
+2

Declan的代碼看起來不錯。 (沒有意外!:-)) 對特定問題的簡短回答: 1.這是因爲它是NotesName類型嗎?是。 2.有什麼辦法可以解決這個問題嗎?使用NotesName.Abbreviated方法。 這假定All \ LocSysLookup視圖在第一個排序列中使用縮寫名稱格式。如果它使用規範格式,則使用NotesName.Canonical。如果它只使用通用名稱,則使用NotesName.Common。 – 2012-02-02 16:48:32

+0

傳奇。多謝你們。 – Thomas 2012-02-03 14:54:06