Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27389

Using structure to write to file?

$
0
0
Hi,this should be an easy question about this inventory program. I am using a structure to write to a text file by date. Now there are two text boxes, a upc text box and a quantity text box. The user inputs upc and quantity and it stores it in an structure. Then I use a if-else statement to seek out the upc within another text file and in the if-else statement is a for loop that stores info and calculations in the structure. Now when I input 2 sets of data with the same upc and write it to text file, it writes both to the file which is supposed to happen but I want to have it only write one upc and the totaled quantity of the same upc. Hope that makes sense?

Basically it appends instead of overwriting the updated upc.
I input 0921115150 as the UPC for both entries and 10 then 12 for the Quantities.

As you can see in the text file it goes by
UPC
Name
Price
Quantity
Sold
The last set is the first entry and the first set is the first entry with the updated quantity info in it. I just want the updated info in it if the same upc is being used with multiple quantities.


I highlighted where the problem potentially lies green.
Code:

Imports System.IO
Public Class frmOrderSheet
    Dim Order(-1) As clsInventory
    Public ApparelInfo(-1) As Report
    Public Sales(-1) As FinalSales
    Public intWintQuant As Integer      'Total quantity
    Public decWintSales As Decimal      'Total of sales

    Structure Report
        Dim Upc As String
        Dim Name As String
        Dim Price As Decimal
        Dim Quantity As Integer
        Dim TotalQuantity As Integer
        Dim Sales As Decimal
        Dim Total As Decimal
    End Structure
    Structure FinalSales      'IGNORE FINALSALES STRUCTURE- HAVENT USED IT YET
        Dim Upc As String
        Dim Quantity As String
        Dim Sales As String
        Dim Total As Double
    End Structure

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        'Validate if text boxes are numeric
        If Not IsNumeric(txtUPC.Text) Or Not IsNumeric(txtQuant.Text) Then
            MessageBox.Show("Please enter only numeric digits.")
            Clear()
        Else
            'storing into arrays
            ReDim Preserve ApparelInfo(ApparelInfo.Length)
            With ApparelInfo(ApparelInfo.Length - 1)
                .Upc = txtUPC.Text
                .Quantity = txtQuant.Text
            End With
            Clear() ' calls clear sub
        End If
        'loops the upc array through the class upc for matches and calculates if true
        For i = 0 To (ApparelInfo.Length - 1)
            If Order(0).UPC = ApparelInfo(i).Upc Then
                MessageBox.Show("Item found")
                ApparelInfo(i).Name = Order(0).Item
                ApparelInfo(i).Price = Order(0).Price
                ApparelInfo(i).TotalQuantity += ApparelInfo(i).Quantity
                ApparelInfo(i).Sales += ApparelInfo(i).Quantity * Order(0).Price
            Else
                MessageBox.Show(ApparelInfo(i).Upc & " not found")
            End If
        Next

    End Sub

    Private Sub frmOrderSheet_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'clear out array
        ReDim Order(-1)
        'check if file exists
        If File.Exists("Order.txt") = True Then
            'read from file
            Dim myfile As StreamReader
            myfile = File.OpenText("Order.txt")
            Do Until myfile.Peek = -1
                ReDim Preserve Order(Order.Length)
                Order(Order.Length - 1) = New clsInventory 'establish New instance Address Of class
                With Order(Order.Length - 1)
                    .UPC = myfile.ReadLine
                    .Item = myfile.ReadLine
                    .Price = myfile.ReadLine
                End With
            Loop
            'close the data file
            myfile.Close()
        End If
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        'Closes the form
        Me.Close()
    End Sub

    Public Sub Clear()
        'Clears out the OrderSheet form and sets focus
        txtQuant.Clear()
        txtUPC.Clear()
        txtUPC.Focus()
    End Sub

    Private Sub btnView_Click(sender As System.Object, e As System.EventArgs) Handles btnView.Click
        Me.Hide()
        frmReport.ShowDialog()
        Me.Show()
    End Sub

    Private Sub btnWrite_Click(sender As System.Object, e As System.EventArgs) Handles btnWrite.Click
        Dim dDate As String
        Dim myfile As StreamWriter

        dDate = dtpCalendar.Text
        'writes the information to text
        myfile = File.CreateText(dDate & ".txt")
        For i As Integer = 0 To ApparelInfo.Length - 1
            With ApparelInfo(i)
                myfile.WriteLine(.Upc)
                myfile.WriteLine(.Name)
                myfile.WriteLine(.Price)
                myfile.WriteLine(.TotalQuantity)
                myfile.WriteLine(.Sales)
            End With
        Next
        myfile.Close()
    End Sub
End Class


Viewing all articles
Browse latest Browse all 27389

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>