Answer:
Sample Patient Table (PatientDB.accdb
):
PID (AutoNumber) | PName | ContactNo | Disease |
---|---|---|---|
1 | John Doe | 123-456-7890 | Flu |
2 | Jane Smith | 987-654-3210 | Cold |
3 | Sam Brown | 555-555-5555 | Pneumonia |
4 | Emma White | 111-222-3333 | Asthma |
5 | Oliver Green | 444-555-6666 | Cancer |
Imports System.Data.OleDb
Public Class Form1
Dim conn As OleDbConnection
Dim cmd As OleDbCommand
Dim connString As String
' This is the connection string to your Access database
' Make sure to replace with the correct path to your .accdb file
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Path\To\Your\Database\PatientDB.accdb;Persist Security Info=False;"
' The Button click event to insert records and show message box
Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
Try
' Open the connection
conn = New OleDbConnection(connString)
conn.Open()
' Insert five records into the Patient table
Dim insertQuery As String = "INSERT INTO Patient (PName, ContactNo, Disease) VALUES " &
"('John Doe', '123-456-7890', 'Flu')," &
"('Jane Smith', '987-654-3210', 'Cold')," &
"('Sam Brown', '555-555-5555', 'Pneumonia')," &
"('Emma White', '111-222-3333', 'Asthma')," &
"('Oliver Green', '444-555-6666', 'Cancer')"
' Execute the insert query
cmd = New OleDbCommand(insertQuery, conn)
cmd.ExecuteNonQuery()
' Display success message
MessageBox.Show("Records inserted successfully into the Patient table.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
' Display error message if something goes wrong
MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Ensure the connection is closed
If conn IsNot Nothing AndAlso conn.State = ConnectionState.Open Then
conn.Close()
End If
End Try
End Sub
End Class