Are you running this in the debugger? If so, are you sure that the vendor code does exist in the view, and that the first column in the view is sorted by the vendor code?
just to confirm, after your set doc2 =....
you may want to put in
if doc2 is nothing then
print "could not find document"