Results 1 to 23 of 23

Thread: SQL Statement Count Total Quantity

Hybrid View

  1. #1

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Thanks,Flair.

    However,I encounter another problems then.I will display out all the model and total quantity in the datagrid by two columns.When the total quantity is less than the set TargetQty,it will display out vbRed and if the total quantity meet the TargetQty,it will display vbGreen.To get the Total quantity,Qty should add the Exceed Packout for that day as shown in the code below so that it can compared with the TargetQty.But,dunno why some of the data should be green,but it turns to Red because it doesn't add up my Qty.

    Maybe my explanation is not good enough.Below attached is the coding that I do.Hope anyone here can solve the problem for me.Thank You.

    VB Code:
    1. Private Sub DisplayGrid ()
    2. .....
    3.  
    4. fg.Clear
    5. sSQL = "SELECT * FROM PackQty_View"
    6. Set oRs1 = oShip.Execute(sSQL)
    7. 'Run FROM PackQty_View and get ModelDefined(Ex:- I730 and I90) and Qty by 2 and 11
    8.  
    9.   Do Until oRs1.EOF
    10.    
    11.         fg.Rows = oRs1.RecordCount + 1
    12.         fg.TextMatrix(Row, 1) = oRs1!Qty
    13.         Qty = oRs1!Qty
    14.         ModelDefined = oRs1!ModelDefined
    15.         oRs1.MoveNext
    16.     Loop
    17.  
    18. DisplaySQL = "SELECT Model,TargetQty,Exceed FROM DataGrid"
    19. Set rst = oSQL.Execute(DisplaySQL)
    20.  
    21.     fg.ColWidth(0) = 2900
    22.     fg.ColWidth(1) = 1600
    23.    
    24.     fg.ColAlignment(0) = flexAlignLeftCenter
    25.     fg.ColAlignment(1) = flexAlignLeftCenter
    26.  
    27.     Row = 0
    28.    
    29.     fg.TextMatrix(Row, 0) = "Model"
    30.     fg.TextMatrix(Row, 1) = "Total Quantity"
    31.    
    32.     Row = Row + 1
    33.    
    34. Do Until rst.EOF
    35.    
    36.         fg.Rows = rst.RecordCount + 1
    37.         fg.TextMatrix(Row, 0) = rst!Model
    38.  
    39.         DisplaySQL = "SELECT * FROM DataGrid"
    40.         Set rst = oSQL.Execute(DisplaySQL)
    41.        
    42.         If fg.TextMatrix(Row, 0) <> oRs1!ModelDefined Then  'Izit i compare like this make it compare only with the Model in the first row in PackQty_View instead of comparing with the second row?
    43.                 Qty = 0
    44.             Else
    45.                 fg.TextMatrix(Row, 1) = oRs1!Qty
    46.                 Qty = oRs1!Qty
    47.                 oRs1.MoveNext
    48.             End If
    49.  
    50.         TargetQty = rst!TargetQty
    51.         Exceed = rst!Exceed
    52.         sTotal = Qty + Exceed
    53.    
    54.         fg.TextMatrix(Row, 1) = sTotal
    55.  
    56.         If fg.TextMatrix(Row, 1) < TargetQty Then
    57.          fg.Row = Row
    58.          fg.Col = 1
    59.          fg.CellBackColor = vbRed
    60.  
    61.         ElseIf fg.TextMatrix(Row, 1) >= TargetQty Then
    62.          fg.Row = Row
    63.          fg.Col = 1
    64.          fg.CellBackColor = vbGreen
    65.         End If
    66.  
    67.         Row = Row + 1
    68.         rst.MoveNext
    69.  
    70.  
    71.     Loop
    72.  
    73. Exit Sub
    74.  
    75. End Sub
    Human Knowledge Belongs To The World

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Statement Count Total Quantity

    Why use multiple SQL statements at all?

    Code:
    insert into PackQty
       Select * from Model_Defined_View
       where trans_date >= (SELECT StartDate FROM DateTime)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Yah,i've put this statement at the beginning of the statement.However,m still facing some problems for the count quantity in the statement which make the color(green,red) turns differently because of the wrong count
    Human Knowledge Belongs To The World

  4. #4

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    anyone can help?Thanks
    Human Knowledge Belongs To The World

  5. #5

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    How to correct this part?It only keep on comparing with the first ModelDefined.So,when the second ModelDefined is actually run the Qty= oRs1!Qty part,but it goes to Qty = 0.Please help.

    If fg.TextMatrix(Row, 0) <> oRs1!ModelDefined Then
    Qty = 0
    Else
    Qty = oRs1!Qty
    End If
    Human Knowledge Belongs To The World

  6. #6
    Addicted Member
    Join Date
    Aug 2004
    Posts
    176

    Re: SQL Statement Count Total Quantity

    You are doing a
    VB Code:
    1. Do Until rst.EOF
    Then inside this you are again defining
    VB Code:
    1. DisplaySQL = "SELECT * FROM DataGrid"
    2.         Set rst = oSQL.Execute(DisplaySQL)
    This would reset the recordset. The .Movenext would no more be effective. Don't set it again.

  7. #7

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    How about this?

    VB Code:
    1. Private Sub DisplayGrid ()
    2. i = 0
    3. fg.Clear
    4.     sSQL = "SELECT * FROM PackQty_View "
    5.     Set oRs1 = oShip.Execute(sSQL)
    6.     Qty = oRs1!Qty
    7.     ModelDefined = oRs1!ModelDefined
    8.  
    9.    DisplaySQL = "SELECT Model,TargetQty,Exceed FROM DataGrid"
    10.    Set rst = oSQL.Execute(DisplaySQL)
    11.  
    12.     fg.ColWidth(0) = 2900
    13.     fg.ColWidth(1) = 1600
    14.    
    15.     fg.ColAlignment(0) = flexAlignLeftCenter
    16.     fg.ColAlignment(1) = flexAlignLeftCenter
    17.  
    18.     Row = 0
    19.    
    20.     fg.TextMatrix(Row, 0) = "Model"
    21.     fg.TextMatrix(Row, 1) = "Total Quantity"
    22.    
    23.     Row = Row + 1
    24.    
    25. Do Until rst.EOF
    26.  
    27.     fg.Rows = rst.RecordCount + 1
    28.     fg.TextMatrix(Row, 0) = rst!Model
    29.  
    30.  
    31.     If fg.TextMatrix(Row, 0) <> oRs1!ModelDefined Then
    32.                 Qty = 0
    33.             Else
    34.                 Qty = oRs1!Qty
    35.             End If
    36.            
    37.  
    38.         TargetQty = rst!TargetQty
    39.         Exceed = rst!Exceed
    40.         sTotal = Qty + Exceed
    41.  
    42.         fg.TextMatrix(Row, 1) = sTotal
    43.  
    44.         If fg.TextMatrix(Row, 1) < TargetQty Then
    45.          fg.Row = Row
    46.          fg.Col = 1
    47.          fg.CellBackColor = vbRed
    48.  
    49.         ElseIf fg.TextMatrix(Row, 1) >= TargetQty Then
    50.          fg.Row = Row
    51.          fg.Col = 1
    52.          fg.CellBackColor = vbGreen
    53.         End If
    54.  
    55.         Row = Row + 1
    56.         rst.MoveNext
    57.    
    58. Loop
    59. Exit Sub
    60. End Sub
    Human Knowledge Belongs To The World

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