View Full Version : getdocumentbykey的问题
eslinan
02-08-06, 06:49 PM
视图中两个列,列值分别为aa域和bb域的值,执行的时候总是提示object variat not found,请教是何原因啊?
abc是第一列的一个值
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Dim key As Variant
key = "abc"
Set db = session.CurrentDatabase
Set view = db.GetView("gg")
Set doc = view.GetDocumentByKey(key,True)
Messagebox doc.GetItemValue("bb")(0)
End Sub
不知道你一共几个视图,但至少要有一个是"缺省视图".
aa是排序的么?升序或者降序
chen_hua790618
03-08-06, 10:35 AM
首先要对视图设定排序的关键字,然后使用GetDocumentByKey这个方法进行查找排序,最后取得文档句柄!
你的语句有错误应该这样写才对:
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim key As Variant
key = "abc"
Set db = session.CurrentDatabase
Set view = db.GetView("gg")
Set dc = view.GetDocumentByKey(key,True)
Set doc=dc.GetFirstDocument()
后面的我就不多说了
getdocumentbykey应该得到的只是一个文档,所以你的代码应该是在最后一句出的错,即doc为空(由于其他原因没有得到文档),所以在你messagebox的时候去doc的某个域值就会报错,所以加一句判断试试,应该就不会报错了,接下来你就该检查为什么getdocumentbykey没有获得文档了。。
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Dim key As Variant
key = "abc"
Set db = session.CurrentDatabase
Set view = db.GetView("gg")
Set doc = view.GetDocumentByKey(key,True)
If not doc is nothing then
Messagebox doc.bb(0)
end If
End Sub
其实你这个问题对初学写LotusScript的人 常碰到,这就要求我们书写代码的时候要规范:
object variat not found
看了你的代码有两种可能:
(1)试图没找到:Set view = db.GetView("gg")
你可以加以下代码判断:
Set view = db.GetView("gg")
if not view is nothing then
else
msgbox "找不到指定试图"
end if
(2):文档没有找到:Set doc = view.GetDocumentByKey(key,True)
可以以下代码判断:
Set doc = view.GetDocumentByKey(key,True)
if not doc is nothing then
else
msgbox "找不到文档"
end if
所以结合这两点,你的代码最好:
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Dim key As Variant
key = "abc"
Set db = session.CurrentDatabase
Set view = db.GetView("gg")
If Not view Is Nothing Then
Set doc = view.GetDocumentByKey(key,True)
If Not doc Is Nothing Then
Messagebox doc.GetItemValue("bb")(0)
Else
Msgbox "找不到文档"
Exit Sub
End If
Else
Msgbox "找不到指定的试图"
Exit Sub
End If
仔细看完上面各位大大的帖子后,发现自己犯鸟个严重错误....
楼主,你msgbox写错了啦,找到那个文档后,要显示bb的值,直接 msgbox doc.bb(0) 就好鸟......不用再GetItemValue鸟哇...淡然大前提是你找得到文档....嘎嘎.....
使用getdocumentbykey必须注意这两点:
一,关键字必须是在视图的第一列;
二,第一列是分类的,并且要升序。
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.