Re: Maximum Coding Problem
Probably because you are comparing strings, that's my guess.
String Compare
10 compare to 8
8 is larger than 1 so it ecomes max.
Try using numeric fields or atleast converting the fields to numeric.
Re: Maximum Coding Problem
It solved my problem already! Thanks a lot!!
But I juz wonder, why sometimes it does work..
Re: Maximum Coding Problem
As randem suggested you could try coercing them into numeric type...
VB Code:
Do While Not DataSISPEDA.Recordset.EOF
If Val(max) <= Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value)) Then
Val(max) = Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value))
DataSISPEDA.Recordset.MoveNext
Else
DataSISPEDA.Recordset.MoveNext
End If
Loop
BTW, instead of Trim which returns a variant type try using Trim$ to return a string, it's faster that way...
Re: Maximum Coding Problem
ohh...thanks for that info!
But, am I need to declare variable max as string or integer? I have problem if declare it as integer bcoz my data exceed the limit of integer allowed.
Re: Maximum Coding Problem
You could use a LONG datatype
Re: Maximum Coding Problem
i have tried this way . but I got compile error sound:
" Function call on left hand side of assignment must return a Variant or Object "
It highligthed the error on the second max statement.
VB Code:
Do While Not DataSISPEDA.Recordset.EOF
If Val(max) <= Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value)) Then
'got error here
Val(max) = Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value))
DataSISPEDA.Recordset.MoveNext
Else
DataSISPEDA.Recordset.MoveNext
End If
Loop
Re: Maximum Coding Problem
You don't need the VAL() function on the left side of the equation, and you are assigning the value to it. Just use Max =
Re: Maximum Coding Problem
You can do that...
VB Code:
max = Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value))
is what you need.
Re: Maximum Coding Problem
Does DataSISPEDA.Recordset.Fields("DocID").Value only contain whole numbers? If it does, the most sensible thing to do would be convert the field in the database to a Long datatype.
That way you avoid all the overhead of using Trim (which is very slow) and Val which actually returns a Double.
Then you would declare max as a Long and your code would look like
VB Code:
Do While Not DataSISPEDA.Recordset.EOF
If max <= DataSISPEDA.Recordset.Fields("DocID").Value Then
max = DataSISPEDA.Recordset.Fields("DocID").Value
DataSISPEDA.Recordset.MoveNext
Else
DataSISPEDA.Recordset.MoveNext
End If
Loop
Re: Maximum Coding Problem
I solved the problem already! I did it as said as Dglienna and Randem adviced me to!
Thanks a lot guyz. I declared max as long datatype and declared DataSISPEDA.Recordset.Fields("DocID") as varchar datatype. I dont see any long datatype in database.
Re: Maximum Coding Problem
It seems like you are using SQL Server and and the Integer type is a long in that database. So just change the datatype of the field to Integer. Then you won't have to use the Val statement at all.
Re: Maximum Coding Problem
In SQL, long would be called Int (i think)
Something like that.
Re: Maximum Coding Problem
randem, i do used SQL Server. I cannot declare it as integer coz integer only allow maximum 4 character but mine used more than that. Thats why im choosing varchar instead of char or integer.
Re: Maximum Coding Problem
LadyAziera,
You are not supposed to be using charaters at all you are supposed to be using numbers. You are confusing Bytes with characters. A long is four bytes in length and can hold a very large number.
Re: Maximum Coding Problem
Ohh...Yeah, I got confused between characters and Bytes. I thought the length of Integer datatype is count as same as Characters as well.. Thanks to you for make me realize on it! Now, I dont have to use Val anymore! :)