Tuesday, February 4, 2014

Hoe to Create/Establish a VBasic--MS Access Database Connection

STEPS
After Designing your form and naming your controls, follow the below steps:

**Double click on any area on your form to display the code window
Outside your class scope, import both system.Data and system.Data.oledb
~~~~~~~~~~~~~~~~~~~                                                                                  
Imports System.Data                                                                         
Imports System.Data.OleDb 
~~~~~~~~~~~~~~~~~~~                                                                                      
**Now, inside your class but outside any method/function, declare a new connection, command and connection string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~                                                            
Dim Conn As New OleDbConnection     
Dim Conn_command As New OleDbCommand                                           
Dim Conn_String = "   ' your connection string "                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~

**Next, under your forms load event, set and open the connection             ~~~~~~~~~~~~~~~~~~~~~~                                                                          
Conn.ConnectionString = Conn_String
Conn.Open()
~~~~~~~~~~~~~~~~~~~~~~                                                                                                       
**Lastly, set your command text, command type, connection AND execute your sql statement                                                                           
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Conn_command.CommandText = " Sql statement "
Conn_command.CommandType = CommandType.Text
Conn_command.Connection = Conn
Conn_command.ExecuteNonQuery()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Still don't get it, see example below
------------------------------
Windows form ~> frm1.vb
------------------------------














First name text box is named txtFirstName
Last name text box is named txtLastName
Address text box is named txtAddress
Phone text box is named txtPhone
Update button is named BtnUpdate

Source Code ~> Designed using vb 2010
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' import statements ~> Outside the class scope 
Imports System.Data
Imports System.Data.OleDb
' class begins
Public Class frm_main   
    
    ' NOW Inside the class BUT outside any method scope                                                                  Creat a new Oledb Connection = Access Dbase connection
    Dim Conn As New OleDbConnection

    ' Creat a new Oledb Command
    Dim Conn_command As New OleDbCommand

    'Connection String = The database connection string
    Dim Conn_String = "Provider=Microsoft.Jet.OLEDB.4.0;Data               Source=C:\DBaseFiles\Employee.mdb"
    Private Sub frm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Set the connection String
        Conn.ConnectionString = Conn_String

        'Open the connection 
        Conn.Open()
    End Sub

     ' Click event
    Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click

        Dim Query As String
        Query = "Insert into Emp_PersonalDetails values (' " & txtFirstName.Text & " ', ' " & txtLastName.Text & " ', ' " & txtAddress.Text & " ', ' " & txtPhone.Text & " ')"
        Conn_command.CommandText = Query
        'Set the command type
        Conn_command.CommandType = CommandType.Text
        'Set the connection
        Conn_command.Connection = Conn
        'Execute Query
        Conn_command.ExecuteNonQuery()

         ' Display message     
         Dim msg As String
        msg = MessageBox.Show("Record Updated Succesfully", "Employee DBase")

    End Sub
End Class

Source code screen shot
~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
Below is the database view after a record is added/updated:







Still don't get it, watch the below video 

No comments: