Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey there, I would like to know how to display Access data from certain fields in VB's textboxes using SQL? I'm a beginner to VB and I've been searching a lot but I can't seem to get it working...

So far, following a tutorial, I can display every row of a column in Message boxes but I want it to show in textboxes and people should be able to edit it and save the changes to the database

Here's the code:
VB
Imports System.Data.OleDb
Public Class Form1
    Dim SQL As String
    Dim dbConnection As OleDbConnection
    Dim dbCommand As New OleDbCommand
    Dim dbFilePath As String = "C:\..\..\..\dbTest.accdb"

'When user clicks writes something on textbox1 and then clicks Button Insert
    Private Sub buttonInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonInsert.Click
        dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
       
        SQL = "INSERT INTO tblTest (fldTest) VALUES ('" & TextBox1.Text & "')"
        dbConnection.Open()
        dbCommand = New OleDbCommand(SQL, dbConnection)
        dbCommand.ExecuteNonQuery()
        dbConnection.Close()
    End Sub

'When user clicks Button Read
    Private Sub buttonRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonRead.Click
        SQL = "SELECT fldTest FROM tblTest"
        dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
        dbCommand = New OleDbCommand(SQL, dbConnection)
        Dim data As New OleDbDataAdapter(dbCommand)
        Dim table As New DataTable("Test")
        dbConnection.Open()
        data.Fill(table)
        For Each row As DataRow In table.Rows
            MessageBox.Show(row.Item("fldTest"))
        Next
    End Sub

End Class

Here's a screen that shows my sample form and the sample database http://goo.gl/yLkuc

Now I have textbox2 and textbox3 and I want the field1 and field2 from the database to show in textbox 2 and textbox3 respectively

How can I do it?
Posted

the link below is for what you need but in C#
http://www.homeandlearn.co.uk/csharp/csharp_s12p6.html[^]

once you get in c# you cn convert it to vb

http://www.developerfusion.com/tools/convert/csharp-to-vb/[^]
 
Share this answer
 
Thank you. That wasn't the best solution but I actually discovered on that same site, it had it all explained for VB.net as well.

So thank you for the reference ;)
http://www.homeandlearn.co.uk/net/nets12p6.html[^]
 
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