Access database source connection

DRGAGI 146 Reputation points
2020-12-15T20:50:30.587+00:00

Hello, i made a Access database connection with a source from access file from my desktop. What i need is to change source from desktop to dataset that i have on form, any suggestion?
48454-snap1.jpg

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,366 Reputation points
    2020-12-15T21:12:35.557+00:00

    Hello @DRGAGI

    If you want to access the connection from any form, module or class.

    Add ApplicationEvents.vb to your project and place the following in it.

    Imports System.Data.OleDb  
    Namespace My  
      
        Partial Friend Class MyApplication  
            Private Shared ConnectionString As String =  
                        "Provider=Microsoft.ACE.OLEDB.12.0;" &  
                        "Data Source=C:\Users\DR\Desktop\LoginDatabase.mdb"  
      
            Private Shared _connection As OleDbConnection  
      
            Public Shared ReadOnly Property AccessConnection() As OleDbConnection  
                Get  
                    If _connection Is Nothing Then  
                        _connection = New OleDbConnection(ConnectionString)  
                    End If  
      
                    Return _connection  
                End Get  
            End Property  
      
        End Class  
      
    End Namespace  
    

    Use it

    Dim cn = My.MyApplication.AccessConnection  
    
    0 comments No comments

  2. DRGAGI 146 Reputation points
    2020-12-15T21:25:30.933+00:00

    Let me be specific, i am trying to make Log In application, i have two forms, Form1 is where i put my data to Access database, and Form2 if when will be my Log In form. My applications is already connected to a Access database via data source. But my code is made to read access file from desktop. I want to tell my app to read source from data source i made instead from desktop.
    48398-snap2.jpg

    0 comments No comments

  3. DRGAGI 146 Reputation points
    2020-12-15T21:26:14.837+00:00
    ' My code is this
    
    Imports System.Data.OleDb
    
    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim conn As New OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\DR\Desktop\LogInDatabase.mdb")
            conn.Open()
    
            Dim cmdlog As OleDbCommand = New OleDbCommand("select * from LoginTable where [Username]='" & TextBox1.Text & "'and [Password]='" & TextBox2.Text & "'", conn)
            Dim drlog As OleDbDataReader = cmdlog.ExecuteReader
    
            If (drlog.Read() = True) Then
    
                ' case sensitive
                If StrComp(TextBox1.Text, drlog.GetValue(1), 0) = 0 Then
                    ' case sensitive
    
                    Me.Hide()
                    Form3.Show()
                    MsgBox("login successfully")
                Else
                    MsgBox("login failed")
                    TextBox1.Text = ""
                    TextBox2.Text = ""
                End If
            End If
        End Sub
    End Class
    
    0 comments No comments

  4. DRGAGI 146 Reputation points
    2020-12-15T21:28:10.467+00:00

    Maybe i will be more satisfied if i could have option to access file via Open File Dialog and load external database into my application

    0 comments No comments

  5. DRGAGI 146 Reputation points
    2020-12-15T21:31:40.633+00:00

    Or, i should enter the path from install folder where my app will be installed by default?
    48399-snap3.jpg

    0 comments No comments