Working with Database in VB6.0 without using Data Control


Basically, the novice users will use the data control to connect the data base with VB6.0. There are many different ways to connect with the database in VB6.0

The objective of this post is to explain the easy way to connect to Access database without using data control. To do that follow the steps:

  1. Design a form as shown below:

  2. Open the Visual Data Manager from your VB Project

  3. The Visual Data Manager will appear on the screen

  4. Create a new Access Database

  5. Save the database in a location where you have saved your VB Project. (i.e. VB Project and the Database should be in the same folder-only for this post)

  6. Right click on the database window and click “Create Table”

  7. Give a name for your table

  8. Then click “Add Field” Button to add the fields for your table

  9. Once you created all the fields, click OK button. Then click “Build the Table” Button.

  10. Once the table is created, click the “Dynaset type Recordset” , the second icon in the toolbar.

  11. Then click the “Use Data Control on New Form” , the fourth icon in the toolbar.

  12. The double click on the “stu”, table icon in the Database window. And you will get following input screen.

  13. Now, you can add data to your table. Once you entered press the “Close” Button.

Coding Part:

  1. Go to the General declaration area of your coding window. In that type the following:

    Dim dbStu As Database

    Dim rsStu As Recordset

    Dim Add As String

  2. Go to the Form_Load() event. In that type the following:

    Private Sub Form_Load()

    Set dbStu = OpenDatabase(App.Path & “\student.mdb”)

    Set rsStu = dbStu.OpenRecordset(“Select * from stu “)

    End Sub

  3. Go to the Click() event of the AddNew Button and type the following:

    Add = “Insert into stu values(‘” & Text1 & “‘, ‘” & Text2 & “‘,’” & Text3 & “‘,” & Val(Text4) & “,” & Val(Text5) & “)”

    dbStu.Execute Add

    Call Clear

  4. Go to the Click() event of the Update Button and type the following:

    Set rsStu = dbStu.OpenRecordset(“Select * from stu where rno = ‘” & Text1 & “‘”) ‘Search the stu table for given roll number in the Text1 text box.

    If rsStu.RecordCount = 0 Then

    MsgBox “No Record Found”

    Else

    rsStu.Edit     ‘Activating Edit Mode

    rsStu(0) = Text1

    rsStu(1) = Text2

    rsStu(2) = Text3

    rsStu(3) = Val(Text4)

    rsStu(4) = Val(Text5)

    rsStu.Update

    Call Clear

    End If

    If Not rsStu Is Nothing Then

    Set rsStu = Nothing

    End If

  5. Go to the Click() event of the Delete button and type the following:

    rsStu.Delete

    Call Clear

  6. Go to the Click() event of the Find button and type the following:

    strName = InputBox(“Enter Student Name…”, “Input Required”)

    Set rsStu = dbStu.OpenRecordset(“Select * from stu where name = ‘” & strName & “‘”)

    If rsStu.EOF = True Then

    MsgBox “No Record Found”

    Else

    Text1 = rsStu(0)

    Text2 = rsStu(1)

    Text3 = rsStu(2)

    Text4 = rsStu(3)

    Text5 = rsStu(4)

    End If

    If Not rsStu Is Nothing Then

    Set rsStu = Nothing

    Else

    rsStu.Close

    End If

  7. Go to the Click() event of the Clear Button and type the following:

    Call Clear

    This will call the Clear function, which should be defined first. The code is given below:

    Sub Clear()

    Text1.Text = “”

    Text2.Text = “”

    Text3.Text = “”

    Text4.Text = “”

    Text5.Text = “”

    If Not rsStu Is Nothing Then

    Set rsStu = Nothing

    Else

    rsStu.Close

    End If

    End Sub

  8. Go to the Click() event of the MoveFirst button and type the following:

    If rsStu Is Nothing Then

    Set rsStu = dbStu.OpenRecordset(“Select * from stu “)

    End If

    rsStu.MoveFirst

    Call LoadText

  9. Go to the Click() event of the MovePrevious button and type the following:

    If rsStu Is Nothing Then

    Set rsStu = dbStu.OpenRecordset(“Select * from stu “)

    End If

    On Error GoTo X:

    rsStu.MovePrevious

    If rsStu.BOF = False Then

    Call LoadText

    Else

    MsgBox “Reached First Record of the Table”, vbInformation, “First Record”

    End If

    X:

    ‘Display the error

  10. Go to the Click() event of the MoveNext button and type the following:

    On Error GoTo X:

    If rsStu Is Nothing Then

    Set rsStu = dbStu.OpenRecordset(“Select * from stu “)

    End If

    rsStu.MoveNext

    If rsStu.EOF = False Then

    Call LoadText

    Else

    MsgBox “Reached Last Record of the Table”, vbInformation, “Last Record”

    End If

    X:

    ‘Display the error

  11. Go to the Click() event of the MoveLast button and type the following:

    If rsStu Is Nothing Then

    Set rsStu = dbStu.OpenRecordset(“Select * from stu “)

    End If

    rsStu.MoveLast

    Call LoadText

  12. Create the LoadText() function with the following code:

    Text1 = rsStu(0)

    Text2 = rsStu(1)

    Text3 = rsStu(2)

    Text4 = rsStu(3)

    Text5 = rsStu(4)

  13. Press F5 to execute the program.
About these ads

Follow

Get every new post delivered to your Inbox.

Join 71 other followers