How do I use multiple textboxes in Visual Studio Basic to create a record in an access database.

VOOI JOO EE 1 Reputation point
2020-10-15T09:32:26.72+00:00

Say I have 6 textboxes and there are 6 columns in the linked database, how would go about making a button at the end of the form save the text input into the textboxes as a new record in the Access database

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 30,356 Reputation points Microsoft Vendor
    2020-10-16T06:25:02.417+00:00

    Hello VOOIJOOEE,

    Thank you for taking time to post this issue in Microsoft Q&A forum.

    You can try following codes:

    Imports System.Data.OleDb  
    
    Public Class Form1  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Dim n1 As String = TextBox1.Text  
            Dim n2 As String = TextBox2.Text  
            Dim n3 As String = TextBox3.Text  
            Dim n4 As String = TextBox4.Text  
            Dim n5 As String = TextBox5.Text  
            Dim n6 As String = TextBox6.Text  
    
            Using dbConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\XXXX\XXXX.accdb")  
                dbConn.Open()  
                Dim strSql = "INSERT INTO XXXX (c1, c2, c3, c4, c5, c6) VALUES (?,?,?,?,?,?)"  
                Using objcmd = New OleDbCommand(strSql, dbConn)  
                    objcmd.Parameters.AddWithValue("@c1", n1)  
                    objcmd.Parameters.AddWithValue("@c2", n2)  
                    objcmd.Parameters.AddWithValue("@c3", n3)  
                    objcmd.Parameters.AddWithValue("@c4", n4)  
                    objcmd.Parameters.AddWithValue("@c5", n5)  
                    objcmd.Parameters.AddWithValue("@c6", n6)  
                    objcmd.ExecuteNonQuery()  
                End Using  
            End Using  
    
        End Sub  
    End Class  
    

    Sincerely,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.