0% found this document useful (0 votes)
47 views8 pages

Activity 6 Codes

This code defines classes and methods for managing items in a database. It includes methods for getting the next item number, adding a new item, updating an existing item, retrieving an item record, clearing fields, and enabling/disabling buttons and text fields depending on the current form state. The methods handle connecting to the database, executing SQL statements, and displaying messages after database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Activity 6 Codes

This code defines classes and methods for managing items in a database. It includes methods for getting the next item number, adding a new item, updating an existing item, retrieving an item record, clearing fields, and enabling/disabling buttons and text fields depending on the current form state. The methods handle connecting to the database, executing SQL statements, and displaying messages after database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

frmItems Code

Imports System.Data.OleDb
Public Class frmItems
Public adding As Boolean
Public updating As Boolean
Public search As Boolean
Private Sub GetItemNo()
Try
sqL = "SELECT ItemNo FROM Items Order By ItemNo Desc"
ConnDB()
cmd = New OleDbCommand(sqL, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read = True Then
txtItemNo.Text = dr(0) + 1
Else
txtItemNo.Text = 1
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
End Sub
Private Sub AddItem()
Try
sqL = "INSERT INTO Items (ItemCode, IDescription, ISize, StocksOnHand,
UnitPrice, ReProduceLevel, Capital) VALUES ('" & txtItemCode.Text & "' , '" &
txtDescription.Text & "' , '" & txtUnit.Text & "' , '" & txtQuantity.Text & "' , '" &
txtUnitPrice.Text & "' , '" & txtReProducePrice.Text & "' , '" & txtCapitalPrice.Text &
"') "
ConnDB()
cmd = New OleDbCommand(sqL, conn)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("You have successfully added an item!", MsgBoxStyle.Information,
"Add Item")
Else
MsgBox("You have failed to add this item, please try again!",
MsgBoxStyle.Critical, "Add Item")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()

End Try
End Sub

Private Sub UpdateItem()


Try
sqL = "Update Items SET ItemCode = '" & txtItemCode.Text & "', IDescription =
'" & txtDescription.Text & "', ISize = '" & txtUnit.Text & "', StocksOnHand = '" &
txtQuantity.Text & "', UnitPrice = '" & txtUnitPrice.Text & "', ReProduceLevel = '" &
txtReProducePrice.Text & "' WHERE ItemNo = " & txtItemNo.Text & ""
ConnDB()
cmd = New OleDbCommand(sqL, conn)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("This item has been successfully updated!",
MsgBoxStyle.Information, "Update Item")
Else
MsgBox("You have failed to update this item, please try again!",
MsgBoxStyle.Critical, "Update Item")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()

End Try
End Sub
Private Sub GetItemRecord()
Try
sqL = "SELECT * From Items WHERE ItemNo = " & txtItemNo.Text & ""
ConnDB()
cmd = New OleDbCommand(sqL, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read = True Then
txtItemCode.Text = dr("ItemCode")
txtDescription.Text = dr("IDescription")
txtUnit.Text = dr("ISize")
txtQuantity.Text = dr("StocksOnHand")
txtUnitPrice.Text = dr("UnitPrice")
txtReProducePrice.Text = dr("ReProduceLevel")
txtCapitalPrice.Text = dr("Capital")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
End Sub
Private Sub ClearFields()
txtItemNo.Text = ""
txtItemCode.Text = ""
txtDescription.Text = ""
txtUnit.Text = ""
txtQuantity.Text = ""
txtUnitPrice.Text = ""
txtReProducePrice.Text = ""
txtCapitalPrice.Text = ""
End Sub
Private Sub EnabledText()
txtItemNo.Enabled = True
txtItemCode.Enabled = True
txtDescription.Enabled = True
txtUnit.Enabled = True
txtQuantity.Enabled = True
txtUnitPrice.Enabled = True
txtReProducePrice.Enabled = True
txtCapitalPrice.Enabled = True
End Sub
Private Sub DisabledText()
txtItemNo.Enabled = False
txtItemCode.Enabled = False
txtDescription.Enabled = False
txtUnit.Enabled = False
txtQuantity.Enabled = False
txtUnitPrice.Enabled = False
txtReProducePrice.Enabled = False
txtCapitalPrice.Enabled = False
End Sub
Private Sub EnabledButton()
btnAdd.Enabled = True
btnUpdate.Enabled = True
btnSearch.Enabled = True

btnSave.Enabled = False
btnCancel.Enabled = False
End Sub
Private Sub DisabledButton()
btnAdd.Enabled = False
btnUpdate.Enabled = False
btnSearch.Enabled = False

btnSave.Enabled = True
btnCancel.Enabled = True
End Sub

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


DisabledText()
EnabledButton()
End Sub
Private Sub txtItemNo_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtItemNo.TextChanged
If search = True Then
GetItemRecord()
search = False
End If
End Sub

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


System.EventArgs) Handles btnSave.Click
If adding = True Then
AddItem()
DisabledText()
EnabledButton()
ClearFields()
Else
UpdateItem()
DisabledText()
EnabledButton()
ClearFields()
End If
End Sub
Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
adding = True
updating = False

EnabledText()
ClearFields()
DisabledButton()
GetItemNo()
txtItemCode.Focus()
txtItemNo.Enabled = False
End Sub
Private Sub btnSearch_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
search = True
frmLoadItems.ShowDialog()

End Sub

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


System.EventArgs) Handles btnUpdate.Click
If txtItemNo.Text = "" Then
MsgBox("Please select record to update <3", MsgBoxStyle.Information, "Update
Record")
Exit Sub
End If
adding = False
updating = True
EnabledText()
DisabledButton()
txtItemCode.Focus()
txtItemNo.Enabled = False
End Sub

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


System.EventArgs) Handles btnCancel.Click
DisabledText()
EnabledButton()
ClearFields()
End Sub

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


