Point of Sale in Visual Basic (Part 1)

Next

Hello tyrodeveloper's, here is my first english publication, i hope that you like.

Requirements:
  • Microsoft Visual Studio 2010
  • Microsoft Access 2007/2010

Data Base Design
We need to create a database like is showed in the picture:


Project creation
We create a project called “PointOfSale”:

Here is the project organization:

We have three folders. Add two forms to the “Forms” folder and a module in the “Modules” folder like is showed in the picture.

Login Form (frmLogin)

Add this “Imports” directives:
Imports System.Data
Imports System.Data.OleDb

Add this Variables:
    ''Variables
    Dim Attempts As Integer = 0
    Public Shared UserLogin As String
    Public Shared FirstName As String
    Public Shared LastName As String
    Public Shared Sales As Boolean
    Public Shared Reporting As Boolean
    Public Shared Management As Boolean
    Public Shared Catalog As Boolean
    Public Shared Access As Boolean

Open the “modMain.vb” module and add this code:
Module modMain
    Public CnnStr As String =
     "Provider=Microsoft.ACE.OLEDB.12.0;" & _
           "Data Source=C:\Users\ddi-gcastillo\" & _
           "Documents\tyrodeveloper\PointOfSale\PointOfSale.accdb;" & _
           "Persist Security Info=False;"
    Sub Main()
        Dim loginFrm As New frmLogin
        loginFrm.StartPosition = FormStartPosition.CenterScreen
        Application.Run(loginFrm)
        If (frmLogin.Access = True) Then
            Dim mainFrm As New mdiMain
            mainFrm.StartPosition = FormStartPosition.CenterScreen
            mainFrm.WindowState = FormWindowState.Maximized
            Application.Run(mainFrm)
        Else
            Application.Exit()
        End If
    End Sub
End Module

Design the login form like this:

On the “frmLogin.vb” form, add this function:
    Protected Function Login() As Boolean
        Dim cnn As New OleDbConnection(CnnStr)
        Try
            cnn.Open()
            Dim cmd As New OleDbCommand
            cmd.Connection = cnn
            cmd.CommandText = "SELECT * FROM USERS " & _
                " WHERE USER_LOGIN=@user_login " & _
                " AND USER_PASSWORD=@user_password"
            cmd.Parameters.Add("@user_login",
                OleDbType.VarChar, 50).Value = txtUserLogin.Text
            cmd.Parameters.Add("@user_password",
                OleDbType.VarChar, 255).Value = txtUserPassword.Text
            Dim dr As OleDbDataReader = cmd.ExecuteReader
            If dr.Read Then
                UserLogin = dr("USER_LOGIN").ToString()
                FirstName = dr("FIRST_NAME").ToString()
                LastName = dr("LAST_NAME").ToString()
                Sales = CBool(dr("SALES"))
                Reporting = CBool(dr("REPORTING"))
                Management = CBool(dr("MANAGEMENT"))
                Catalog = CBool(dr("CATALOG"))
            Else
                Attempts += 1
                Throw (New Exception( _
                 "Username or password incorrect"))
            End If
            dr.Close()
            Return True
        Catch ex As Exception
            Throw (ex)
        Finally
            cnn.Close()
        End Try
    End Function

And here is the code for the “Ok” button:
    Private Sub btnOk_Click(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) _
                    Handles btnOk.Click
        Try
            If (Attempts >= 3) Then
                Access = False
                MessageBox.Show("Attempts exceeded", _
                 "System Information", _
                 MessageBoxButtons.OK, _
                 MessageBoxIcon.Error)
                Me.Close()
            Else
                Access = Login()
                If (Access) Then
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message,
            "System Information", _
            MessageBoxButtons.OK,
            MessageBoxIcon.Error)
        End Try
    End Sub

Go to the project configuration and set:

1 comentario: