Results 1 to 4 of 4

Thread: coding prob

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2004
    Posts
    100

    coding prob

    I have a flexgrid here.
    I want to calculate mean for every FG row.
    My prob is the result for the mean is totally different if we calculate manually.

    Here, I enclose the screen shot for my FG and also, the coding.
    If u could see from the shot, the value for column "Purata" is the value for the mean.
    And the value is not logical.

    Pls help.
    Thank u in advance.

    VB Code:
    1. Private Sub Button7_Click()
    2. On Error Resume Next
    3. Set conn = GetConnection
    4. Set rs = New ADODB.Recordset
    5.  
    6. If optWeight1.Value = True Then
    7. rs.Open "SELECT weight1.Tarikh, weight1.Sampel1, weight1.Sampel2, weight1.Sampel3, weight1.Sampel4, weight1.Sampel5  FROM weight1 WHERE Tarikh between # " & DTPicker1.Value & " # and # " & DTPicker2.Value & " # ", conn, adOpenStatic, adLockOptimistic
    8.  
    9. With rs
    10.     If Not .EOF Then
    11.         'show data from database
    12.       FG_ShowRecordset MSFlexGrid7, rs
    13.      
    14.        'add column
    15.       MSFlexGrid7.Cols = MSFlexGrid7.Cols + 1
    16.       MSFlexGrid7.TextMatrix(0, 6) = "Purata"
    17.       MSFlexGrid7.Cols = MSFlexGrid7.Cols + 1
    18.       MSFlexGrid7.TextMatrix(0, 7) = "Julat"
    19.      
    20.       'mean
    21.       Dim x
    22.       For x = 1 To MSFlexGrid7.Rows
    23.         MSFlexGrid7.TextMatrix(x, 6) = (MSFlexGrid7.TextMatrix(x, 1) + MSFlexGrid7.TextMatrix(x, 2) + MSFlexGrid7.TextMatrix(x, 3) + MSFlexGrid7.TextMatrix(x, 4) + MSFlexGrid7.TextMatrix(x, 5)) / 5
    24.     Next
    25.     End If
    26.  
    27.     .Close
    28.   End With
    29.   conn.Close
    30.  
    31.   Set conn = Nothing
    32.   Set rs = Nothing
    33.  
    34.   Set conn = Nothing
    35.   Set rs = Nothing
    36.  
    37.  
    38. End If
    39.  
    40. End Sub
    41. '==================================================================================
    42. '                            function
    43. '
    44. 'by : FlyGuy @ [url]www.visualbasicforum.com[/url]
    45. '==================================================================================
    46. Private Sub FG_ShowRecordset(myFG As MSFlexGrid, myRST As ADODB.Recordset)
    47.   Dim iField As Integer, iNofFields As Integer
    48.   Dim lRow As Long
    49.  
    50.   Screen.MousePointer = vbHourglass
    51.  
    52.   With myRST
    53.     .MoveFirst
    54.     iNofFields = .Fields.Count
    55.   End With
    56.  
    57.   With myFG
    58.     .Redraw = False
    59.     .AllowUserResizing = flexResizeColumns
    60.     .ScrollTrack = True
    61.     .FixedCols = 0
    62.     .FixedRows = 0
    63.     .Cols = iNofFields
    64.     .Rows = 1
    65.    
    66.     ' setup the header
    67.     For iField = 0 To iNofFields - 1
    68.       .TextMatrix(0, iField) = myRST.Fields(iField).Name
    69.     Next iField
    70.  
    71.    
    72.   End With
    73.  
    74.   With myRST
    75.     Do
    76.       ' increase the number of rows
    77.       lRow = myFG.Rows
    78.       myFG.Rows = myFG.Rows + 1
    79.      
    80.       ' add the values to the current row
    81.       For iField = 0 To iNofFields - 1
    82.         If Not IsNull(.Fields(iField).Value) Then
    83.           myFG.TextMatrix(lRow, iField) = .Fields(iField).Value
    84.         End If
    85.       Next iField
    86.      
    87.       ' proceed to the next record
    88.       .MoveNext
    89.     Loop Until .EOF
    90.   End With
    91.  
    92.   With myFG
    93.     .FixedRows = 1
    94.     .Redraw = True
    95.   End With
    96.  
    97.   Screen.MousePointer = vbNormal
    98. End Sub
    Attached Images Attached Images  

  2. #2
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782
    I was thinking its the order of operation problem but your have the parentheses. Try making the first whole term an integer and then dividing that by five.
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  3. #3
    Fanatic Member
    Join Date
    Sep 2000
    Location
    Over There
    Posts
    522
    ha ha I know what your problem is.

    do the math on this.


    355355355355355 / 5
    or
    350350356352355 / 5

    do those #'s look familiar??

    You should know the solution now.
    It Never Fails. Everytime I try to make a program idiot proof, the world makes a better idiot.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Right, they are strings, the plus sign is concatenating them, rather than adding them. Use Val to fix it.

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