I want to insert muliple textbox values to muliple column in access db from vb.net


This would usually be done with an INSERT statement. For example, if you had two text boxes for First Name and Last Name and you wanted to insert that data into a table called Contacts with fields FirstName and LastName then your INSERT statement would look like:

INSERT INTO Contacts (FirstName, LastName) VALUES ('Joe', 'Bloggs')

To do this via code, taking the data from your text boxes you would need to construct the string accordingly, something like:

INSERT INTO Contacts (FirstName, LastName) VALUES ('" & firstNameTextBox.Text & "', '" & lastNameTextBox.Text & "')"

The above however opens you up to some issues such as SQL Injection attack and having to know the correct characters in which to enclose your values (apostraphe for strings, hash/pound character for dates and nothing for numeric). A better approach would be to use parameterised queries:

INSERT INTO Contacts (FirstName, LastName) VALUES (?, ?)

So, using the parameterised option, your code would be something like:

Dim connectionString As String = "Your connection string" 'Check www.connectionstrings.com for Access
Dim insertStatement As String = "INSERT INTO Contacts (FirstName, LastName) VALUES (?, ?)

    Using connection As New OleDbConnection(connectionString)

        Using command As New OleDbCommand(insertStatement, connection)

            command.Parameters.AddWithValue("?", firstNameTextBox.Text)
            command.Parameters.AddWithValue("?", lastNameTextBox.Text)


        End Using

    End Using

For more information on working with Access and VB.NET, you might find this tutorial useful: ADO.NET For Beginners. It covers basic CRUD (Create, Read, Update and Delete) operations. It is written in C# but there is a link to a VB.NET version.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.