Type mismatch

I am editing a database done by a previous designer.

In one of the agents,
there is a sub function call BalanceK and when I try to run, it gives me error msg " Type mismatch" at this line ->old = profile.GetItemValue(fieldname & Cstr(6))(0)

Sub BalanceK(n As Integer)
      old = profile.GetItemValue(fieldname & Cstr(6))(0)
      newbal = Cdbl(old - totaldkotoshi(0))
      Call profile.ReplaceItemValue(fieldname & Cstr(6),Cdbl(newbal))
      Call profile.ReplaceItemValue(fieldname & Cstr(61),Cdbl(newbal))
End Sub

For fieldname, it is declare as Dim fieldname As String
and under Initialize,
leavetype = note.LeaveType(0)
fieldname = Left(leavetype,6)

I do not know where goes wrong.

Anyone can advise??
Thank you.
Who is Participating?
HemanthaKumarConnect With a Mentor Commented:
before converting.. make sure the value that is returned is not null.

u need to make sub into function and pass the value to fieldname.

try this:
Function tmptest(tmpfield As String)
      old = profile.GetItemValue(tmpfield & Cstr(6))(0)
      newbal = Cdbl(old - totaldkotoshi(0))
      Call profile.ReplaceItemValue(tmpfield & Cstr(6),Cdbl(newbal))
      Call profile.ReplaceItemValue(tmpfield & Cstr(61),Cdbl(newbal))
End Function

And in Initialize,
leavetype = note.LeaveType(0)
tmpfield = Left(leavetype,6)

and for calling fn.,
Call tmptest(tmpfield)
>     old = profile.GetItemValue(fieldname & Cstr(6))(0)
Try this instead
     old = profile.GetItemValue(fieldname & "6")(0)

Replace every line with cstr function similar to the one above

- My guess is that the Item <fieldname>6 doesn't exist, so when you deference the "value" using (0) on the end, it fails with a type mismatch.  I would set a NotesItem, check to be sure it's valid, then get its Value.

Dim it As NotesItem
Dim itVal As String

set it=profile.getFirstItem(fieldname+"6")
if(not it is nothing) then itVal=it.values(0) else itVal=""

or something like that.  You could shorten it by using

if(profile.hasItem(fieldname+"6") then itVal=profile.getItemValue(fieldname+"6")

but then you're computing the string twice, and accessing the document twice.  I don't know how that compares, performance-wise, to computing the string once and constructing a NotesItem.
- I would also HIGHLY recommend defining your data types instead of letting LotusScript default to Variant for everything.  I've had no end of quirky bugs with Variants over the years.
- I would also HIGHLY recommend getting rid of all the globals that appear to be in use.  They're going to bite you in the ... uh ... wrong end, in the long run.
-   Hope this helps...
kopibeanAuthor Commented:

another agent is also having the same problem - Type mismatch.
I debug lotusscript and error is at this line->
totalbal = Cdbl(profile.GetItemValue("Childa3")(0))

Under this AlreadyTaken agent, the code for Sub Initialize code ->

Sub Initialize
      Set s = New NotesSession
      Set db = s.CurrentDatabase
      Set collection1 = db.Search("Form = ""MP""",Nothing,0)
      For j = 1 To collection1.Count
            Set profile = collection1.GetNthDocument(j)
          ' Annual
            totalbal = Cdbl(profile.GetItemValue("Annual3")(0))
            balnow = Cdbl(profile.GetItemValue("Annual6")(0))
            future = Cdbl(profile.GetItemValue("Annual5")(0))
            taken = Cdbl(totalbal - balnow - future)          
            Call profile.ReplaceItemValue("Annual4",Cdbl(taken))
            Call profile.ReplaceItemValue("Annual41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Health3")(0))
            balnow = Cdbl(profile.GetItemValue("Health6")(0))
            future = Cdbl(profile.GetItemValue("Health5")(0))
            taken = Cdbl(totalbal - balnow - future)          
            Call profile.ReplaceItemValue("Health4",Cdbl(taken))
            Call profile.ReplaceItemValue("Health41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Replac3")(0))
            balnow = Cdbl(profile.GetItemValue("Replac6")(0))
            future = Cdbl(profile.GetItemValue("Replac5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Replac4",Cdbl(taken))
            Call profile.ReplaceItemValue("Replac41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Unpaid3")(0))
            balnow = Cdbl(profile.GetItemValue("Unpaid6")(0))
            future = Cdbl(profile.GetItemValue("Unpaid5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Unpaid4",Cdbl(taken))
            Call profile.ReplaceItemValue("Unpaid41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Reserv3")(0))
            balnow = Cdbl(profile.GetItemValue("Reserv6")(0))
            future = Cdbl(profile.GetItemValue("Reserv5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Reserv4",Cdbl(taken))
            Call profile.ReplaceItemValue("Reserv41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Medica3")(0))
            balnow = Cdbl(profile.GetItemValue("Medica6")(0))
            future = Cdbl(profile.GetItemValue("Medica5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Medica4",Cdbl(taken))
            Call profile.ReplaceItemValue("Medica41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Marria3")(0))
            balnow = Cdbl(profile.GetItemValue("Marria6")(0))
            future = Cdbl(profile.GetItemValue("Marria5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Marria4",Cdbl(taken))
            Call profile.ReplaceItemValue("Marria41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Compas3")(0))
            balnow = Cdbl(profile.GetItemValue("Compas6")(0))
            future = Cdbl(profile.GetItemValue("Compas5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Compas4",Cdbl(taken))
            Call profile.ReplaceItemValue("Compas41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Matern3")(0))
            balnow = Cdbl(profile.GetItemValue("Matern6")(0))
            future = Cdbl(profile.GetItemValue("Matern5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Matern4",Cdbl(taken))
            Call profile.ReplaceItemValue("Matern41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Patern3")(0))
            balnow = Cdbl(profile.GetItemValue("Patern6")(0))
            future = Cdbl(profile.GetItemValue("Patern5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Patern4",Cdbl(taken))
            Call profile.ReplaceItemValue("Patern41",Cdbl(taken))
            totalbal = Cdbl(profile.GetItemValue("Childa3")(0))
            balnow = Cdbl(profile.GetItemValue("Childa6")(0))
            future = Cdbl(profile.GetItemValue("Childa5")(0))
            taken = Cdbl(totalbal - balnow - future)    
            Call profile.ReplaceItemValue("Childa4",Cdbl(taken))
            Call profile.ReplaceItemValue("Childa41",Cdbl(taken))
            profile.Save True,True
End Sub

Please advise!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.