You are on page 1of 3

9/12/13

How to use Microsoft Visual Basic .NET to connect to a Microsoft Access database and to retrieve data

Find it myself Ask the community Get live help

Select the product you need help with

Windows

Internet Explorer

Office

Surface

Xbox

Skype

Windows Phone

More products

How to use Microsoft Visual Basic .NET to connect to a Microsoft Access database and to retrieve data
Article ID: 821765 - View products that this article applies to.

SUMMARY
This step-by-step article describes how to use Microsoft ADO.NET to open a Microsoft Access database by using the OLE DB data provider. You use the OleDbDataAdapter class to obtain rows from the Access database and to insert the rows into a DataSet object. This article describes how to add rows, how to delete rows, and how to update rows in the Access database by using DataSet and OleDbDataAdapter.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required: Microsoft .NET Framework Microsoft Visual Basic .NET Microsoft Access This article assumes that you are familiar with the following topics: Visual Basic .NET ADO.NET Access

Create a Microsoft Access database


To create a database in Access and then create a table in the database, follow these steps: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. Start Access. On the File menu, click New. Click Blank database on the task pane. In the File name box, type testdb, and then click Create. Right-click Create table in Design view, and then click Open. In the Field Name text box, type SName. In the Data Type list, click Text. In the Field Name text box, type SNo. In the Data Type list, click Number. Right-click SNo, and then click Primary Key. On the File menu, click Save As. In the Save As dialog box, type Student in the text box and then click OK. Close the design view. Right-click the Student table, and then click Open. Type ABC in the SName column. Type 101 in the SNo column. Type XYZ in the SName column. Type 102 in the SNo column. Type several more records in the Student table, and then close the Student:Table window. Close the testdb database.

Create a connection to the Access database by using Visual Basic .NET


The following step-by-step example describes how to create a connection to the Access database by using the Microsoft Visual Studio .NET Server Explorer. The following example also describes how to use the OleDbDataAdapter class to retrieve data from the database and to insert data into a DataSet object. This example also describes how to create new rows, how to add these rows to the table, how to modify the data in the rows, and how to remove rows from the table in the Access database.

support.microsoft.com/kb/821765

1/3

9/12/13

How to use Microsoft Visual Basic .NET to connect to a Microsoft Access database and to retrieve data
Create a Windows application in Visual Basic .NET 1. 2. 3. 4. Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects. Under Templates, click Windows Application, and then click OK. By default, Form1 is created. Open a connection to the Access database 1. 2. 3. 4. 5. 6. 7. 8. On the View menu, click Server Explorer. In Server Explorer, right-click Data Connections, and then click Add Connection. In the Data Link Properties dialog box, click the Provider tab. In the OLE DB Provider(s) list, click Microsoft Jet 4.0 OLE DB Provider, and then click Next. Click the Connection tab, and then click the ellipses button (... ). Locate the Access database testdb.mdb file that you created by following the corresponding path on your computer. Select the testdb.mdb file, and then click Open. In the Data Link Properties dialog box, click OK.

Retrieve data from the Access database by using the OleDbDataAdapter class 1. 2. 3. 4. On the toolbox, click the Data tab. Drag an OleDbDataAdapter control to Form1. In the Data Adapter Configuration Wizard, click Next three times. In the Generate the SQL statements panel, type the following Microsoft SQL Server statement, and then click Next: S e l e c t*f r o mS t u d e n t 5. In the View Wizard Results panel, click Finish. Note In the Do you want to include the password in the connection string? dialog box, click Don't include password. 6. Right-click OleDbDataAdapter1, and then click Generate Dataset. 7. In the Generate Dataset dialog box, click OK. 8. Add the following code to the Form1_Load event handler: ' F i l lr e t r i e v e sr o w sf r o mt h ed a t as o u r c eb yu s i n gt h eS E L E C Ts t a t e m e n tO l e D b D a t a A d a p t e r 1 . F i l l ( D a t a S e t 1 1 )

Display records that are retrieved from the Access database


1. Add a DataGrid control to Form1. By default, DataGrid1 is created. 2. Right-click DataGrid1, and then click Properties. 3. In the Properties dialog box, set the DataSource property to DataSet11 and set the DataMember property to Student.

Add a row to a table in the Access database


