简体   繁体   中英

Visual Basic Group by

Need help...

Table Name - cashbook
Field name - pname & amount

Examble

pname              amount
Antony              1500
Jose                3000
Antony              3500

need to display in msflexgrid like belo

Antony - 5000
Jose   - 3000

below my code is not working....can anybody help me

Rec.open "select  from pname,sum(amount) as amt from cashbook group by pname order by pname,var,adopendynamic,adlockoptimist"

While rec.eof=false
    Pymnt.rows=pymnt.rows+1
    Pymnt.textmatrix(pymnt.rows -1,0)=rec!pname
    Pymnt.textmatrix(pymnt.rows -1,1)=amt
    Rec.movenext
Wend
Rec.close

I made a few fixes to your code and came up with the following:

Public Sub Test()
   rec.Open "select pname, sum(amount) as amt from cashbook group by pname order by pname", var, adOpenDynamic, adLockOptimistic

   While Not rec.EOF
      pymnt.Rows = pymnt.Rows + 1
      pymnt.textmatrix(pymnt.Rows - 1, 0) = rec.Fields("pname").Value
      pymnt.textmatrix(pymnt.Rows - 1, 1) = rec.Fields("amt").Value
      rec.MoveNext
   Wend

   rec.Close
End Sub

The fixes I made include:

  1. Constructing a proper SQL statement
  2. Supplying the correct parameters to the Open method
  3. Assuming var is an open Connection object
  4. Correcting the spelling of adLockOptimistic
  5. Changing how the data is accessed in the loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM