ReplaceItemvalue - type mismatch

I’m just new to this and I have created a button with this code to change the value of an item, but when try and run it I get an error saying"Type mismatch"

Does anyone know why this is?

Thanks in advance.

Sub Click(Source As Button)

Dim s As NotesSession

Dim db As NotesDatabase

Dim v As NotesView

Dim Doc As NotesDocument



Set s = NewNotesSession

Set db = s.Currentdatabase

Set v = db.getView ("student")

Set doc = v.getDocumentByKey("Michael")



Call doc.ReplaceItemvalue("Course", "Advanced")

Call doc.Save (False,True)

End Sub

Subject: ReplaceItemvalue - type mismatch

It’s a simple enough script.I assume the statement Set s = NewNotesSession is a typo.

It should, of course, be Set s = New NotesSession

Could it be that the item you are trying to update - Course - is not a Text item but perhaps numeric?

I would recommend you always use an error handler, like this:

Sub Click(Source As Button)

Dim s As NotesSession

Dim db As NotesDatabase

Dim v As NotesView

Dim Doc As NotesDocument

Dim strMsg As String

On Error GoTo errHandler

Set s = NewNotesSession

Set db = s.Currentdatabase

Set v = db.getView (“student”)

Set doc = v.getDocumentByKey(“Michael”, True)

Call doc.ReplaceItemvalue(“Course”, “Advanced”)

Call doc.Save (False,True)

bailOut:

Exit Sub

errHandler:

strMsg = “Error in the code.” & chr(10)

strMsg = strMsg & "Error code: " & Cstr(Err()) & chr(10)

strMsg = strMsg & "Error text: " & Error$ & chr(10)

strMsg = strMsg & "Error line: " & Cstr(Erl())

MessageBox strMsg, 16, “Error”

Resume bailOut

End Sub

Notice that I added the ‘True’ argument to the view lookup.

You need that to make sure you get a unique document.

Hope this helps.

// Ken Haggman, http://www.NotesHound.com

Subject: RE: ReplaceItemvalue - type mismatch

“Could it be that the item you are trying to update - Course - is not a Text item but perhaps numeric?”

After this line

Call doc.ReplaceItemvalue(“Course”, “Advanced”)

it WOULD be a text item anyway. That would not cause an error.

There’s nothing wrong with the code itself (apart from the NewNotesSession thing), so adding error handling to catch run time errors is a good idea indeed. But before doing so, it is definitely worth checking, that you use a strict compile time error checking. Since Notes 6 you can configure Domino Designer to automatically insert

Option Declare in the Options section of each LotusScript module. The setting is in the design pane properties and should be enabled by default, but if someone else has been setting up your environment you might still want to check it. Using Option Declare is highly recommended, as it will force you to declare all of your variables prior to using them, thus greatly reducing the risk of using inappropriate variable types.

Furthermore, when adding error handling, simply reporting an error is just the first basic step. While the message displayed might help you during development, they will not make your users happy. Errors could be caused by undiscovered programming errors, but they might also just be expected: What if there is no document that displays exactly the value “Michael” in the first sorted column of the students view? Not too unlikely, and it wouldn’t make much sense to tell the user whilch line of your code caused what error.

The finer art of error trapping includes foreseeing possible runtime errors, judging, if they can be ignored or circumvented, require a message the user, a mail to an administrator or even reverting changes you did in your code before the error occurred.

To simply find out what actually caused the error in your case, use the build in LotusScript debugger (that can be enabled from the File → Tools menu). In most cases you will switch it to the variables view and then walk through your code line by line. At some point, one variable will not be assigned its expected value and that’s what will get you on the right track.