Access in VisualBasic.Net

  • Thread starter Thread starter John Pettitt
  • Start date Start date
J

John Pettitt

I have just bought VB.Net and am trying to convert code
written in VB 4.0 that uses Access. Going directly from
4.0 to Net would not work saying that my VB6 files were
corrupt. I got them converted to VB6 and then put them
through the convertion process to VB net. Most of this
seemed to work except being able to connect to the Access
tables necessary to the functionality. The original
codeing was of the type.
Set Db = DbEngine.Workspaces(0).OpenDataBase("C:\Path")
Set Rs = DbOpenRecordSet("Table Name", dbOpenDynaset)
Rs.FindFirst Criteria
Rs.EOF & Rs.NoMatch, Rs.Close and Set Rs = Nothing.
The Convertion inserted DAODBEngine_definst. and
DAO.RecordsetTypeEnum.dbOpenDynaset but included comments
saying that it Couldn't resolve default property of
object for most of the commands.
Can somebody tell me what I have to do to get this to
work. The Access data bases that I have imported were
from a version of Access that came with Windows95. I now
have the latest version but as yet have not converted
them.
Yours,
John Pettitt
 
Connecting a VB.Net app to Access is a whole different ball game. VB.Net
does not use Recordset's or DAO or ADO Datagrids any longer. I believe you
will need to scrap all your present code that you mention in your post. I
would comment this code out and not delete it. I would also buy a book on
VB.Net as many ways of doing business even from VB6 to .Net has changed
drastically and even a seasoned VB6 programmer has a learning curve.

I believe the lowest common denominator of Access DB might be 97. If the
Access Db isn't Access 97 or higher, then I believe you may have to upgrade
your databases to 97 or higher first.

To connect to Access databases in VB.Net you first make a Connection to your
database by dropping the appropriate Connection from the Data tab of your
Toolbox. Make sure you select the appropriate provider for an Access
database. Once you've got the connection you need to create a DataAdapter;
drag and drop onto your form. Follow the wizard, again be sure to select the
appropriate Jet provider. Once you have the DataAdapter created, then you
must create a DataSet against that DataAdapter. Make sure the DataAdapter
you created in the previous step is highlighted. You should see a link at
the bottom of the Properties called Generate Dataset. Again follow the
wizard. Since this is a new link you will create a new DataSet. Once you
have created these links, you then need to bind each field to your text
boxes.

One of the nice things about creating a link this way to the database, all
your functionality to Add, Delete, Update, Edit, Close etc are encapsulated
already into the dataAdapter, so you only have to write simple code to
process these functions:

I could go into this, but then I'd have to write a book to tell you how to
do this. However, I will post some code that I have:
Note: OleDBIceCream = Connection name; OledaIceCream = DataAdapter name; and
DSIceCream1 is my DataSet name.
Private Sub IceCreamSales_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim pintRecords As Integer

Dim pbndTemp As Binding

pintRecords = OledaIceCream.Fill(DsIceCream1)

'Bind the data to the TextBox on the form.

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.SalesID")

txtSales.DataBindings.Add(pbndTemp)

txtSales.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.fkCompanyID")

txtCompID.DataBindings.Add(pbndTemp)

txtCompID.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.fkIceCreamID")

txtICFlavor.DataBindings.Add(pbndTemp)

txtICFlavor.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.Quantity")

txtQty.DataBindings.Add(pbndTemp)

txtQty.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.IceCreamCost")

txtICCost.DataBindings.Add(pbndTemp)

'fill in color

txtICCost.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.DateOrdered")

txtDateOrdered.DataBindings.Add(pbndTemp)

txtDateOrdered.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.DateDispatched")

txtDateDisp.DataBindings.Add(pbndTemp)

txtDateDisp.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.DatePaid")

txtDatePaid.DataBindings.Add(pbndTemp)

txtDatePaid.BackColor = Color.FromKnownColor(KnownColor.Aquamarine)

pbndTemp = New Binding("Text", DsIceCream1, _

"tblSales.AmountPaid")

txtAmtPaid.DataBindings.Add(pbndTemp)

txtAmtPaid.BackColor = Color.FromKnownColor(KnownColor.Red)

lblDateTime.Text = System.DateTime.Today.ToShortDateString & " " &
System.DateTime.Now.ToShortTimeString

End Sub



Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFirst.Click

Me.BindingContext(DsIceCream1, "tblSales").Position = 0

End Sub


Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrevious.Click

Me.BindingContext(DsIceCream1, "tblSales").Position _

-= 1

End Sub


Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click

Me.BindingContext(DsIceCream1, "tblSales").Position _

+= 1

End Sub


Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLast.Click

Me.BindingContext(DsIceCream1, "tblSales").Position = _

Me.DsIceCream1.tblSales.Rows.Count - 1

End Sub


Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click

Me.BindingContext(DsIceCream1, "tblSales").AddNew()

End Sub


Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click

Dim pdsChangedRows As DataSet

pdsChangedRows = DsIceCream1.GetChanges()

If Not pdsChangedRows Is Nothing Then

OledaIceCream.Update(pdsChangedRows)

End If

End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click

Dim pdsDeletedRows As DataSet

Me.BindingContext(DsIceCream1,
"tblSales").RemoveAt(Me.BindingContext(DsIceCream1, _

"tblSales").Position)

pdsDeletedRows = DsIceCream1.GetChanges(DataRowState.Deleted)

OledaIceCream.Update(pdsDeletedRows)

DsIceCream1.AcceptChanges()

End Sub
 
Back
Top