Visual Studio 2022 | Visual Basic .NET | Connect to data in an Access Database stored in Google Drive





Visual Studio 2022 | Visual Basic .NET | Connect to data in an Access Database stored in Google Drive.
สอนฟรี การเขียนโปรแกรม Visual Basic .NET เชื่อมต่อกับฐานข้อมูล Microsoft Access ที่เก็บไว้บน Google Drive!

::: Visual Basic .NET Source Code by iBasskung :::

Connection strings stored in application settings:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\My Drive\Database\Your_Database.accdb;Persist Security Info=False;

Connection strings are stored in the connectionStrings section of the configuration element of an application configuration file (App.config).

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="AccessDb_Connection.My.MySettings.Your_DatabaseConnectionString"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\My Drive\Database\Your_Database.accdb;Persist Security Info=False;"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
</configuration>


Form1:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Your_DatabaseDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.Your_DatabaseDataSet.Table1)

        With DataGridView1
            .ClearSelection()
            .ReadOnly = True
            .MultiSelect = False
        End With

        Dim colors() As String = [Enum].GetNames(GetType(KnownColor))

        cboDfColor.Items.AddRange(colors)
        cboAltColor.Items.AddRange(colors)

        Me.reset()

    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Table1BindingSource.MovePrevious()
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        Table1BindingSource.AddNew()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Table1BindingSource.MoveNext()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        On Error GoTo SaveErr
        Table1BindingSource.EndEdit()
        Table1TableAdapter.Update(Your_DatabaseDataSet.Table1)

ErrEx:
        Exit Sub
SaveErr:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrEx

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If MessageBox.Show("Are you sure", "Delete Record", MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = _
                       Windows.Forms.DialogResult.Yes Then
            Table1BindingSource.RemoveCurrent()
        End If

    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

        Dim msg As String = "Do you want to Exit?"
        Dim title As String = "Exit Application"

        If MessageBox.Show(msg, title, MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = _
                       Windows.Forms.DialogResult.Yes Then
            Me.Close() 'End
        End If

    End Sub
    
    Private Sub cboDfColor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDfColor.SelectedIndexChanged
        cboDfColor.BackColor = Color.FromName(cboDfColor.SelectedItem)
        txtSearch.Select()
    End Sub

    Private Sub cboAltColor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAltColor.SelectedIndexChanged
        cboAltColor.BackColor = Color.FromName(cboAltColor.SelectedItem)
        txtSearch.Select()
    End Sub

    Private Sub lblReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblReset.Click
        On Error GoTo ErrRe
        txtSearch.Select()
        Table1BindingSource.Filter = Nothing


        With DataGridView1
            .ClearSelection()
            .ReadOnly = True
            .MultiSelect = False
            .DataSource = Table1BindingSource
        End With

        Me.reset()

ErrEx:
        Exit Sub
ErrRe:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrEx
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Try

            If txtSearch.Text = "" Then
                Call notFound()
                Exit Sub
            Else

                Dim cantFind As String = txtSearch.Text
                Me.dgvFill()

                Table1BindingSource.Filter = "(Convert(ID, 'System.String') LIKE '%" & txtSearch.Text & "%')" &
                    "OR (YourName LIKE '%" & txtSearch.Text & "%') OR (Lastname LIKE '%" & txtSearch.Text & "%')" &
                    "OR (Phone LIKE '%" & txtSearch.Text & "%') OR (Email LIKE '%" & txtSearch.Text & "%')" &
                    "OR (Facebook LIKE '%" & txtSearch.Text & "%')"

                If Table1BindingSource.Count <> 0 Then
                    With DataGridView1
                        .DataSource = Table1BindingSource
                    End With

                Else
                    Me.notFound()
                    MsgBox("--> " & cantFind & vbNewLine &
                           "The search item was not found.",
                           MsgBoxStyle.Information, "Hey Boss!")

                    Table1BindingSource.Filter = Nothing
                    With DataGridView1
                        .ClearSelection()
                        .ReadOnly = True
                        .MultiSelect = False
                        .DataSource = Table1BindingSource
                    End With
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Search Error: " & ex.Message, "Google Drive Search : iBasskung.", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub


    Private Sub reset()

        Dim txtS As TextBox = txtSearch
        With txtS
            .Text = ""
            .Select()
            .BackColor = Color.LightCyan
        End With

        cboDfColor.SelectedItem = "MistyRose"
        cboAltColor.SelectedItem = "Gold"

        cboDfColor.BackColor = Color.LightCyan
        cboAltColor.BackColor = Color.LightCyan

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If


        Dim dgv1 As DataGridView = DataGridView1
        With dgv1
            .RowsDefaultCellStyle.BackColor = Color.FromName(cboDfColor.SelectedItem)
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FromName(cboAltColor.SelectedItem)
        End With

    End Sub


    Private Sub dgvFill()

        txtSearch.BackColor = Color.LightBlue

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If


        Dim myDGV1 As DataGridView = DataGridView1
        With myDGV1
            .RowsDefaultCellStyle.BackColor = Color.FromName(cboDfColor.SelectedItem)
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FromName(cboAltColor.SelectedItem)
        End With

    End Sub

    Private Sub notFound()

        Dim txtS As TextBox = txtSearch
        With txtS
            .BackColor = Color.White
            .Select()
            .SelectAll()
        End With

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If


        Dim dgv As DataGridView = DataGridView1
        With dgv
            .RowsDefaultCellStyle.BackColor = Color.White
            .AlternatingRowsDefaultCellStyle.BackColor = Color.White
        End With
    End Sub

End Class

#END

🎯 You can find me on the other social media platforms:

📺 Pages:





📺 Blogger:


💯 THANK YOU SO MUCH 💯

#GoogleDrive #MicrosoftAccess #VisualBasic #VisualStudio2022 #Programming #iBasskung #CodeAMinute

Comments