Im having a problem here, i dont know where to start, im just a begginner i already finished my window forms in visual studio and now i want to connect it to my ms access database, i dont know what to do. Please help.

2 Years
Discussion Span
Last Post by Santanu Das

No one have sufficient time to teach a new one. Read books, journals or from MSDN and also learn form teaching concerns and socities. Forum members can help you to correct your learnings and ways by making some fruitful tips. Achievement is your concern.

Now I am describing here how to Connect,Open and Close an Access Database, through a simple codification.

Imports System.Data.OleDb

Public Class Form1

    'Declaring variables
    Dim MyConn As New System.Data.OleDb.OleDbConnection
    Dim dirty As Boolean = False

    Private Sub OpenConnection()
        'Checking here, whether Database Connection is open or not
        'If it is opened then try to close it
        If MyConn.State = ConnectionState.Open Then MyConn.Close()

        'Assigning ConnectionString Property
        MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyProject\XXX.mdb"

        'Open it

        dirty = True
    End Sub

    Private Sub OpenConnectionButton_Click(sender As System.Object, e As System.EventArgs) Handles OpenConnectionButton.Click
        'Open the connection
        Select Case MyConn.State
            Case ConnectionState.Closed : Me.OpenConnection()
            Case Else : MsgBox("Connection is already opened.")
        End Select

    End Sub

    Private Sub CloseConnectionButton_Click(sender As System.Object, e As System.EventArgs) Handles CloseConnectionButton.Click
        Select Case MyConn.State
            Case ConnectionState.Open : MyConn.Close()
            Case Else : MsgBox("Connection is already closed.")

        End Select
        dirty = False
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Checking Finally at the time of closing the form
        If dirty Then
            Select Case MessageBox.Show("Do you sure to close the connection?", "Connection...", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                Case Windows.Forms.DialogResult.OK
                    dirty = False
                    e.Cancel = False
                Case Else
                    e.Cancel = True
            End Select
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.