You are on page 1of 11

A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Connecting VB form to a Database


Go to view tab >> Other windows>> Data source

Data source window


appears click add new
Data source
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Choose Database and


click next

Click next
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Click New connection

Change the database

To Ms Access Database
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Choose the
database
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Test Connection click


>>ok>>ok

Click next
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Click yes
then next

Note the copy of the Database is created in the folder bin >> debug of your saved project

Click next
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Select Tables and


Click Finish

Select Details from the


customers

Then drag the customers


file to the form
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Adding code to the form buttons

Add new
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnAdd.Click

CustomersBindingSource.AddNew()
End Sub

Save
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnSave.Click

Try
CustomersBindingSource.EndEdit()
TableAdapterManager.UpdateAll(It4uDataSet)
MsgBox("Record Successfully saved")
Catch ex As Exception
MsgBox("Error occur, please recheck the fields and try again")
End Try

End Sub

Delete
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnDelete.Click

CustomersBindingSource.EndEdit()
TableAdapterManager.UpdateAll(It4uDataSet)
MsgBox("Record deleted")
End Sub
End Class
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Exit/close
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnClose.Click

If MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo,


MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
frmMain.Show()

Me.Hide()
Else
Return
End If

End Sub

Move next
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnNext.Click

Customersbindingsource.MoveNext

End Sub

Move previous code


Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnPrevious.Click

Customersbindingsource.MovePrevious
End Sub

Using radio button on gender selection


Private Sub radMale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles radMale.Click

If radmale.checked then

GenderTextbox.Text = radmale.text

End if

End Sub
A & O level ZIMSEC Project Compiled by Engineer Nyamz +265994961051

Private Sub radFemale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles radFemale.Click

If radfemale.checked then

GenderTextbox.Text = radfemale.text

End if

End Sub

Private Sub CustomersBindingNavigator_RefreshItems(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles CustomersBindingNavigator.RefreshItems

If GenderTextbox.text = radMale.text Then

radMale.Checked = True

radFemale.Checked = False

ElseIf GenderTextbox.text = radFemale.text Then

radFemale.Checked = True

radMale.Checked = False

Endif

End Sub

Add new button with radio


Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnAdd.Click

CustomersBindingSource.AddNew()
Radmale.checked =True
End Sub

You might also like