Make CRUD Form in Visual Studio using Visual Basic and MS AccessNawaraj Shah
Hello guys, today in this tutorial I will teach you how to we can make CRUD application form in a visual studio just using visual basic and Microsoft access.
Before you start this tutorial, there are some requirements.
- Visual Studio with Visual Basic package
- Microsoft Access
The complete application looks like this.
To make it easier, I have divided this tutorial into four parts.
- Creating a database in MS Access
- Connect that database to visual studio
- Design application
- Write some coding so that our form can function
Creating a database in MS Access
- First, open your MS Access then choose “Blank desktop database”
- Click on the folder icon to choose a location where you want to save your database.
- Give a name “StudentData” and Save as “2002-2003 format” then click on “Create”
After this, click on “View” and give a table name “StudentTable” then fill fields along with their Data Type.
Then fill up the “Field Name” along with their “Data Type” and Save
After that to save data on a database, just double click on “StudentTable” which you find on the left side. Then fill some data and save
Connect this database to visual studio
- Open Visual Studio then creates a new project by clicking on “New Project”
- Choose a template “Visual Basic”, give a name for your project “StudentForm”
- Browse a location where you want to save your project then click on “ok” to save
To connect a database, go to “view” (on the top bar) > “Other Windows” > “Data Source” then data source window will appear.
- Click on “Add New Data Source”, choose “Database” > “Dataset”
- “New Connection” > “Microsoft Access Database File” > Browse to a location of a recently created database file.
- Test the connection then click ok
- After you click the “Next” button then message window pop-up.
- If you click on “Yes” then it will create a copy of database file on your project folder.
- But if you choose “No” then the project will use an actual database file.
- At this time continue with No
- Next > Select “Tables” > Finish
- By default, it was in “DataViewGrid” but this time we are going to use “Details”
Select “StudentTable”, drag it and drop it on the form.
Select all then change the font size to 12. And make a suitable space between elements.
Search for “RadioButton” on toolbox then drag it and drop it on the form along with “Gender” for two times. Then change their Text and Name.
- RadioButton1: Text = Male and Name = radMale
- RadioButton2: Text = Female and Name = radFemale
And also change their font size to 12.
Now select GenderTextBox and make it ReadOnly: True
To display a cross-ponding text on a GenderTextBox after a user clicks on a radio button. First, just double click on the Male radio button. Then write the following code.
If radMale.Checked Then GenderTextBox.Text = radMale.Text End If
Also, do the same thing for Female radio button.
If radFemale.Checked Then GenderTextBox.Text = radFemale.Text End If
You can check whether your application is working or not by clicking on the “Start” button on the top bar.
Then you will get a window like this.
After you run the application, you notice that Text on a GenderTextBox changes when you switch between Male and Female radio buttons. But changes did not reflect on these radio buttons if you go to Previous or Next record.
Now, to solve this issue. Just double click on “StudentTableBindingNavagitor”
Then write the following code.
If GenderTextBox.Text = radMale.Text Then radMale.Checked = True ranFemale.Checked = False ElseIf GenderTextBox.Text = radFemale.Text Then radFemale.Checked = True radMale.Checked = False End If
Now if you run the application, that problem did not occur.
Creating buttons for Save, Delete, Add Next, Next and Previous operation
So go to a toolbox, search for a button and drag it on the form for 5 times. Then change their text and name.
- Previous = Text: <<<, Name: PreBnt
- Save = Text: Save Data, Name: SaveBnt
- Delete = Text: Delete, Name: DeleteBnt
- Add New = Text: Add New, Name: AddBnt
- Next = Text: >>>, Name: NextBnt
Select all these buttons and arrange them.
Then write a code for a Previous button.
After that code for the Next button.
Code for Save data button
Try StudentTableBindingSource.EndEdit() TableAdapterManager.UpdateAll(StudentDataDataSet) MsgBox("Sucess") Catch ex As Exception MsgBox("Error occur, Please recheck the fields and try again.") End Try
Code for the Delete button
StudentTableBindingSource.RemoveCurrent() TableAdapterManager.UpdateAll(StudentDataDataSet) MsgBox("Current Record Deleted")
Last code for Add New button
StudentTableBindingSource.AddNew() radMale.Checked = True GenderTextBox.Text = radMale.Text
And this is one of the ways to make CRUD windows application form in Visual Studio using Visual Basic and Microsoft Access.
You may also like to make a registration form using MySQL and PHP.
Also, check out a video of this tutorial.