Skip to content

Commit

Permalink
Add the VB COMP4 coursework code, for the fun of it.
Browse files Browse the repository at this point in the history
  • Loading branch information
issyl0 committed Oct 13, 2012
0 parents commit 159371f
Show file tree
Hide file tree
Showing 20 changed files with 1,539 additions and 0 deletions.
41 changes: 41 additions & 0 deletions frmAdd.vb
@@ -0,0 +1,41 @@
Public Class frmAdd

Private Sub frmAdd_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Check if the database connection is breathing.
'If it isn't, resuscitate it. :)
If frmLoginForm.accConnection.State <> ConnectionState.Open Then
frmLoginForm.accConnection.Open()
End If

'Populate drop down box.
cbtxtAddOptions.Items.Add("Customer")
cbtxtAddOptions.Items.Add("Supplier")
cbtxtAddOptions.Items.Add("Component")
End Sub

Private Sub btnShow_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnShow.Click

'According to the option selected, display the next form and hide
'this one.
If cbtxtAddOptions.Text = "Customer" Then
Me.Hide()
frmAddCustomer.Show()
ElseIf cbtxtAddOptions.Text = "Supplier" Then
Me.Hide()
frmAddSupplier.Show()
ElseIf cbtxtAddOptions.Text = "Component" Then
Me.Hide()
frmAddComponent.Show()
End If
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
'Close this window and show the main menu.
Me.Close()
frmMainMenu.Show()
End Sub

End Class
103 changes: 103 additions & 0 deletions frmAddComponent.vb
@@ -0,0 +1,103 @@
Imports System.Data
Imports System.Data.OleDb

Class frmAddComponent
Public accConnection As New OleDbConnection

Private Sub AddComponent_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If frmLoginForm.accConnection.State <> ConnectionState.Open Then
frmLoginForm.accConnection.Open()
End If

accConnection = frmLoginForm.accConnection
Dim strSQL As String = "SELECT supp_name FROM Supplier"
Dim da As New OleDbDataAdapter(strSQL, accConnection)
Dim ds As New DataSet

da.Fill(ds, "Supplier")

Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow

For Each dr In dt.Rows()
'List supplier names in the box so that the user can select
'the supplier that the component is coming from. Pull this
'from the Supplier table.
txtcbCompSupplierName.Items.Add(dr("supp_name"))
Next

txtcbCompSupplierName.SelectedIndex = -1

End Sub

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

'This takes the supplier name from the supplier table and gets the
'supplier ID from it, thereby letting it into the Component table.

Dim suppidvaluecmd As String = "SELECT supp_id FROM Supplier WHERE " _
& "supp_name = '" & txtcbCompSupplierName.SelectedItem & "'"

Dim cmdString As String = "INSERT INTO Component (comp_name, comp_type, comp_serialno," _
& " comp_panelkwp, comp_supplier)" _
& "VALUES (@comp_name,@comp_type,@comp_serialno," _
& "@comp_panelkwp,@comp_supplier)"

Dim da As New OleDbDataAdapter(suppidvaluecmd, accConnection)
Dim ds As New DataSet
da.Fill(ds, "Supplier")

Dim AddSupplierDataAdapter As New OleDbDataAdapter
Dim accCommand As New OleDbCommand
Dim intInsert As Integer

accCommand.Connection = frmLoginForm.accConnection
accCommand.CommandType = CommandType.Text
accCommand.CommandText = cmdString
Dim supplieridvalue As Integer = ds.Tables(0).Rows(0).Item("supp_id")
MsgBox(supplieridvalue)
Call InsertParameters(accCommand, supplieridvalue)
'Now check if all the textboxes are populated.
Try
intInsert = accCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Enter a value in each of the boxes, and make it a valid one!")
End Try

Call frmSwankyCode.CheckAdditions(intInsert, btnSave)
End Sub

Private Sub InsertParameters(ByRef acccmd As OleDbCommand, ByRef siv As Integer)

acccmd.Parameters.Add("@comp_name", OleDbType.Char).Value = txtComponentName.Text
acccmd.Parameters.Add("@comp_type", OleDbType.Char).Value = txtComponentType.Text
acccmd.Parameters.Add("@comp_serialno", OleDbType.Char).Value = txtComponentSerialNo.Text
acccmd.Parameters.Add("@comp_panelkwp", OleDbType.Numeric).Value = txtComponentPanelkWp.Text
'And now the supplier ID value.
acccmd.Parameters.Add("@comp_supplier", OleDbType.Numeric).Value = siv

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click

If btnSave.Enabled = False Then
'It's all fine and has gone through OK, so don't display a
'message because that would just be annoying, just close this
'and display the previous form.
Me.Close()
frmAdd.Show()
Else
'If it hasn't gone through OK, or nothing has been added,
'then let the user know this so as not to panic them;
'if the user did not mean to click the button, he/she knows.
MsgBox("This window will close and these details will not be saved.")
Me.Close()
frmAdd.Show()
End If

End Sub

End Class
123 changes: 123 additions & 0 deletions frmAddCustomer.vb
@@ -0,0 +1,123 @@
Imports System.Data
Imports System.Data.OleDb

Public Class frmAddCustomer
Public accConnection As New OleDbConnection

Private Sub frmAddCustomers_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If frmLoginForm.accConnection.State <> ConnectionState.Open Then
frmLoginForm.accConnection.Open()
End If

'Populate this list with selected titles.
Me.cbCustomerTitle.Items.Add("Mr")
Me.cbCustomerTitle.Items.Add("Mrs")
Me.cbCustomerTitle.Items.Add("Miss")
Me.cbCustomerTitle.Items.Add("Dr")

'Add tooltip to lblCustomerName and txtCustomerName field
'so that the user knows to add the name in a uniform way.
Me.ttCustomerName.SetToolTip(Me.lblCustomerName, _
"Name must be in the format 'Forename <space> Surname'.")
Me.ttCustomerName.SetToolTip(Me.txtCustomerName, _
"Name must be in the format 'Forename <space> Surname'.")
End Sub

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

Dim cmdString As String = "INSERT INTO Customer (cust_title, cust_name, " _
& "cust_billaddress, cust_billpostcode, cust_instaddress, " _
& "cust_instpostcode, cust_hometelno, cust_mobtelno, " _
& "cust_email, cust_mpan) VALUES (@cust_title,@cust_name," _
& "@cust_billaddress,@cust_billpostcode,@cust_instaddress," _
& "@cust_instpostcode,@cust_hometelno,@cust_mobtelno," _
& "@cust_email,@cust_mpan)"

Dim AddCustomerDataAdapter As New OleDbDataAdapter
Dim accCommand As New OleDbCommand
Dim intInsert As Integer
Dim CustomerForename As String

CustomerForename = txtCustomerName.Text
accCommand.Connection = frmLoginForm.accConnection
accCommand.CommandType = CommandType.Text
accCommand.CommandText = cmdString

'Make the customer bill address be the install address too if
'the user ticks the box.
If cbCustomerBillInstallSameAddress.CheckState = 1 Then
txtCustomerInstAddress.Text = txtCustomerBillAddress.Text
txtCustomerInstPostcode.Text = txtCustomerBillPostcode.Text
End If

Call InsertParameters(accCommand)

'Check if there are no empty textboxes. If there aren't, do all
'other checks, and send it through to the database.
Try
intInsert = accCommand.ExecuteNonQuery()
Catch ex As Exception
intInsert = 0
MsgBox("Enter a value in each of the boxes, and make it a valid one!")
End Try

'Check if the email textbox contains '@', therefore if it is a
'valid email address.
Dim pos As Integer
Dim etb As String = txtCustomerEmail.Text 'email textbox
Dim atsymbol As String = "@"
pos = InStr(etb, atsymbol)
If pos = 0 Then
MsgBox("Input another email address!")
intInsert = 0
End If

Call frmSwankyCode.CheckAdditions(intInsert, btnSave)

End Sub

Private Sub InsertParameters(ByRef acccmd As OleDbCommand)
acccmd.Parameters.Add("@cust_title", OleDbType.Char).Value = _
cbCustomerTitle.SelectedItem
acccmd.Parameters.Add("@cust_name", OleDbType.Char).Value = _
txtCustomerName.Text
acccmd.Parameters.Add("@cust_billaddress", OleDbType.Char).Value = _
txtCustomerBillAddress.Text
acccmd.Parameters.Add("@cust_billpostcode", OleDbType.Char).Value = _
txtCustomerBillPostcode.Text
acccmd.Parameters.Add("@cust_instaddress", OleDbType.Char).Value = _
txtCustomerInstAddress.Text
acccmd.Parameters.Add("@cust_instpostcode", OleDbType.Char).Value = _
txtCustomerInstPostcode.Text
acccmd.Parameters.Add("@cust_hometelno", OleDbType.Char).Value = _
txtCustomerHomeTelNo.Text
acccmd.Parameters.Add("@cust_mobtelno", OleDbType.Char).Value = _
mtxtCustomerMobTelNo.Text
acccmd.Parameters.Add("@cust_email", OleDbType.Char).Value = _
txtCustomerEmail.Text
acccmd.Parameters.Add("@cust_mpan", OleDbType.Char).Value = _
txtCustomerMpanNo.Text
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click

If btnSave.Enabled = False Then
'It's all fine and has gone through OK, so don't display a
'message because that would just be annoying, just close this
'and display the previous form.
Me.Close()
frmAdd.Show()
Else
'If it hasn't gone through OK, or nothing has been added,
'then let the user know this so as not to panic them;
'if the user did not mean to click the button, he/she knows.
MsgBox("This window will close and these details will not be saved.")
Me.Close()
frmAdd.Show()
End If

End Sub
End Class
71 changes: 71 additions & 0 deletions frmAddSupplier.vb
@@ -0,0 +1,71 @@
Imports System.Data
Imports System.Data.OleDb

Public Class frmAddSupplier
Public accConnection As New OleDbConnection

Private Sub frmAddSupplier_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If frmLoginForm.accConnection.State <> ConnectionState.Open Then
frmLoginForm.accConnection.Open()
End If
End Sub

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

Dim cmdString As String = "INSERT INTO Supplier (supp_name, supp_address, supp_postcode, " _
& "supp_telno, supp_contactname)" _
& "VALUES (@supp_name,@supp_address,@supp_postcode," _
& "@supp_telno,@supp_contactname)"

Dim AddSupplierDataAdapter As New OleDbDataAdapter
Dim accCommand As New OleDbCommand
Dim intInsert As Integer

accCommand.Connection = frmLoginForm.accConnection
accCommand.CommandType = CommandType.Text
accCommand.CommandText = cmdString
Call InsertParameters(accCommand)
'Now check if all the textboxes are populated.
Try
intInsert = accCommand.ExecuteNonQuery()
Catch ex As Exception
intInsert = 0
MsgBox("Enter a value in each of the boxes, and make it a valid one!")
End Try

Call frmSwankyCode.CheckAdditions(intInsert, btnSave)

End Sub

Private Sub InsertParameters(ByRef acccmd As OleDbCommand)
acccmd.Parameters.Add("@supp_name", OleDbType.Char).Value = txtSupplierName.Text
acccmd.Parameters.Add("@supp_address", OleDbType.Char).Value = txtSupplierAddress.Text
acccmd.Parameters.Add("@supp_postcode", OleDbType.Char).Value = txtSupplierPostCode.Text
acccmd.Parameters.Add("@supp_telno", OleDbType.Char).Value = mtxtSupplierTelNo.Text
acccmd.Parameters.Add("@supp_contactname", OleDbType.Char).Value = txtSupplierContactName.Text

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click

If btnSave.Enabled = False Then
'It's all fine and has gone through OK, so don't display a
'message because that would just be annoying, just close this
'and display the previous form.
Me.Close()
frmAdd.Show()
Else
'If it hasn't gone through OK, or nothing has been added,
'then let the user know this so as not to panic them;
'if the user did not mean to click the button, he/she knows.
MsgBox("This window will close and these details will not be saved.")
Me.Close()
frmAdd.Show()
End If

End Sub

End Class

0 comments on commit 159371f

Please sign in to comment.