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 bkrike 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 collect data from an Access DB using VB.Net 1

Status
Not open for further replies.

terryomag

Programmer
Aug 30, 2005
6
US
I have a fairly simple Access database, and I need to pull some of the fields from it, do some calculations and generate a report. I'd like to have a code example of how to recognize the database, how to open it for input, read selected valued from it, and then close it. I will NOT be updating the database, simply reading some of the data and manipulating the data. I used to do this in VB 6.0, but everything seems to have changed. I don't want to write in VB 6.0 and convert the code to VB.Net. I'd really like to just access the data directly.
 
You could try something like:

Code:
    Dim axsconn As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDbName.mdb;User Id=admin;Password=;")
    Dim dr As Data.OleDb.OleDbDataReader
    axsconn.Open()
    Dim axscmd As New Data.OleDb.OleDbCommand("SELECT * FROM ATable", axsconn)
    dr = axscmd.ExecuteReader
    If dr.HasRows Then
      While dr.Read
        If IsDBNull(dr.Item("AField")) Then
          ListBox1.Items.Add("<NULL>")
        Else
          ListBox1.Items.Add(dr.Item("AField").ToString)
        End If
      End While
    Else
      MessageBox.Show("No records")
    End If
    dr.Close()
    axscmd = Nothing
    axsconn.Close()
    axsconn = Nothing

While dr.Read
'At this point type dr.
'and check the various options to fnd the one most suitable
End While


Hope this helps.
 
Dear earthandfire,
I am trying to get your code to work, but I keep getting an OleDbException error on the statement
.
axsconn.open()
.
It says "OleDbException in system.data.dll"
.
Any suggestions??

Thanks.
 
You may need to check the connection string to make sure that it matches your database.


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top