4 Replies - 994 Views - Last Post: 05 August 2012 - 10:16 PM Rate Topic: -----

#1 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Group and Count datarecords from database

Posted 05 August 2012 - 08:54 PM

can someone give me an idea on how to do this scenario:
i have an ms access database table named IssuedBooks, this table has a primary key "IDNumber" which is set into AutoNumberField. In my vb, i have a form with 20 textboxes (10 for txtBorrowerNames, 10 for txtTotalNumber of BorrowerNames). what i want to happened is that when i clicked a button txtBorrowerName1 will be filled with 1 BorrowerName and its corresponding txtTotalNumber1 will be filled with the total number that, that name is repeated in that table.
for example,
KAREN is repeated 30x; txtBorrowerName(KAREN) = txtTotalNumber(30)
ANA is repeated 20x; txtBorrowerName(ANA) = txtTotalNumber(20)
DEAN is repeated 10x; txtBorrowerName(DEAN) = txtTotalNumber(10)

i can't contruct any codes yet, because i'm a bit confused on what procedure i'll do; whether to use arrays or just a select query with COUNT, DISTINCT and GROUP BY options.

Is This A Good Question/Topic? 0
  • +

Replies To: Group and Count datarecords from database

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 966
  • View blog
  • Posts: 3,721
  • Joined: 02-July 08

Re: Group and Count datarecords from database

Posted 05 August 2012 - 09:09 PM

Design-wise a DataGridView would work great for this. Make a query and display the results as the datasource.

Dim query = From rec As Record In Library Where rec.name = "KAREN"
dgv.Datasource = query


You need to replace the parts of the above query with your data model.
Was This Post Helpful? 0
  • +
  • -

#3 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: Group and Count datarecords from database

Posted 05 August 2012 - 09:42 PM

i've also thought that having dgv would be easier, but in this form i did't plan placing a dgv. is it possible just to use a textbox? and one more thing, with regards to this,

Quote

Where rec.name = "KAREN"
, what if i don't have a specific rec.name, i mean karen, ana and dean where just examples so txtBorrowerNames would primarily depends on the greatest Total number that the names are repeated. If "karen" has the greatest number of repeated name then she will be placed on the first txtBorrowerNames. Can you follow me? pardon if i can't explain it clearly. just to clarify what's this for, this will be the basis of a pie graph that i created in the same form that these textboxes belong.
Was This Post Helpful? 0
  • +
  • -

#4 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: Group and Count datarecords from database

Posted 05 August 2012 - 10:10 PM

i've solved it now.. thanks for your idea.

here is the codes i used:

  Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pie.Click
        con.Open()
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        da = New OleDbDataAdapter("Select TOP 10 Borrower, COUNT(Borrower) as Total from IssueBooks GROUP BY Borrower ORDER BY COUNT(Borrower) desc", con)
        da.Fill(dt)
        DataGridView1.DataSource = dt.DefaultView
       
 txtName1.Text = DataGridView1.Rows(0).Cells("Borrower").Value
        txtNum1.Text = DataGridView1.Rows(0).Cells("Total").Value
        con.Close()

    End Sub



i still used datagridview, just like what you said, and in order to achieve what i desired, i just hide it upon load form.
  Private Sub PieGraph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.Hide()
    End Sub




thank you.
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 966
  • View blog
  • Posts: 3,721
  • Joined: 02-July 08

Re: Group and Count datarecords from database

Posted 05 August 2012 - 10:16 PM

Great, I was thinking of Group By. Anyway this is a great site too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1