Select problem from DB Ms access to TextBox

Nader

Well-known member
Joined
Oct 9, 2008
Messages
55
Programming Experience
Beginner
I tried this code and it succeed only for one textbox but it didn't succeed for tow textboxes

Dim cmd AsNew OleDbCommand
cmd.Connection = conn
conn.Open()
cmd.CommandText = " SELECT Name,Place FROM TBaccount
TextBox1.Text = cmd.ExecuteScalar
TextBox2.Text = cmd.ExecuteScalar

conn.Close()

 
This process sounds lengthy but takes about 30 seconds.

Select Data --> Add New Data Source
Choose Database and click Next
Click the New Connection button
Click the Change button next to data source
---
If it's a .mdb file you can select Microsoft Access Database File and click OK
---
If it's a .accdb file select <other> from the Data source: list box
Select .NET Framework Data Provider for OLE DB
Click OK
Select Microsoft Office 12.0 Access Database Engine OLE DB Provider
---
Put the full path to your database in the Enter a server or file name: box
Click the Test Connection box to make sure everything is set up correctly.
Click OK
Click Next
Name your connection string something meaningful and click Next
Select the fields you want in your DataSet.
Name your DataSet
Click Finish

You should now have your DataSet in the Data Sources window. If you can't see it click Data --> Show Data Sources.

Click the name of the table in the data set and then click on the drop down arrow. Change it from DataGridView to Details.

Drag the table onto your form.

Visual Studio will create a BindingSource, TableAdapter, and BindingNavigator for you. You'll also see a label with the column name and textbox for each column in your DataSet.

Run your program. Enjoy writing 0 code.
 
I'm using thie code in module and called from Sub. Your way will not service me.

Here is the solution

Dim sql AsString = "Select Name,Company FROM TBaccount"

Dim cmd AsNew OleDbCommand(sql, conn)
Dim dr As OleDbDataReader
conn.Open()
dr = cmd.ExecuteReader
DoWhile dr.Read
Form1.TextBox1.Text = dr("Name")
Form1.TextBox2.Text = dr("Company")
 
Nader, take a read of the DW2 link in my signature, section "Creating a Simple Data App"

MattP said:
/sigh...you can lead a horse to water
p1273ex2b.jpg
 

Latest posts

Back
Top