VB.NET: MS-Access Interaction Tutorial (Part I)

Pages: 1, 2
free web hosting

Read Latest Entries..: (Post #18) by iGuest on Aug 18 2008, 06:14 AM. (Line Breaks Removed)
Replying to iGuestUSe the keystroke instance of the textbox and use the Asscii-reply by Ivan
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > .NET (VB, C# & J#)

VB.NET: MS-Access Interaction Tutorial (Part I)

miCRoSCoPiC^eaRthLinG
VB.NET - MS-Access Interaction Tutorial

    [/tab]I got down to writing this tutorial because of this certain question that Dhanesh posted on our forums here. This tutorial will attempt to show you how to:
  • Create a MS-Access Database
  • Create/Edit/Delete Tables in it
  • Access it and Add/Edit/Update data in those tables
all with VB.NET.


Note:[tab]This is not a beginner's tutorial and I'll assume you know all the basics of creating WinForm based applications using VS.NET. The screen-shots provided are from VS.NET 2005 - but you shouldn't face any difficult even if you're using VS.NET 2003. The functionality is essentially the same.

    [/tab]We will start by creating a blank Windows Application project called MS-AccessTest.

Creating a MS-Access Database
[tab]Microsoft didn't provide us with any easy modern method of creating an Access Database. No such classes are provided in the .NET Framework and hence we'll have to fall back onto an old library routine that came along with the older genre of Visual Studio. As for accessing a database and modifying data in it, we can conveniently use the OleDB Wrapper.

    [/tab]For now, right-click on the project name in the Solution Explorer and select Add Reference. This will bring up the Add Reference dialog box. Next select the COM tab and scroll down till you find a library named Microsoft ADO Ext. 2.8 for DLL and Security as shown in the screen-shot.
IPB Image

Click OK to add the reference to this dll. The library should show up as ADOX in your References in the Solution Explorer, if you're using VS.NET 2003. Else you can spot it under the References tab under Project Properties in VS.NET 2005.

[tab]Next, we'll create our own class to encapsulate all database related routines. In the Solution Explorer, right-click on the solution name and select Add > Class. Name this class Database.We'll create a function called CreateDatabase( FileName ) that'll take the FileName as a parameter and create a blank database at the given location.

Here's the code for the function - it returns True or False depending on the success/failure in creating the database. We use the Create method which can be found under the ADOX.Catalog class to create the database. This method takes the standard OleDB connection string (as shown in oConnect) and creates a blank database with the file name passed to it as Data Source.
CODE

'Method to create a blank database
Public Function CreateDatabase(ByVal FileName As String) As Boolean

'Instantiate the ADOX Object
Dim ADOXCatalog As ADOX.Catalog = New ADOX.Catalog
Dim oConnect As String

'Setup the connection string
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName

' Try creating the database
Try

'Create the database
ADOXCatalog.Create(oConnect)

Catch ex As System.Runtime.InteropServices.COMException
Catch ex As Exception

'Show error message and return failure
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
Return False

Finally

'Dispose the object
ADOXCatalog = Nothing

End Try

'Return success
Return True

End Function



Create/Edit/Delete Tables
    [/tab]Our next job is to define some tables in this database. For this we use the standard OleDB Data Provider.You need to have a little prior knowledge of SQL to understand this - although syntactically this is very simple. We start by defining a process called CreateTable(). We'll create a simple table named accessTest for demonstration purposes. This will have just two columns:
  • ID - which is of the Data Type COUNTER, which basically means an Auto Incrementing Integer field
  • Name - A TEXT field of length 50, which will store the name of a person.
Moreover, we'll set the ID field as the Primary Key.The code for this function follows:
CODE

Public Function CreateTable(ByVal FileName As String) As Boolean

'Define the connectors
Dim oConn As OleDbConnection
Dim oComm As OleDbCommand
Dim oConnect, oQuery As String

'Define connection string
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";User ID=Admin;Password="

'Define the query string the creates the table
oQuery = "CREATE TABLE accessTest ( ID Counter," & _
"Name TEXT(50) NOT NULL," & _
"PRIMARY KEY(ID) )"

' Instantiate the connectors
oConn = New OleDbConnection(oConnect)
oComm = New OleDbCommand(oQuery, oConn)

'Try connecting and crate the table
Try

'Open the connection
oConn.Open()

'Perform the Non-Query
oComm.ExecuteNonQuery()

'Close the connection
oConn.Close()

Catch ex As OleDb.OleDbException
Catch ex As Exception

'Show error message and return failure
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
Return False

Finally

'Dispose the connector objects
If Not (oConn Is Nothing) Then
oConn.Dispose()
oConn = Nothing
End If
If Not (oComm Is Nothing) Then
oComm.Dispose()
oComm = Nothing
End If

End Try

'Return success
Return True

End Function


[tab]Next we've to define some methods to READ data from this table and return to us as a DataSet - which can be easily bound to a DataGrid to display the data directly on screen. Once again - we define a method called FetchData() to facilitate this. We use the standard connectors that we'd used in the above procedure, but along with that we introduce a new object called a DataAdapter - which is used to read the whole table and place the data into the DataSet.
CODE

Public Function FetchData(ByVal FileName As String) As DataSet

'Define the connectors
Dim oConn As OleDbConnection
Dim oComm As OleDbCommand
Dim oData As OleDbDataAdapter
Dim resultSet As New DataSet
Dim oConnect, oQuery As String

'Define connection string
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";User ID=Admin;Password="

'Query String
oQuery = "SELECT * FROM accessTest"

'Instantiate the connectors
oConn = New OleDbConnection(oConnect)
oComm = New OleDbCommand(oQuery, oConn)
oData = New OleDbDataAdapter(oQuery, oConn)

Try

'Open connection
oConn.Open()

'Fill dataset
oData.Fill(resultSet, "accessTest")

'Close connection
oConn.Close()

Catch ex As OleDb.OleDbException
Catch ex As Exception

'Show error message and exit
MsgBox(ex.Message & vbCrLf & ex.StackTrace)

Finally

'Dispose the connector objects
If Not (oConn Is Nothing) Then oConn.Dispose()
oConn = Nothing
If Not (oComm Is Nothing) Then oComm.Dispose()
oComm = Nothing
If Not (oData Is Nothing) Then oData.Dispose()
oData = Nothing

End Try

'Return results
Return resultSet

End Function


So far we've created some basic routines to create database/table and fetch data. Now it's time to concentrate on the main interface and come up with a way to display the fetched data on the screen.For this we'll utilize a DataGrid control, as it can be easily bound to the fetched data. On the main form, drop a DataGrid. Also add a DataSet to the main form. We're going to link these two controls and whenever we update the dataset with any data from the database, the datagrid will reflect the changes on screen. Likewise, when data is modified on screen, the changes get reflected in the actual database.


Continued in Part II ...

 

 

 


Reply

apurba
QUOTE(miCRoSCoPiC^eaRthLinG @ May 15 2006, 08:31 PM) *
VB.NET - MS-Access Interaction Tutorial

    [/tab]I got down to writing this tutorial because of this certain question that Dhanesh posted on our forums here. This tutorial will attempt to show you how to:
  • Create a MS-Access Database
  • Create/Edit/Delete Tables in it
  • Access it and Add/Edit/Update data in those tables
all with VB.NET.
Note:[tab]This is not a beginner's tutorial and I'll assume you know all the basics of creating WinForm based applications using VS.NET. The screen-shots provided are from VS.NET 2005 - but you shouldn't face any difficult even if you're using VS.NET 2003. The functionality is essentially the same.

    [/tab]We will start by creating a blank Windows Application project called MS-AccessTest.

Creating a MS-Access Database
[tab]Microsoft didn't provide us with any easy modern method of creating an Access Database. No such classes are provided in the .NET Framework and hence we'll have to fall back onto an old library routine that came along with the older genre of Visual Studio. As for accessing a database and modifying data in it, we can conveniently use the OleDB Wrapper.

    [/tab]For now, right-click on the project name in the Solution Explorer and select Add Reference. This will bring up the Add Reference dialog box. Next select the COM tab and scroll down till you find a library named Microsoft ADO Ext. 2.8 for DLL and Security as shown in the screen-shot.


Click OK to add the reference to this dll. The library should show up as ADOX in your References in the Solution Explorer, if you're using VS.NET 2003. Else you can spot it under the References tab under Project Properties in VS.NET 2005.

[tab]Next, we'll create our own class to encapsulate all database related routines. In the Solution Explorer, right-click on the solution name and select Add > Class. Name this class Database.We'll create a function called CreateDatabase( FileName ) that'll take the FileName as a parameter and create a blank database at the given location.

Here's the code for the function - it returns True or False depending on the success/failure in creating the database. We use the Create method which can be found under the ADOX.Catalog class to create the database. This method takes the standard OleDB connection string (as shown in oConnect) and creates a blank database with the file name passed to it as Data Source.
CODE

'Method to create a blank database
Public Function CreateDatabase(ByVal FileName As String) As Boolean

'Instantiate the ADOX Object
Dim ADOXCatalog As ADOX.Catalog = New ADOX.Catalog
Dim oConnect As String

'Setup the connection string
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName

' Try creating the database
Try

'Create the database
ADOXCatalog.Create(oConnect)

Catch ex As System.Runtime.InteropServices.COMException
Catch ex As Exception

'Show error message and return failure
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
Return False

Finally

'Dispose the object
ADOXCatalog = Nothing

End Try

'Return success
Return True

End Function

Create/Edit/Delete Tables
    [/tab]Our next job is to define some tables in this database. For this we use the standard OleDB Data Provider.You need to have a little prior knowledge of SQL to understand this - although syntactically this is very simple. We start by defining a process called CreateTable(). We'll create a simple table named accessTest for demonstration purposes. This will have just two columns:
  • ID - which is of the Data Type COUNTER, which basically means an Auto Incrementing Integer field
  • Name - A TEXT field of length 50, which will store the name of a person.
Moreover, we'll set the ID field as the Primary Key.The code for this function follows:
CODE

Public Function CreateTable(ByVal FileName As String) As Boolean

'Define the connectors
Dim oConn As OleDbConnection
Dim oComm As OleDbCommand
Dim oConnect, oQuery As String

'Define connection string
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";User ID=Admin;Password="

'Define the query string the creates the table
oQuery = "CREATE TABLE accessTest ( ID Counter," & _
"Name TEXT(50) NOT NULL," & _
"PRIMARY KEY(ID) )"

' Instantiate the connectors
oConn = New OleDbConnection(oConnect)
oComm = New OleDbCommand(oQuery, oConn)

'Try connecting and crate the table
Try

'Open the connection
oConn.Open()

'Perform the Non-Query
oComm.ExecuteNonQuery()

'Close the connection
oConn.Close()

Catch ex As OleDb.OleDbException
Catch ex As Exception

'Show error message and return failure
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
Return False

Finally

'Dispose the connector objects
If Not (oConn Is Nothing) Then
oConn.Dispose()
oConn = Nothing
End If
If Not (oComm Is Nothing) Then
oComm.Dispose()
oComm = Nothing
End If

End Try

'Return success
Return True

End Function


[tab]Next we've to define some methods to READ data from this table and return to us as a DataSet - which can be easily bound to a DataGrid to display the data directly on screen. Once again - we define a method called FetchData() to facilitate this. We use the standard connectors that we'd used in the above procedure, but along with that we introduce a new object called a DataAdapter - which is used to read the whole table and place the data into the DataSet.
CODE

Public Function FetchData(ByVal FileName As String) As DataSet

'Define the connectors
Dim oConn As OleDbConnection
Dim oComm As OleDbCommand
Dim oData As OleDbDataAdapter
Dim resultSet As New DataSet
Dim oConnect, oQuery As String

'Define connection string
oConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";User ID=Admin;Password="

'Query String
oQuery = "SELECT * FROM accessTest"

'Instantiate the connectors
oConn = New OleDbConnection(oConnect)
oComm = New OleDbCommand(oQuery, oConn)
oData = New OleDbDataAdapter(oQuery, oConn)

Try

'Open connection
oConn.Open()

'Fill dataset
oData.Fill(resultSet, "accessTest")

'Close connection
oConn.Close()

Catch ex As OleDb.OleDbException
Catch ex As Exception

'Show error message and exit
MsgBox(ex.Message & vbCrLf & ex.StackTrace)

Finally

'Dispose the connector objects
If Not (oConn Is Nothing) Then oConn.Dispose()
oConn = Nothing
If Not (oComm Is Nothing) Then oComm.Dispose()
oComm = Nothing
If Not (oData Is Nothing) Then oData.Dispose()
oData = Nothing

End Try

'Return results
Return resultSet

End Function


So far we've created some basic routines to create database/table and fetch data. Now it's time to concentrate on the main interface and come up with a way to display the fetched data on the screen.For this we'll utilize a DataGrid control, as it can be easily bound to the fetched data. On the main form, drop a DataGrid. Also add a DataSet to the main form. We're going to link these two controls and whenever we update the dataset with any data from the database, the datagrid will reflect the changes on screen. Likewise, when data is modified on screen, the changes get reflected in the actual database.
Continued in Part II ...



Hi,

please give me the advise, how to display the fetch records set.......i am very much thanks full to you.....

please please help me.......

Apurba

 

 

 


Reply

miCRoSCoPiC^eaRthLinG
Have neglected continuing this series for a long time.. will come up with a record fetching / saving tutorial asap.

Reply

vizskywalker
M^E! Haven't heard from you in forever. Just looked over this, and am looking forward to your next update, even though I don't use access or VB.NET. Should be simple enough to convert the methods here to any other .NET language though, such as C# (my favorite).

~Viz

Reply

iGuest
Sir/Madam,

I have sum problems but at present.
I know the how to create DataBase and Tables in MS Access 2000, But problem in table creation field yes/no looking as text. I want yes/no filed in as check box what can I write in table creation for this purpose.

Thank.

-K.Nageshwar Rao

Reply

iGuest
Part 2
VB.NET: MS-Access Interaction Tutorial (Part I)

Dear sir/ Madam

Part 1 was useful for my friend and I, and we'll waiting for part 2 to complete our exercises!
We'll be pleased if you would do us a favor and give us the rest of this tutorial.

Thanks in advance

Soudeh and Fariba

-Soudeh

Reply

iGuest
I really need Part 2
VB.NET: MS-Access Interaction Tutorial (Part I)

Hi,
That was really amazing, found myself understanding the code line by line. Could you please post the 2nd part or send it to me by email, that will be great.

Good to have people one can count on.

Forrest

PS : Thanks alot

-forrest

Reply

iGuest
VB.net and MSaccess
VB.NET: MS-Access Interaction Tutorial (Part I)

I'm using adodb and the following code is not picking up the entries in the database

If (textbox1.Text) = Rec.Fields("Username").Value then
Statement

How do I make it work?

-Clement

Reply

iGuest
I am using vb.Net 2005 as a front-End and Ms-Access 2000 (Mdb) database as a backend. The problem which I am facing is that.

Suppose I make a OledbTransaction and In that transaction I insert some rows into a table Named "Abc". After insertion is complete those Newly added rows are not available for further use unless I "commit" the transaction. I want that those rows should be available for further use after insertion without completing the transaction. (This happens in Sql-Server) what should I do for that.

Waiting for your response.

Thanking You
From : Kaushalendra Pandey

-Kaushaleandra Pandey

Reply

iGuest
Generating crystal report at run time
VB.NET: MS-Access Interaction Tutorial (Part I)

I want to generate crystal report which shows only those records which satisfies the given condition . How to do it ? I am using MS-Access as back end and vb.Net

-reply by Rajeshwar Rajemane

Reply

Latest Entries

iGuest
Replying to iGuest

USe the keystroke instance of the textbox and use the Asscii

-reply by Ivan

Reply

iGuest
HOW TO CREATE MS ACCESS DATABASE WITH VS2005 VB.NET
VB.NET: MS-Access Interaction Tutorial (Part I)

Pl provide me code and screenshot that how to create database in ms access by using vb.Net and how to store datas and access that datas

-question by ANAND

Reply

iGuest
Making Installer using MS SQL database
VB.NET: MS-Access Interaction Tutorial (Part I)

How can I include ms sql database in making vb.Net program as installer?.. How to change its connection string and dataset?

-reply by jhanz

Reply

iGuest
ms access database concept
VB.NET: MS-Access Interaction Tutorial (Part I)

I have made a table in access and when auto number is active data already inserted but iwant to ramove auto number and enter data without duplication.What is the complete process .

-question by ritesh

Reply

iGuest
Hello

Please solve my query.

How can I validate a textBox (that takes only numbers) in Windows application Like Phone number,Fax,Pincode etc. In C#?



-question by Upendra Pratap

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Pages: 1, 2
Recent Queries:-
  1. how to use ms acess database in vb.net - 0.47 hr back. (1)
  2. how to use ms access database in vb.net - 0.89 hr back. (1)
  3. vb.net access - 1.03 hr back. (1)
  4. connection string for ms-access in vb.net - 2.28 hr back. (1)
  5. how to connect vb.net 2008 to ms access - 3.15 hr back. (1)
  6. vb.net change sql query from sql to access - 3.35 hr back. (1)
  7. insert into boolean data type vb.net ms access - 5.50 hr back. (1)
  8. vb .net listview microsoft access - 5.57 hr back. (2)
  9. how to access a dataset in vb - 10.84 hr back. (1)
  10. vb.net ms access - 4.61 hr back. (2)
  11. vb.net access database update table example - 12.58 hr back. (1)
  12. vb.net access database example - 13.92 hr back. (1)
  13. connecting vb.net 2005 to ms access - 15.22 hr back. (1)
  14. vb.net, ms access, database - 15.27 hr back. (1)
Similar Topics

Keywords : vb net ms access interaction


    Looking for vb, net, ms, access, interaction, tutorial, part

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for vb, net, ms, access, interaction, tutorial, part
advertisement




VB.NET: MS-Access Interaction Tutorial (Part I)



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE