1. Saving information to the database.

Before you begin, make sure Microsoft Visual Basic 6.0 is already installed.
Steps
1. On your disk drive storage, create the folders as shown below:
2. On start menu, click VB 6.0, and choose Standard.exe and click open.
3. On Project Explorer rename Project1 to Sales, and Form1 to CustomerUI.
4. Save CustomerUI.frm to Forms Folder and Sales.vbp to Sales Folder.

Creating User Interface
5. Add controls to the form and should be look like this.
4. Select each object and change its name property on the properties window.
Objects-Name
Form-CustomerUI
Textboxes
ID-txtID
Name-txtName
Address-txtAddress
ContactNo-txtContactNo
CreditLimit-txtCreditLimit
Buttons
Save-btnSave
Reset-btnReset
Close-btnClose

Creating Database
5.To create a database, on the menu bar, click Add-ins->Visual Data Manager
6.On VisData, click file->New->Microsoft Access->Version 7.0
7.Look for Database folder(refer to step 1) and save the database with the filename-SALES.mdb
8.In the SQL Statement window, type the following:


CREATE TABLE Customers
(
id integer,
name text(50),
address text(70),
contactno text(11),
creditlimit currency
)
9.Click EXECUTE button, just answer NO for the dialog box that will appear.
10.In the whitespace of the Database Window, right click->refresh. You should see like this.


11. Close Visual Data Manager


Project Reference for MS Access Database
12. On the menu bar, click Project->references
13. Check Microsoft ActiveX Data Objects 2.6 Library

Organizing files and writing codes
14. On Project Explorer Window, right click Sales.vbp
15. Select Add->Module->open
16. On the properties window, rename Module1 to dbs
17. On the toolbar, click save button and save dbs to Helpers folder.
18. On dbs window, type the following:


Option Explicit
Public db As New Connection

Public Sub OpenConnection()

    If db.State = 1 Then db.Close()

db.ConnectionString = "Provider=Microsoft.Jet.OLEDb.4.0;Data Source ="& App.Path & "\Database\SALES.mdb"

    db.Open(db.ConnectionString)

End Sub

Public Sub Execute(ByVal sql As String)
  db.Execute(sql)
End Sub

19. On Project Explorer Window, right click Sales.vbp
20. Click Add->Modules->open
21. On the properties window, rename Module1 to AppToolBox
22. On the toolbar, click save button and save AppToolBox to Helpers folder.
23. On AppToolBox window, type the following:


Public Sub Reset(ByVal obj As Object)
    Dim ndx As Control
    With obj
        For Each ndx In obj
            If TypeOf ndx Is TextBox Then
                ndx = Empty
            End If
        Next
    End With
End Sub


Public Function IsEmpty(ByVal obj As Object) As Boolean
    IsEmpty = False
    Dim ndx As Control
    With obj
        For Each ndx In obj
            If TypeOf ndx Is TextBox Then
                If ndx = Empty Then
                    IsEmpty = True
                    Exit Function
                End If
            End If
        Next
    End With
End Function


Public Sub Message(ByVal msg As String)
    MsgBox(msg, vbInformation, "Message")
End Sub


24. Click Add->Class Modules->open
25. On the properties window, rename Class1 to Customers
Sidenote: Your Folder Structure on the Project explorer should look like this:
26. On the class module Customers, type the following:

Option Explicit
Private mID As Integer
Private mName As String
Private mAddress As String
Private mContactNo As String
Private mCreditLimit As Currency

Public Property Let ID(ByVal vNewValue As Integer)
mID = vNewValue
End Property

Public Property Get ID() As Integer
ID = mID
End Property

Public Property Get Name() As String
Name = mName
End Property

Public Property Let Name(ByVal vNewValue As String)
mName = vNewValue
End Property

Public Property Get Address() As String
Address = mAddress
End Property

Public Property Let Address(ByVal vNewValue As String)
mAddress = vNewValue
End Property

Public Property Get ContactNo() As String
ContactNo = mContactNo
End Property

Public Property Let ContactNo(ByVal vNewValue As String)
mContactNo = vNewValue
End Property

Public Property Get Creditlimit() As Currency
Creditlimit = mCreditLimit
End Property

Public Property Let Creditlimit(ByVal vNewValue As Currency)
mCreditLimit = vNewValue
End Property

Private Sub Class_Initialize()
    dbs.OpenConnection()
End Sub

Public Sub Save()
    Dim sql As String
    sql = "INSERT INTO Customers " _
            & "(" _
            & "id," _
            & "name," _
            & "address," _
            & "contactno," _
            & "creditlimit" _
            & ")" _
            & "VALUES" _
            & "(" _
            & "'" & ID & "'," _
            & "'" & Name & "'," _
            & "'" & Address & "'," _
            & "'" & ContactNo & "'," _
            & "'" & Creditlimit & "'" _
            & ")"
    dbs.db.Execute(sql)
End Sub


27. On CustomerUI, double click Save button and start typing: if AppToolbox.....:

Private Sub btnSave_Click()
    If AppToolBox.IsEmpty(Me) Then
        AppToolBox.Message("Check for empty textbox")
        Exit Sub
    End If

    If IsNumeric(Me.txtID.Text) = False Or _  
       IsNumeric(Me.txtCreditLimit.Text) = False Then
        AppToolBox.Message("ID/Creditlimit should be numeric.")
        Exit Sub
    End If

    Dim customer As Customers
    customer = New Customers
    With customers
        .ID = CInt(Me.txtID.Text)
        .Name = Me.txtName.Text
        .Address = Me.txtAddress.Text
        .ContactNo = Me.txtContactNo.Text
        .Creditlimit = CCur(Me.txtCreditLimit.Text)
        .Save
    End With

    AppToolBox.Message("Successfully Save...")
    AppToolBox.Reset(Me)
    Me.txtID.SetFocus()
End Sub


28. Run your program by clicking start button on the toolbar.

Testing your code
29. Click save while all or other textbox is empty, notice what happen.
30. This time input all textboxes with valid values, and click save.
31. To check the data you inputted if it is really stored:
       31.1 Go to Add-Ins->Visual Data Manager->File->Open->Microsoft Access
       31.2  Locate your database on the database folder, click open.
       31.3 Double click customers table, you should see the data you input on the form.

Debugging your code
-To familiarize the behaviour of the language or to know how your codes are executed in sequence, debugging should be done. In visual basic, just put a break point by clicking at the left margin of the procedure or function and then press F8. To break the debugging mode, just click the end button at the right of the start button in the toolbar and to remove the break point, just click the break point to toggle.



1 comment:

  1. Ahh... I have a question sir.. mmm i tried your program in dbs program in
    Option explicit
    Public db As New Connection

    when i run the program the compiler said
    compile error : invalid use of new keyword

    i tried to debug but still error

    ReplyDelete