FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Access DataBase using visual basic6





hamzasoft
Database programming made easy
by Hamza Tassaddaq


This tutorial will show you "Step by Step" how to create an Access DataBase, Link to it with AdoDC and link your controls to the DataBase. If this tutorial helps you then PLEASE vote! This will let me know if anyone is benefiting from it. If there are any other tutorials that you would like to see, just let me know.

In this tutorial we will be creating a simple address book.

First Step - Create the DataBase

Open Visual Basic, and create a new project. Under "Add-Ins" select "Visual Data Manager"
In this window select "File" then "New" then "Microsoft Access" and select the latest DataBase. In my case it is the "Version 7.0 mdb". Name the new DataBase "MyContacts.mdb" and click save.
The "Database Window" should now be open. Right Click on "Properties" and select "New Table". In the "Table Structure" window on "Table Name" type in "Contacts".
In the "Field List" select "Add Field".
In this window in the "Name" space type in "cName" and click OK
then type in "cAddress" and click OK
then type in "cEmail" and click OK
then type in "cTelephone" and click OK
Now click Close
Now click "Build the Table". That's it! Your table has been created. You can now close this window.

Add the controls

The controls you will need are the Microsoft Ado Data Control 6.0 or Later and the Microsoft DataGrid Control 6.0 or later. 4 TextBoxes and 4 Labels. For simplicity, I will keep the default control names. Add these controls to your form. Set the AdoDC1 properties EOFAction to 3-adDoAddNew
This will allow you to add new records when you reach the end of the DataBase.
Set the DataGrid1 properties DataSource to AdoDC1. This will link the DataGrid to the AdoDC.

Link the controls

Link the AdoDC control with our DataBase. To do this, right-click over the AdoDC in our form and select AdoDC Properties.

Select the "Use connection string" Under the "General" tab. Click "Generate"
This will pop-up a second dialog box. Under the "Provider" tab we will select the type of Database we are using. Here we will be using an Access DataBase, So click the "Microsoft Jet 4.0 OLE DB Provider" option and click Next.

You should now be on the "Connection" tab. On the first option "Select or enter a database name:" find the database that you created earlier, if you used the same name as in this tutorial it will be "MyContacts.mdb". Click the "Test Connection" button just to make sure you are linked. Click OK

You should now be back in the first dialog box. Select the "RecordSource" tab. Under "Command Type" select "2- adCmdTable". This simply lets the control know that we want to import a table from our database. Next, under "Table or stored procedure name" we must select the name of the table we want to import. In this case it will be "Contacts", the name of our table. When done Click Ok.

Connect the AdoDC to your controls

For each TextBox we must edit the following properties:
note: connect the "DataSource" first for simplicity
Text1:
DataSource: Adodc1 (name of the control that we linked with the database)
DataField: cName (name of the field that we want to link to this control)
Label this as "Name" with Label1
Text2:
DataSource: Adodc1 DataField: cAddress - Label this as "Address" - with Label2
Text3:
DataSource: Adodc1 DataField: cEmail - Label this as "Email" - with Label3
Text4:
DataSource: Adodc1 DataField: cTelephone - Label this as "Telephone #" with Label4

For the "DataGrid" control set the "DataSource" to "Adodc1"
The default numbr of columns for this control is two. To add more columns, right click the control and select "Edit". Right click again at the top of the DataGrid where the description for each cell is placed and select "Insert". Do this until you have four cells.
Right Click the "DataGrid" control and select "Properties". Under the "Columns" tab select
"Column 0" and in the "Caption" box type in "Name" and in the "Data Field" select "cName"
"Column 1" in the "Caption" box type in "Address" in the "Data Field" select "cAddress"
"Column 2" in the "Caption" box type in "Email" and in the "Data Field" select "cEmail"
"Column 3" in the "Caption" box type in "Telephone#" in the "Data Field" select "cTelephone"

Your Done! Run and test your program. You will get an error message if you try to add a blank record into the DataBase. This is not a problem, it just wont save a blank record.
Related topics
C++ Microsoft Access Database
Access denied as Admin in phpNuke
HOW TO USE ADO TO WORK WITH ACCESS DATABASE IN VB
Converting Access database to SQL server 2005
Using MySQL from DirectAdmin
ms access and visual basic
Microsoft ACCESS 2003
The Game creation topic! - Share experience - Find resources
Access database from other site ???
is it possible to connect to Access Database using PHP?
excel database
How do you Create a New Database using a php script?
i tried access frihost using my blackberry browser
MySQL + Access Database Software
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.