Results 1 to 3 of 3

Thread: searching data in an access database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    searching data in an access database

    Hey everyone, I know how to search for one specific number from a single line in an access database using a query, but now I have to search through a whole database and display each instance. Ex) I search the number "1" in column "1" of the database and it has "2" instances. So now I have to take the corresponding data in the rows next to it in order to display them in the label.

    search: "1", both results are ID = "1"

    results:
    dates amount
    10/13/2009 $43.00
    12/25/2009 $86.00

    I know how to put the results for "1" line of a database into the .item field, but im confused on how to search multiple lines of a database and store multiple items. Heres what I have so far...

    Code:
    Private Sub btnShowPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowPay.Click
            Dim targetid As String
            Dim dummy As Integer
            Dim selectstring As String
            Dim dates As String
            Dim amount As String
            Dim total As String
    
            If Not Integer.TryParse(txtID.Text.Trim, dummy) Then
                MessageBox.Show("member id must be integer")
                txtID.Focus()
                txtID.SelectAll()
                Exit Sub
            End If
            targetid = txtID.Text.Trim
    
            'create select statement
            selectstring = "select * from payments where member_id = " & targetid
    
            'find total amount of rows in database
            total = "select * from payments"
    
            'create adapter
            memberadapter = New OleDb.OleDbDataAdapter(selectstring, connectionstring)
    
            'create and fill the member data table
            membertable = New DataTable
            memberadapter.Fill(membertable) 'fill the data table
    
            'check to see if a member was found or not
            If membertable.Rows.Count = 0 Then
                lblDispPay.Text = "id not found"
                Exit Sub
            End If
    
            lblDispPay.Text = "Dates            Amount"
    
            ' extract data
            For x = 0 To CInt(total) Step 1
                dates = CStr(membertable.Rows(x).Item(2))
                amount = CStr(membertable.Rows(x).Item(3))
                lblDispPay.Text &= dates & "   " & amount
            Next x
    
        End Sub

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    Re: searching data in an access database

    So i figured out the loop, but I dont know how to set the end of the loop. Its keeps giving me an error message (index out of range). How do i change it, so it stops when it finishes going through all the numbers? Thanks...

    Code:
     ' extract data
            For x = 0 To ??? Step 1
                dates = CStr(membertable.Rows(x).Item(2))
                amount = CStr(membertable.Rows(x).Item(3))
                lblDispPay.Text &= dates & "   " & amount & vbCrLf
            Next x

  3. #3
    New Member
    Join Date
    Apr 2011
    Posts
    9

    Re: searching data in an access database

    ??? should be the total count of the record minus 1 (since thes tart of your array is 0)

    Dim TotalCount as integer = membertable.Rows.Count - 1

    For x = 0 to TotalCount

    Next

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