In this tutorial, I’m going to show you how to create a stock master. This stock master will cater all the data entry for the new acquired stocks or Item(s). And the loading, updating and deleting of a specific stocks or Items. To start in this lesson, first we need to open our project called “Stockinven”. Then, were going to add new form and we will name it as “Stock Master”. To do this, go to solution explorer right click the project name, then point the mouse pointer to “Add” then choose “Windows Form”. And it looks like as shown below:
After creating the form for Stock Master, you need to set the Name Property of all then design it, and the stock master will looks like as shown below.
To test this application, we need to set first the Stock Master to be the first form to load when we run the program. To do this, here are the following steps:
1. On the menu bar, click the Project.
2. And select Stockinven Properties.
3. Then the properties of stockinven will show.
4. Next, click the ApplicationTabbed.
5. Then on the Startup form.
6. Choose Stock Master.
7. Then press “F5” to test your program.
At this time, we will be going to manually populate the following combobox.
For color, here are the items:
For bbrand here are the items:
For Category, Here are the following items:
Computer parts, School supplies, Consumables, Books
And for Unit and quantity:
Set, Kilograms, Grams, Yard, Piece, Box
Next, let’s start adding code to our application. First Double click the txtprice and add the following code:
This code will activate when you input some value to the txtprice. And it will be computted if the input is numeric else it will be reset to nothing.
Then, were we going to copy some for database connectivity from form1. And we will paste the declaration of variables below the public class and the connection string inside the Stock_Master_load sub. And the code we now look like as shown below:
- Public Class Stock_Master
- Dim con As New OleDb.OleDbConnection
- Private Sub Stock_Master_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\stocksdb.mdb"
- End Sub
- Private Sub txtprice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtprice.TextChanged
- 'it check if the value of txtprice is not numeric
- If Not IsNumeric(txtprice.Text) Then
- txtprice.Text = ""
- Dim total As Double
- 'it computes for total price
- 'it passed the value to txttotprice
- txttotprice.Text = FormatNumber(total, 2)
- End If
- End Sub
- End Class
This time, double click the save button and add the following code:
The code below will do the insertion of data inputted bby the user and store it into the database.
- Dim result As Integer
- 'we declare cmd to represent an SQL Statements or a stored procedure
- 'to execute against a data source
- Dim cmd As New OleDb.OleDbCommand
- 'gather all information for item description
- Dim a As String
- a = "Color: " & cbColor.SelectedItem & " " & "Brand: " & cbBrand.SelectedItem & " " & "Other Desc: " & txtitemdescription.Text
- Dim sql As String
- 'we set up an sql statement intended for inserting a data
- sql = "INSERT INTO tblitemmaster(itemname, itemdescription, " & _
- " itemremarks, itembarcodeno, itemcategory, itemnoqty,itemavailqty, " & _
- " itemqty, itempricce, itemtotalprice, dateencoded) " & _
- " VALUES('" & txtitemname.Text & "'," & _
- "'" & a & "','" & txtitemremarks.Text & "'," & _
- "'" & txtitembarcode.Text & "','" & cbcategory.SelectedValue & "'," & _
- "'" & txtnumqty.Text & "','" & txtnumqty.Text & "','" & cbunitqty.SelectedValue & "'," & _
- " #" & DateValue(itemdate.Value) & "#)"
- 'open the connection
- With cmd
- .Connection = con
- .CommandText = sql
- 'execute query and return the affected rows
- result = cmd.ExecuteNonQuery
- 'check if result is zero it means that no data has inserted
- If result = 0 Then
- MsgBox("No Data has been Inserted!")
- 'else greater than one it insert the data successfully
- MsgBox("New Data is inseted succesfully!")
- End If
- End With
- Catch ex As Exception
- 'if something went wrong, it will give a message about the error
- MsgBox(ex.Message, MsgBoxStyle.Information)
- End Try
- 'close the connection
After adding the code above, press “F5” to test the application. then fill up all the fields provided, then click the save button. If successfully save, a message box will appear. And the output now will look like as shown below:
And my next lesson will focus on loading, updating and deleting of an item.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.