|
-
Nov 24th, 2005, 09:36 PM
#1
Thread Starter
Member
Maximum Coding Problem
Hello Guyzz..
I have a little problem here..I know this might be very easy to some people but I dont know why this become problem to me!
This is a codes to find a maximum number. But I dont know why when it trace a comparison between single digit number and 2 digit number, the data not accurate anymore. Example:
initial max = 0
DocID = 4, 8 , 10 , 12
PROBLEM: when max = 8 , compare to DocID = 10, the max value still remains as 8!
VB Code:
Do While Not DataSISPEDA.Recordset.EOF
If max <= Trim(DataSISPEDA.Recordset.Fields("DocID").Value) Then
max = Trim(DataSISPEDA.Recordset.Fields("DocID").Value)
DataSISPEDA.Recordset.MoveNext
Else
DataSISPEDA.Recordset.MoveNext
End If
Loop
I cannot find any error here..Plz anyone help me on this..
-
Nov 24th, 2005, 09:51 PM
#2
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.
-
Nov 24th, 2005, 10:33 PM
#3
Thread Starter
Member
Re: Maximum Coding Problem
It solved my problem already! Thanks a lot!!
But I juz wonder, why sometimes it does work..
-
Nov 24th, 2005, 11:09 PM
#4
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...
-
Nov 25th, 2005, 12:33 AM
#5
Thread Starter
Member
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.
-
Nov 25th, 2005, 12:34 AM
#6
Re: Maximum Coding Problem
You could use a LONG datatype
-
Nov 25th, 2005, 01:02 AM
#7
Thread Starter
Member
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
-
Nov 25th, 2005, 01:05 AM
#8
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 =
-
Nov 25th, 2005, 01:05 AM
#9
Re: Maximum Coding Problem
You can do that...
VB Code:
max = Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value))
is what you need.
-
Nov 25th, 2005, 01:26 AM
#10
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
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Nov 25th, 2005, 02:20 AM
#11
Thread Starter
Member
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.
-
Nov 25th, 2005, 02:24 AM
#12
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.
-
Nov 25th, 2005, 02:25 AM
#13
Fanatic Member
Re: Maximum Coding Problem
In SQL, long would be called Int (i think)
Something like that.
r0ach™
Don't forget to rate the post
-
Nov 25th, 2005, 02:28 AM
#14
Thread Starter
Member
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.
-
Nov 25th, 2005, 02:40 AM
#15
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.
Last edited by randem; Nov 25th, 2005 at 02:53 AM.
-
Nov 25th, 2005, 02:50 AM
#16
Thread Starter
Member
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|