System.EventArgs) Handles MyBase.Load

End Sub
End Class
frmLoadItems Code
Imports System.Data.OleDb
Public Class frmItems
Public adding As Boolean
Public updating As Boolean
Public search As Boolean
Private Sub GetItemNo()
Try
sqL = "SELECT ItemNo FROM Items Order By ItemNo Desc"
ConnDB()
cmd = New OleDbCommand(sqL, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read = True Then
txtItemNo.Text = dr(0) + 1
Else
txtItemNo.Text = 1
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
End Sub
Private Sub AddItem()
Try
sqL = "INSERT INTO Items (ItemCode, IDescription, ISize, StocksOnHand,
UnitPrice, ReProduceLevel, Capital) VALUES ('" & txtItemCode.Text & "' , '" &
txtDescription.Text & "' , '" & txtUnit.Text & "' , '" & txtQuantity.Text & "' , '" &
txtUnitPrice.Text & "' , '" & txtReProducePrice.Text & "' , '" & txtCapitalPrice.Text &
"') "
ConnDB()
cmd = New OleDbCommand(sqL, conn)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("You have successfully added an item!", MsgBoxStyle.Information,
"Add Item")
Else
MsgBox("You have failed to add this item, please try again!",
MsgBoxStyle.Critical, "Add Item")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()

End Try
End Sub

Private Sub UpdateItem()


Try
sqL = "Update Items SET ItemCode = '" & txtItemCode.Text & "', IDescription =
'" & txtDescription.Text & "', ISize = '" & txtUnit.Text & "', StocksOnHand = '" &
txtQuantity.Text & "', UnitPrice = '" & txtUnitPrice.Text & "', ReProduceLevel = '" &
txtReProducePrice.Text & "' WHERE ItemNo = " & txtItemNo.Text & ""
ConnDB()
cmd = New OleDbCommand(sqL, conn)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("This item has been successfully updated!",
MsgBoxStyle.Information, "Update Item")
Else
MsgBox("You have failed to update this item, please try again!",
MsgBoxStyle.Critical, "Update Item")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()

End Try
End Sub
Private Sub GetItemRecord()
Try
sqL = "SELECT * From Items WHERE ItemNo = " & txtItemNo.Text & ""
ConnDB()
cmd = New OleDbCommand(sqL, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read = True Then
txtItemCode.Text = dr("ItemCode")
txtDescription.Text = dr("IDescription")
txtUnit.Text = dr("ISize")
txtQuantity.Text = dr("StocksOnHand")
txtUnitPrice.Text = dr("UnitPrice")
txtReProducePrice.Text = dr("ReProduceLevel")
txtCapitalPrice.Text = dr("Capital")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
End Sub
Private Sub ClearFields()
txtItemNo.Text = ""
txtItemCode.Text = ""
txtDescription.Text = ""
txtUnit.Text = ""
txtQuantity.Text = ""
txtUnitPrice.Text = ""
txtReProducePrice.Text = ""
txtCapitalPrice.Text = ""
End Sub
Private Sub EnabledText()
txtItemNo.Enabled = True
txtItemCode.Enabled = True
txtDescription.Enabled = True
txtUnit.Enabled = True
txtQuantity.Enabled = True
txtUnitPrice.Enabled = True
txtReProducePrice.Enabled = True
txtCapitalPrice.Enabled = True
End Sub
Private Sub DisabledText()
txtItemNo.Enabled = False
txtItemCode.Enabled = False
txtDescription.Enabled = False
txtUnit.Enabled = False
txtQuantity.Enabled = False
txtUnitPrice.Enabled = False
txtReProducePrice.Enabled = False
txtCapitalPrice.Enabled = False
End Sub
Private Sub EnabledButton()
btnAdd.Enabled = True
btnUpdate.Enabled = True
btnSearch.Enabled = True

btnSave.Enabled = False
btnCancel.Enabled = False
End Sub
Private Sub DisabledButton()
btnAdd.Enabled = False
btnUpdate.Enabled = False
btnSearch.Enabled = False

btnSave.Enabled = True
btnCancel.Enabled = True
End Sub

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


DisabledText()
EnabledButton()
End Sub
Private Sub txtItemNo_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtItemNo.TextChanged
If search = True Then
GetItemRecord()
search = False
End If
End Sub

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


System.EventArgs) Handles btnSave.Click
If adding = True Then
AddItem()
DisabledText()
EnabledButton()
ClearFields()
Else
UpdateItem()
DisabledText()
EnabledButton()
ClearFields()
End If
End Sub
Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
adding = True
updating = False

EnabledText()
ClearFields()
DisabledButton()
GetItemNo()
txtItemCode.Focus()
txtItemNo.Enabled = False
End Sub
Private Sub btnSearch_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
search = True
frmLoadItems.ShowDialog()

End Sub

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


System.EventArgs) Handles btnUpdate.Click
If txtItemNo.Text = "" Then
MsgBox("Please select record to update <3", MsgBoxStyle.Information, "Update
Record")
Exit Sub
End If
adding = False
updating = True
EnabledText()
DisabledButton()
txtItemCode.Focus()
txtItemNo.Enabled = False
End Sub

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


System.EventArgs) Handles btnCancel.Click
DisabledText()
EnabledButton()
ClearFields()
End Sub

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


System.EventArgs) Handles MyBase.Load

End Sub
End Class

You might also like