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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Populate text boxes with .mdb field data?

Status
Not open for further replies.

jbgood

Technical User
Oct 30, 2003
4
US
I am creating a small windows app., a journal that tracks entries and the date and time they were made.

This was a MS Access app. I wish to use the old .mdb table and create a new interface with vb.net.

I have been able to connect to the .mdb but can't quite figure out how to read through the records yet.

This is the code I am using to connect to my .mdb database.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oOleDbConnection As OleDb.OleDbConnection
Dim sConnString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\Jackie\My Documents\journal2.mdb;" & _
"User Id=admin;" & _
"Password="

oOleDbConnection = New OleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()

'Fields in my db are: entry_no, entry_date, entry_time,entry_subject,entry_emotion, and entry
End Sub

This is what I think I know.
I need to define variables.
Read the first record.
Populate my variables with the appropriate data.
Display the info in text boxes on my form.
Repeat these steps as I move through the records.

Or would it be better to read the whole database into memory?

Either way it sounds easy enough but I'm missing the proper syntax.

Could someone please show me a piece of code that would help get me jumpstarted?

FYI
I am using Visual Basic.net 2003
Database was created with MS Access 2000

TIA
Jack

Everything is harder than it looks
nothing is as easy as it seems.


 
try reading through

just in case the link is broke

<reference author &quot;Gustavo Pares&quot;>

Filling Dynamic Objects using ADO.NET
By Gustavo Pares 07/20/2003
Download

Sometimes you need to use information of different types like strings, booleans or doubles in the same data structure. In that case you would like to be able to create your own objects to fulfill your needs. But if you had to create 5 thousand different objects you wouldn´t be too happy to do that by coding each one of them in a static way. You would like to fill the data in each one of the objects dynamically and most probably using databases.

If you create your own object and you can fill it with the data you want, then you are ready to get your job done. The use of the OleDbDataReader is sometimes better than using OleDbDataAdapter because you have better control of what you are doing. Follow the next steps and enjoy using your own data types.

Step # 1: Add the following statement at the top of your source code

imports System.Data.OleDb

Step # 2: Declare the following things you will use to read data from a database

Dim cmd as new OleDbCommand ()
Dim reader as OleDbDataReader

Step # 3 : Open the database connection
myConnection.Open()

Step # 4:Configure the OleDbCommand properties
cmd.Connection = myConnection
md.CommandText = &quot;SELECT A, B, C, D FROMaTable WHERE A = Yes&quot;

Step # 5:Instantiate the reader with the data returned by the OleDbCommand
reader = cmd.ExecuteReader()

Step # 6:Create an array of objects of the class you defined
Imports System

Namespace myNamespace

Public Class [myClass]

Public A As Integer
Public B As [String]
Public C As Boolean
Public D As Boolean

Public Sub New()
A = 0
B = &quot;0&quot;
C = False
D = False
End Sub 'New
End Class '[myClass]
End Namespace 'myNamespace

Dim arrMyClass(150) As [myClass]

Step # 7: Loop through the OleDbDataReader to get the information you need and fill the object array Dim row As Integer = 0
While reader.Read()
Dim getA As Integer = reader.GetInt32(0)
Dim getB As [String] = reader.GetString(1)
Dim getC As Boolean = reader.GetBoolean(2)
Dim getD As Boolean = reader.GetBoolean(3)
arrMyClass(row).A = getA
arrMyClass(row).B = getB
arrMyClass(row).C = getC
arrMyClass(row).D = getD
row += 1 ' Counts how many rows are being read
End While
reader.Close() ' It is important to close the OleDbDataReader
</reference>


_____________________________________________________________________
onpnt2.gif

The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
 
Thanks for the quick reply, I'm running out the door right now but will try to absorb the info you supplied later tonight. I will let you know how I made out.

Thanks again
Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top