1. 2. 3. 4. Add a Button control to Form1. Right-click Button1, and then click Properties. In the Properties dialog box, set the Text property to Add. Add the following code to the Button1_Click event handler: D i mi ,s n oA sI n t e g e rD i ms n a m eA sS t r i n gD i mr wA sD a t a R o w' A d dan e wr o wt ot h eS t u d e n tt a b l e .r w= D a t a S e t 1 1 . T a b l e s ( 0 ) . N e w R o ws n o=I n p u t B o x ( " E n t e rt h eR o l ln oo ft h eS t u d e n t : " )s n a m e=I n p u t B o x ( " E n t e rt h eN a m eo ft h e S t u d e n t : " )r w . I t e m ( " S N o " )=s n or w . I t e m ( " S N a m e " )=s n a m eT r yD a t a S e t 1 1 . T a b l e s ( 0 ) . R o w s . A d d ( r w )' U p d a t et h eS t u d e n tt a b l ei n t h et e s t d bd a t a b a s e .i=O l e D b D a t a A d a p t e r 1 . U p d a t e ( D a t a S e t 1 1 )C a t c he xA sE x c e p t i o nM e s s a g e B o x . S h o w ( e x . M e s s a g e )E n dT r y ' D i s p l a y sn u m b e ro fr o w su p d a t e d .M e s s a g e B o x . S h o w ( " n oo fr o w su p d a t e d = "&i )

Update the rows of a table in the Access database


1. Add a Button control to Form1. By default, Button2 is created. 2. Right-click Button2, and then click Properties. 3. In the Properties dialog box, set the Text property to Update. 4. Add the following code to the Button2_Click event handler: D i mi ,r w n oA sI n t e g e rD i mc o l n a m eA sS t r i n gD i mv a l u eA sO b j e c tc o l n a m e=I n p u t B o x ( " E n t e rt h en a m eo ft h eC o l u m nt ob e u p d a t e d " )r w n o=I n p u t B o x ( " E n t e rt h eR o wN u m b e rt ob eu p d a t e d :R o wN os t a r t sf r o m0 " )v a l u e=I n p u t B o x ( " E n t e rt h ev a l u et o b ee n t e r e di n t ot h eS t u d e n tt a b l e " )T r y' U p d a t et h ec o l u m ni nt h eS t u d e n tt a b l e . D a t a S e t 1 1 . T a b l e s ( 0 ) . R o w s ( r w n o ) . I t e m ( c o l n a m e )=v a l u e' U p d a t et h eS t u d e n tt a b l ei nt h et e s t d bd a t a b a s e .i= O l e D b D a t a A d a p t e r 1 . U p d a t e ( D a t a S e t 1 1 )C a t c he xA sE x c e p t i o nM e s s a g e B o x . S h o w ( e x . M e s s a g e )E n dT r y' D i s p l a y sn u m b e ro fr o w s u p d a t e d .M e s s a g e B o x . S h o w ( " n oo fr o w su p d a t e d = "&i )

support.microsoft.com/kb/821765

2/3

9/12/13

How to use Microsoft Visual Basic .NET to connect to a Microsoft Access database and to retrieve data

Delete rows from a table in the Access database


1. Add a Button control to Form1. By default, Button3 is created. 2. Right-click Button3, and then click Properties. 3. In the Properties dialog box, set the Text property to Delete. 4. Add the following code to the Button3_Click event handler: D i miA sI n t e g e rD i mr n oA sI n t e g e rr n o=I n p u t B o x ( " E n t e rt h eR o wn ot ob ed e l e t e d :R o wn os t a r t sf r o m0 " )T r y' D e l e t ea r o wf r o mt h eS t u d e n tt a b l e .D a t a S e t 1 1 . T a b l e s ( 0 ) . R o w s ( r n o ) . D e l e t e ( )' U p d a t et h eS t u d e n tt a b l ei nt h et e s t d bd a t a b a s e .i= O l e D b D a t a A d a p t e r 1 . U p d a t e ( D a t a S e t 1 1 )C a t c he xA sE x c e p t i o nM s g B o x ( e x . M e s s a g e )E n dT r y' D i s p l a y sn u m b e ro fr o w su p d a t e d . M e s s a g e B o x . S h o w ( " n oo fr o w su p d a t e d = "&i )

Verify that it works


1. On the Build menu, click Build Solution. 2. On the Debug menu, click Start. 3. Click Add, and then type the data in the input box to add a row to the Student table. Note: You receive an error if you click Cancel in the input boxes. 4. Click Update, and then type the data in the input boxes to update a column in the Student table. 5. Click Delete, and then type the data in the corresponding input boxes to delete a row from the Student table.

REFERENCES
For more information, visit the following Microsoft Developer Network (MSDN) Web sites: OleDbDataAdapter Class http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter(vs.71).aspx (http://msdn2.microsoft.com/enus/library/system.data.oledb.oledbdataadapter(vs.71).aspx)

Accessing Data with ADO.NET http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)

Properties
Article ID: 821765 - Last Review: May 16, 2007 - Revision: 1.4 APPLIE S TO
Microsoft Visual Basic .NET 2003 Standard Edition Microsoft Visual Basic .NET 2002 Standard Edition

K eywo r d s : kbtable kbhowtomaster kbhowto kbsystemdata kbdatabase kbdataadapter KB821765

Give Feedback
Back to the top

support.microsoft.com/kb/821765

3/3

You might also like