Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying an Access DB

Status
Not open for further replies.

drimades

IS-IT--Management
Nov 8, 2004
221
MK
[VB 2008]

I have the following connection to the DB:

Dim connString As String
connString = "Provider=Microsoft.JET.OLEDB.4.0;datasource=C:\Documents and Settings\Acer\Desktop\Db-Books.mdb"

if I have a Query string in queStr how can I execute it to return the records I want and which data container should I use for viewing them easily in a form?
 
Are you using a container?

Or, ADO?

If you're using straight ADO, it would be kind of like this:

Code:
Do While Not RS.EOF

textbox1.text = RS(0).Value

RS.MoveNext

Loop

I hope this helps.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Thanks! I was asking how to execute the query and fill a container ...
 
I'm using the following procedure to get data in a master/detail form:
Code:
Private Sub GetData()

    Try
        ' Specify a connection string. Replace the given value with a 
        ' valid connection string for a Northwind SQL Server sample
        ' database accessible to your system.
        Dim connectionString As String = _
            "Integrated Security=SSPI;Persist Security Info=False;" & _
            "Initial Catalog=Northwind;Data Source=localhost"
        Dim connection As New SqlConnection(connectionString)

        ' Create a DataSet.
        Dim data As New DataSet()
        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        ' Add data from the Customers table to the DataSet.
        Dim masterDataAdapter As _
            New SqlDataAdapter("select * from Customers", connection)
        masterDataAdapter.Fill(data, "Customers")

        ' Add data from the Orders table to the DataSet.
        Dim detailsDataAdapter As _
            New SqlDataAdapter("select * from Orders", connection)
        detailsDataAdapter.Fill(data, "Orders")

        ' Establish a relationship between the two tables.
        Dim relation As New DataRelation("CustomersOrders", _
            data.Tables("Customers").Columns("CustomerID"), _
            data.Tables("Orders").Columns("CustomerID"))
        data.Relations.Add(relation)

        ' Bind the master data connector to the Customers table.
        masterBindingSource.DataSource = data
        masterBindingSource.DataMember = "Customers"

        ' Bind the details data connector to the master data connector,
        ' using the DataRelation name to filter the information in the 
        ' details table based on the current row in the master table. 
        detailsBindingSource.DataSource = masterBindingSource
        detailsBindingSource.DataMember = "CustomersOrders"
    Catch ex As SqlException
        MessageBox.Show("To run this example, replace the value of the " & _
            "connectionString variable with a connection string that is " & _
            "valid for your system.")
    End Try

End Sub
Can u please tell me how can I use it not to find the Orders of a Customer but just to display under the Customers grid while I move in the grid the data of the selected Customer only.

Thank u in advance!
 
With that procedure I can display 2 grids with Customers in the first and Orders for the selected Customer in the second. But I need to display the data for each Customer record under the first grid. so if I delete the second grid and use textboxes instead, how can I bind the textboxes to the selected row in the grid Customers?
 
I have a newbie question... just getting my feet wet here...

I know there is an elegant way to do this, but it's not coming to me... I have a form with several labels and I would like to supply the text of these labels from a database.

I have the database connection working, but I'm not sure how to programatically cycle through the code...

Any help getting me going would be greatly appreciated.

Here's the code:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New OleDb.OleDbConnection
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String
        Dim intCount As Integer
        Dim Currentlbl As Integer

        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = \\4fl-w2k01\public\OutOfOffice\OutOfOffice.mdb"
        sql = "SELECT Employees.[First Name], Employees.[Last Name] FROM Employees ORDER BY Employees.[Last Name]"
        da = New OleDb.OleDbDataAdapter(sql, con)

        da.Fill(ds, "Names")
        intCount = 0


        N1.Text = ds.Tables("Names").Rows(0).Item(0) & " " & ds.Tables("Names").Rows(0).Item(1)
        N2.Text = ds.Tables("Names").Rows(1).Item(0) & " " & ds.Tables("Names").Rows(1).Item(1)
        N3.Text = ds.Tables("Names").Rows(2).Item(0) & " " & ds.Tables("Names").Rows(2).Item(1)
        N4.Text = ds.Tables("Names").Rows(3).Item(0) & " " & ds.Tables("Names").Rows(3).Item(1)
        N5.Text = ds.Tables("Names").Rows(4).Item(0) & " " & ds.Tables("Names").Rows(4).Item(1)
        N6.Text = ds.Tables("Names").Rows(5).Item(0) & " " & ds.Tables("Names").Rows(5).Item(1)

Please feel free to comment about any aspect of the code... not just what I have submitted a quesiton about...

Thanks!!

Damon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top