Imports System Imports System.Data Imports System.Data.SqlClient Imports System.IO Imports System.Text Public Class partsForm Private cnstr = mainForm.cnstr Private conn As New SqlConnection(cnstr) Private myQuery As String = String.Empty Private myCommand As SqlCommand Private adapter As SqlDataAdapter = New SqlDataAdapter() Private ds As New DataSet("KParts") Private i, limit As Integer Private oldPartNum As String = String.Empty Private addMode, editMode As Integer Private Sub parts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.MdiParent = mainForm Control.CheckForIllegalCrossThreadCalls = False Me.Top = 0 Me.Left = 0 fillDataSet() disableControls() moveFirst() addMode = 0 editMode = 0 btn_find.Visible = False End Sub Private Sub enableControls() txtPartno.Enabled = True txtDesc.Enabled = True End Sub Private Sub disableControls() txtPartno.Enabled = False txtDesc.Enabled = False End Sub Private Sub clear() txtPartno.Text = String.Empty txtDesc.Text = String.Empty End Sub Public Sub toNormalMode() ts_new.Enabled = True ts_edit.Enabled = True ts_delete.Enabled = True ts_find.Enabled = True ts_revert.Enabled = False ts_save.Enabled = False disableControls() End Sub Public Sub toEditMode() ts_new.Enabled = False ts_edit.Enabled = False ts_delete.Enabled = False ts_find.Enabled = False ts_revert.Enabled = True ts_save.Enabled = True enableControls() End Sub Public Sub toAddMode() ts_new.Enabled = False ts_edit.Enabled = False ts_delete.Enabled = False ts_find.Enabled = False ts_revert.Enabled = True ts_save.Enabled = True enableControls() End Sub Public Sub toSearchMode() ts_new.Enabled = False ts_edit.Enabled = False ts_delete.Enabled = False ts_find.Enabled = True ts_revert.Enabled = False ts_save.Enabled = False End Sub Private Sub fillDataSet() Dim conn As New SqlConnection(cnstr) myQuery = "SELECT * FROM KParts ORDER BY kptpartnum" myCommand = New SqlCommand(myQuery, conn) Try conn.Open() adapter.SelectCommand = myCommand ds.Clear() adapter.Fill(ds) i = 0 limit = ds.Tables(0).Rows.Count - 1 Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Finally conn.Close() conn.Dispose() End Try End Sub Private Sub trace(ByVal partno As String) Dim j As Integer = 0 For j = 0 To ds.Tables(0).Rows.Count - 1 If Trim(ds.Tables(0).Rows(j).Item("kptpartnum")) = partno Then txtPartno.Text = ds.Tables(0).Rows(j).Item("kptpartnum") txtDesc.Text = ds.Tables(0).Rows(j).Item("kptdescription") i = j Exit For End If Next End Sub Public Sub moveFirst() If ds.Tables(0).Rows.Count <> 0 And limit > -1 Then i = 0 txtPartno.Text = ds.Tables(0).Rows(i).Item("kptpartnum") oldPartNum = ds.Tables(0).Rows(i).Item("kptpartnum") txtDesc.Text = ds.Tables(0).Rows(i).Item("kptdescription") Else clear() End If End Sub Public Sub moveNext() If i < limit And limit > -1 Then i = i + 1 txtPartno.Text = ds.Tables(0).Rows(i).Item("kptpartnum") oldPartNum = ds.Tables(0).Rows(i).Item("kptpartnum") txtDesc.Text = ds.Tables(0).Rows(i).Item("kptdescription") End If End Sub Public Sub movePrevious() If i <> 0 And limit > -1 Then i = i - 1 txtPartno.Text = ds.Tables(0).Rows(i).Item("kptpartnum") oldPartNum = ds.Tables(0).Rows(i).Item("kptpartnum") txtDesc.Text = ds.Tables(0).Rows(i).Item("kptdescription") End If End Sub Public Sub moveLast() If limit > -1 Then i = limit txtPartno.Text = ds.Tables(0).Rows(i).Item("kptpartnum") oldPartNum = ds.Tables(0).Rows(i).Item("kptpartnum") txtDesc.Text = ds.Tables(0).Rows(i).Item("kptdescription") Else clear() End If End Sub Public Sub add() clear() addMode = 1 editMode = 0 toAddMode() End Sub Public Sub edit() oldPartNum = txtPartno.Text editMode = 1 addMode = 0 toEditMode() End Sub Public Sub save() Dim parts As New Parts(txtPartno.Text) If addMode Or editMode Then If addMode Then parts.add(txtPartno.Text, txtDesc.Text) ElseIf editMode Then parts.edit(txtPartno.Text, txtDesc.Text) End If fillDataSet() If ds.Tables(0).Rows.Count <> 0 Then oldPartNum = txtPartno.Text trace(oldPartNum) End If disableControls() addMode = 0 editMode = 0 toNormalMode() End If End Sub Public Sub delete() If txtPartno.Text <> "" Then If MessageBox.Show("Are you sure you want to delete?", "System User", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes And txtPartno.Text <> "" Then Dim parts As New Parts(txtPartno.Text) parts.delete(txtPartno.Text) fillDataSet() moveFirst() End If End If End Sub Public Sub revert() If (editMode Or addMode) Then disableControls() clear() If oldPartNum <> "" Then retrieve(oldPartNum) End If toNormalMode() End If End Sub Public Sub find() txtPartno.Enabled = True txtPartno.Text = String.Empty txtDesc.Text = String.Empty btn_find.Visible = True toSearchMode() End Sub Private Sub retrieve(ByVal partnum As String) fillDataSet() trace(partnum) txtPartno.Enabled = False btn_find.Visible = False toNormalMode() End Sub Private Sub ts_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_new.Click add() End Sub Private Sub ts_edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_edit.Click edit() End Sub Private Sub ts_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_delete.Click delete() End Sub Private Sub ts_find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_find.Click find() End Sub Private Sub ts_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_save.Click save() End Sub Private Sub ts_revert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_revert.Click revert() End Sub Private Sub btn_find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_find.Click oldPartNum = txtPartno.Text retrieve(oldPartNum) End Sub Private Sub txtUsername_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) If e.KeyCode = Keys.Enter Then oldPartNum = txtPartno.Text retrieve(oldPartNum) End If End Sub Private Sub ts_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Close() End Sub Private Sub partsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.MdiParent = mainForm Control.CheckForIllegalCrossThreadCalls = False Me.Top = 0 Me.Left = 0 fillDataSet() disableControls() moveFirst() addMode = 0 editMode = 0 btn_find.Visible = False End Sub End Class