VB.net connection to Access

AJSatori

Member
Joined
Nov 3, 2006
Messages
5
Programming Experience
Beginner
I know there are many threads about this but I can not find one to help me and I am getting frusterated!

I have a Access DB and I want to populate a combobox...
I am looking at using oledb but I am not sure what to do. This is where I am at and I dont know where to go from here. PLEASE HELP

Dim strConn AsString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\ac49264\My Documents\Estimation tool\Estimation.mdb;" & _
"Jet OLEDB:System Database=system.mdw"
Dim MyConn AsNew OleDb.OleDbConnection(strConn)
Dim MySQL AsString = "Select * from Release"
 
VB.NET:
[/COLOR]
[COLOR=black]Private MyDatatable As New System.Data.Datatable[/COLOR]
Private MyDataAdapter As New System.Data.OleDb.OleDbDataAdapter
 
 
 
Dim strConn AsString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\ac49264\My Documents\Estimation tool\Estimation.mdb;" & _
"Jet OLEDB:System Database=system.mdw"
Dim MyConn AsNew OleDb.OleDbConnection(strConn)
Dim MyCommand As New OleDbCommand("SELECT * FROM Release", MyConn)
Me.MyDataAdapter.SelectCommand = MyCommand
 
Me.MyDataAdapter.Fill(me.MyDatatable)
 
YourCombobox.DataSource = Me.MyDatatable
YourCombobox.DisplayMember = The Name Of The Column You Want To Display As String

Try that and let me know how it goes. If it works and you want me to explain it then post back.
 
well, I had to add "oledb." in front of all the "OleDbCommand" and it did not like the me. so I removed them. after that I an it and i got the error below on this line
MyDataAdapter.Fill(MyDatatable)

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll
 
Easiest way here is post the code including the sub that the code resides in. The 'Me' should not have given you any problems but it depends on where that code is.
 
This is the whole sub routine it is in and it errors on the .fill part


PrivateSub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

Dim strConn AsString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\ac49264\My Documents\Estimation tool\Estimation.mdb;" & _
"Jet OLEDB:System Database=system.mdw"

Dim MyConn AsNew OleDb.OleDbConnection(strConn)
Dim MyCommand AsNew OleDb.OleDbCommand("SELECT * FROM Release", MyConn)



MyDataAdapter.SelectCommand = MyCommand
MyDataAdapter.Fill(MyDatatable)
ComboBox1.DataSource = MyDatatable
ComboBox1.DisplayMember = "re_name"

end sub
 
VB.NET:
'These two line must be outside of any subs or functions.
Private MyDatatable As New System.Data.Datatable
Private MyDataAdapter As New System.Data.OleDb.OleDbDataAdapter

 
PrivateSub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

Dim strConn AsString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\ac49264\My Documents\Estimation tool\Estimation.mdb;" & _
"Jet OLEDB:System Database=system.mdw"

Dim MyConn AsNew OleDb.OleDbConnection(strConn)
Dim MyCommand AsNew OleDb.OleDbCommand("SELECT * FROM Release", MyConn)



Me.MyDataAdapter.SelectCommand = MyCommand
 
Try
Me.MyDataAdapter.Fill(Me.MyDatatable)
ComboBox1.DataSource = MyDatatable
ComboBox1.DisplayMember = "re_name"

Catch Ex As Exception
Messagebox.Show(Ex.Message)
End Try



end sub

I'm assuming that you have the private variable located in your class somewhere, you shouldn't get any errors on the 'Me' part. Then post the messge that pops up in the messagebox if an exception is thrown.
Also if you place the following line right at the top of the class, above the
Public Class Something line
VB.NET:
Imports System.Data.OleDb

You won't have to type system.data.oledb infront of the oledbcommands/dataadapters anymore.
 
Back
Top