Query Access Database and Display Data on New Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhonda2010
    New Member
    • Aug 2010
    • 4

    Query Access Database and Display Data on New Form

    I have a program in which I want to have the user enter two percentages and display the data matching the criteria on a new form. The query would be:
    Code:
    Select *
    From Corrosion
    Where corrpct1 >= {column in database} OR
          corrpct2 >= {column in database}
    The way I currently have it written is to display only one row of data. I need to display any number of rows that meet this criteria. My current code is:

    Code:
    Private Sub btnDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetails.Click
            Dim form As New frmDetails()
            Me.Hide()
            form.Show()
            Try
                cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Corrosion.mdb;")
                
                cn.Open()
                cmd = New OleDbCommand("select * from Corrosion", cn)
                dr = cmd.ExecuteReader
                While dr.Read()
                    form.txtID.Text = dr(0)
                    form.txtCountry.Text = dr(1)
                    form.txtCompany.Text = dr(2)
                    form.txtAsset.Text = dr(3)
                    form.txtBlock.Text = dr(4)
                    form.txtFacility.Text = dr(5)
                    form.txtAreaItem.Text = dr(6)
                    form.txtCode.Text = dr(7)
                End While
            Catch
            End Try
            dr.Close()
            cn.Close()
        End Sub
    This will open the new form and put the information in the textboxes I have. However, I never know how many textboxes I will need to display.

    My questions are:

    1. How can I do my query to incorporate the user entered criteria (in the where statement)?
    2. How can I display ALL of the data instead of just one line?

    Thank you for any help you can give.

    Rhonda
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    #1. your sql query will be like....
    Select * From Corrosion Where {column in database} <=corrpct1 OR {column in database}<=corr pct2

    #2. If this query returns more than one row then instead of using datareader use dataset....

    fill the dataset(say ds) with this sql query

    take a datagridview or listview or datalist in the frmDetails form

    now put........
    Dim form As New frmDetails()
    Me.Hide()
    form.DataGridvi ew1.DataSource= ds
    form.Show()

    Comment

    Working...