Results 1 to 16 of 16

Thread: Maximum Coding Problem

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    Arrow 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:
    1. Do While Not DataSISPEDA.Recordset.EOF
    2.        
    3.             If max <= Trim(DataSISPEDA.Recordset.Fields("DocID").Value) Then
    4.                
    5.                 max = Trim(DataSISPEDA.Recordset.Fields("DocID").Value)
    6.                 DataSISPEDA.Recordset.MoveNext
    7.            
    8.             Else
    9.                
    10.                 DataSISPEDA.Recordset.MoveNext
    11.            
    12.            End If
    13.        
    14.     Loop

    I cannot find any error here..Plz anyone help me on this..

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    Re: Maximum Coding Problem

    It solved my problem already! Thanks a lot!!

    But I juz wonder, why sometimes it does work..

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Maximum Coding Problem

    As randem suggested you could try coercing them into numeric type...

    VB Code:
    1. Do While Not DataSISPEDA.Recordset.EOF
    2.        
    3.             If Val(max) <= Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value)) Then
    4.                
    5.                 Val(max) = Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value))
    6.                 DataSISPEDA.Recordset.MoveNext
    7.            
    8.             Else
    9.                
    10.                 DataSISPEDA.Recordset.MoveNext
    11.            
    12.            End If
    13.        
    14.     Loop

    BTW, instead of Trim which returns a variant type try using Trim$ to return a string, it's faster that way...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    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.

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Maximum Coding Problem

    You could use a LONG datatype

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    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:
    1. Do While Not DataSISPEDA.Recordset.EOF
    2.        
    3.             If Val(max) <= Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value)) Then
    4.                
    5.                 'got error here
    6.                 Val(max) = Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value))
    7.                 DataSISPEDA.Recordset.MoveNext
    8.            
    9.             Else
    10.                
    11.                 DataSISPEDA.Recordset.MoveNext
    12.            
    13.            End If
    14.        
    15.     Loop

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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 =

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Maximum Coding Problem

    You can do that...
    VB Code:
    1. max = Val(Trim$(DataSISPEDA.Recordset.Fields("DocID").Value))
    is what you need.

  10. #10
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    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:
    1. Do While Not DataSISPEDA.Recordset.EOF
    2.        
    3.     If max <= DataSISPEDA.Recordset.Fields("DocID").Value Then
    4.         max = DataSISPEDA.Recordset.Fields("DocID").Value
    5.         DataSISPEDA.Recordset.MoveNext
    6.     Else
    7.         DataSISPEDA.Recordset.MoveNext
    8.     End If
    9.  
    10. Loop
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    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.

  12. #12
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

  13. #13
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Maximum Coding Problem

    In SQL, long would be called Int (i think)
    Something like that.

    r0ach™
    Don't forget to rate the post

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    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.

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    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
  •  



Click Here to Expand Forum to Full Width