Total Pageviews

Saturday, 22 October 2016

How to connect to Data file(access database) in vb6.0


1. Start a new project

2. Before you can connect to a data file you need add two controls in your form ADODC AND DATAGRIDVIEW

  Place your cursor on the tools box and right-click and select components ,the components window will open
  and you find and check ADODC and DATAGRIDVIEW and click on apply to add the controls to you components list

3. Drag and drop the controls to your form you should have it look like this

Save the project to a directory name it  NaeateExpense


4. The next thing you have to do is create your data file ,for this project we i will be using access databse

5.Save the file 2002 /2003 format the extension will be .mdb eg my file name is  naeate.mdb
5. Lets get back to the project 
Now that you create your datafile copy it to the project directory 
C:\Program Files\Microsoft Visual Studio\VB98\NaeateExpense
or 
 C:\Program Files(x86)\Microsoft Visual Studio\VB98\NaeateExpense
6. Go to the to the form  now we are to create the connection string 
right click on ADODC and go to properties

next the property page appears  



Click on build to create the connection 

Browse to the data file and select  it,Click on test connection you should see on the figure below 

Now get back to property page  and click on Recordsource
7.Right click on Datagridview  and  click on properties ,you should see this figure below then check the boxes as seen below

8.Add Three buttons and name them cmdCalculate,cmdDelete,cmdAddnew 
,add four textboxes  , add four labels on each text box and named them accordingly 
This how your form will look like 
Add this code below to you project

Private Sub CmdCalculate_Click()
 With Adodc1.Recordset
  .Fields(4) = .Fields(1) + .Fields(2) + .Fields(3)

End With
End Sub

Private Sub CmdDelete_Click()

With Adodc1.Recordset
  .Fields(4) = Null
End With
End Sub

Private Sub CmdAddnew_Click()
With Adodc1.Recordset
.AddNew
.Fields(0) = Text1.Text
.Fields(1) = Text2.Text
.Fields(2) = Text3.Text
.Fields(3) = Text4.Text

End With
End Sub

Run the program if there is no errors in your code you will be able to add new record ,calculate total ,clear 

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is really helpful...
    Much thanks for that.

    ReplyDelete