Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there anyone here with a good knowledge working with Visual Basic + Access DB?
#10
Here this should help you get started. Since I don't know the schema of your database, I created a sample database. I made a sample project that demonstrates adding Values to a database table and reading from the same table. The code can easily be modified to suit your needs and to add to/read from multiple tables. If you need any help, respond and someone will help you out.

Code:
Imports System.Data.OleDb
Public Class Form1
    Dim SQL As String                           'holds the SQL string sent to the database
    Dim dbCommand As OleDbCommand               'sends the SQL string
    Dim dbConnection As OleDbConnection         'connects us to the database
    Dim dbFilePath As String = "C:\Users\KoBE\Documents\dbTest.accdb"   'file where database is located

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        'create new connection
        dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
        'the sql (instructions)
        SQL = "INSERT INTO tblTest (fldTest) VALUES ('" & TextBox1.Text & "')"
        'connect to the database
        dbConnection.Open()
        'create new command to send the sql through the connection we just initiated
        dbCommand = New OleDbCommand(SQL, dbConnection)
        'send the command
        dbCommand.ExecuteNonQuery()
        'close the connection
        dbConnection.Close()
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        'sql command
        SQL = "SELECT fldTest FROM tblTest"
        'new connection
        dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
        'new command to send the sql
        dbCommand = New OleDbCommand(SQL, dbConnection)
        'data will be read via this DataAdapter
        Dim data As New OleDbDataAdapter(dbCommand)
        'creates a new data table that will store the data in memory
        Dim table As New DataTable("Test")
        'open the connection
        dbConnection.Open()
        'fill the table with the data
        data.Fill(table)
        'for each row show the value in the field 'fldTest'
        For Each row As DataRow In table.Rows
            MessageBox.Show(row.Item("fldTest"))
        Next
    End Sub
End Class

Click here to download the Sample Project and database.
Reply


Messages In This Thread
RE: Is there anyone here with a good knowledge working with Visual Basic + Access DB? - by KoBE - 04-25-2011, 11:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How To make an advanced Operating System in Visual Basic 2010 ? Mohamed Samir 9 12,384 01-15-2013, 09:30 PM
Last Post: Resistance
  [Visual Basic] FTP Uploader [Tutorial] Coding Support 6 2,993 08-12-2012, 12:36 AM
Last Post: Kenneth
  Visual Studio Bill Nye The Science Guy 1 1,013 03-19-2012, 09:08 AM
Last Post: BreShiE
  Visual Basic Guide - Buttons Death Demise 9 5,245 03-09-2012, 06:46 PM
Last Post: BreShiE
  Visual Basic Guides, Tutorials, Web Resources, and E-books Compilation simply_mark 1 1,763 11-16-2011, 06:30 AM
Last Post: TalishHF

Forum Jump:


Users browsing this thread: 4 Guest(s)