Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wish to create a userform in VB NET, that has a single textbox that I can put in a string in the format "AAA00 AA000" (this is in my second column) TO then use a button to search access database and return all data relating to that number and display in gridview on the same form.

my headings will be defined by the first row of my DB

Can anyone point me in the right direction, ive done some programming in vb with excel but this is my first project in vb via visual studio

Thanks izzy

What I have tried:

VB.NET
Imports System.Data.OleDb
Public Class Form1

    'Change "C:\Users\Jimmy\Documents\Merchandise.accdb" to your database location
    Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\IzzyM\Desktop\Engie\KKS\KKS Maintenance Tool.accdb"
    Dim MyConn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource
    Dim provider As String
    Dim dataFile As String
    Public myConnection As OleDbConnection = New OleDbConnection
    Public dr As OleDbDataReader

    'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'MyConn = New OleDbConnection
    'MyConn.ConnectionString = connString
    'ds = New DataSet
    'tables = ds.Tables
    'da = New OleDbDataAdapter("Select * from [EWH INSTRUMENT KKS]", MyConn) 'Change items to your database name
    'da.Fill(ds, "items") 'Change items to your database name
    'Dim view As New DataView(tables(0))
    'source1.DataSource = view
    'DataGridView1.DataSource = view
    'End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connstring As String
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        dataFile = "C:\Users\IzzyM\Desktop\Engie\KKS\KKS Maintenance Tool.accdb" ' Change it to your Access Database location
        connString = provider & dataFile
        myConnection.ConnectionString = connString
    End Sub
    Private Sub Button1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
        myConnection.Open()
        DescriptionText.Clear()
        CostText.Clear()
        PriceText.Clear()
        Dim str As String
        str = "SELECT * FROM Items WHERE (Code = '" & CodeText.Text & "')"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
        dr = cmd.ExecuteReader
        While dr.Read()
            DescriptionText.Text = dr("Description").ToString
            CostText.Text = dr("Cost").ToString
            PriceText.Text = dr("Price").ToString
        End While
        myConnection.Close()
    End Sub

End Class
Posted
Updated 21-Feb-18 21:20pm
v2

1 solution

First of all, NEVER use concatenating string as a query. This exposes you on Sql Injection[^]. Rather than this, use parameterized queries[^].
See: OleDbCommand.Parameters Property (System.Data.OleDb)[^]

A proper way to work with data is to create Data Access Layer[^] and Bussines Logic Layer[^].

Finally, i'd strongly recommend to read this: Writing a Portable Data Access Layer[^] and this: Simplified Database Access via ADO.NET Interfaces[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900