?
Solved

VB Code- HELP!

Posted on 2007-11-27
10
Medium Priority
?
230 Views
Last Modified: 2016-08-29
Hello to all :)
Urgent!! Help Pllllease!!!
 I am trying to enhance something I am working on to make it better. I created a project that took a series of numbers that was entered by the user and displayed: average, minimum, maximum, total, and total numbers.
I now have a text file opening to display the numbers. The program reads one line at a time. When the first number is requested, the data file is to be opened and the first number is to be read and processed. The file is to be left open for the following numbers to be read. The program then is to continue reading numbers until all numbers have been read. When the program is ended the file is to be closed.

I have the majority of it completed... I have it to where the text file is opening and where the count figure is working but from there nothing is reading...

I will attach my project if that will help!


Imports System.IO
Public Class Form1
    Dim InpFileName, Temp As String, FileOpenFlag As Integer
    Dim FileData(100) As String, ItemCount As Integer
    Dim DataFile As StreamReader
    Dim InpNumber, InpTotal, InpCount, InpAverage As Double
    Dim InpMaximum, InpMinimum As Double
 
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim dgrResult As DialogResult
        Dim dlgOpen As New OpenFileDialog
        'If FileOpenFlag > 0 Then DataFile.Close()
 
        dlgOpen.InitialDirectory = "F:"
        dlgOpen.Filter = "Text Files|*.txt|Word Files|*.doc|All Files|*.*"
        dgrResult = dlgOpen.ShowDialog()
        If dgrResult = DialogResult.OK Then
            InpFileName = dlgOpen.FileName
            DataFile = File.OpenText(InpFileName)
            FileOpenFlag = 1
            ItemCount = 0
        Else
            TxtData.Text = "File not specified"
        End If
    End Sub
 
    Private Sub btnReadNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadNext.Click
 
 
        If FileOpenFlag > 0 Then
            If DataFile.Peek <> -1 Then
                Temp = DataFile.ReadLine
                If Temp.Trim <> "" Then
                    ItemCount = ItemCount + 1
                    FileData(ItemCount) = Temp.Trim
                    TxtCount.Text = CStr(ItemCount)
                    TxtData.Text = FileData(ItemCount)
                    InpNumber = CDbl(TxtData.Text)
                    If InpCount = 0 Then
                        InpTotal = InpNumber
                        InpAverage = InpNumber
                        InpMinimum = InpNumber
                        InpMaximum = InpNumber
 
                    Else
                        InpTotal = InpTotal + InpNumber
                        InpAverage = InpTotal / InpCount
                        If InpMinimum > InpNumber Then
                            InpMinimum = InpNumber
                            If InpMaximum < InpNumber Then
                                InpMaximum = InpNumber
                            End If
                        End If
                    End If
                Else
                    TxtData.Text = ""
                    TxtData.Text = "Blank line - Not stored"
                End If
            End If
        End If
 
       
    End Sub
 
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If FileOpenFlag > 0 Then DataFile.Close()
    End Sub
 
    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        Dim Inpdata, inpTotal, InpCount, InpAverage As Double
        Dim InpMaximum, InpMinimum As Double
 
        Inpdata = CDbl(TxtData.Text)
 
        If Inpdata = Inpdata Then
            inpTotal = 0
            InpAverage = 0
            InpMinimum = 0
            InpMaximum = 0
            InpCount = 0
            Inpdata = 0
        End If
 
 
        TxtData.Text = CStr(Inpdata)
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
    End Sub
 
 
 
End Class

Open in new window

0
Comment
Question by:ashblynn02
  • 4
  • 4
  • 2
10 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 20361489
And what you realy need ???
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20361542
I notice your filter includes Word (.doc) files.  Make sure you are not using a Word file, as this is a proprietary format that is not like a text file at all.

The code works for me, at least loading items into the text boxes for count and data when I click btnReadNext

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ashblynn02
ID: 20361610
yeah the count and data works but none of the other ones. I can't figure why the rest of the txt boxes are not working!!

Does that makes sense at all?
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20361677
>> I can't figure why the rest of the txt boxes are not working!!

But you are assigning any values to the textboxs !!!
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 20361715
Because it might help if you had code to put data in the rest of the textboxes.  Also you will need to increment your InpCount variable somewhere.  
    Private Sub btnReadNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadNext.Click
 
 
        If FileOpenFlag > 0 Then
            If DataFile.Peek <> -1 Then
                Temp = DataFile.ReadLine
                If Temp.Trim <> "" Then
                    ItemCount = ItemCount + 1
                    FileData(ItemCount) = Temp.Trim
                    TxtCount.Text = CStr(ItemCount)
                    TxtData.Text = FileData(ItemCount)
                    InpNumber = CDbl(TxtData.Text)
                    If InpCount = 0 Then
                        InpTotal = InpNumber
                        InpAverage = InpNumber
                        InpMinimum = InpNumber
                        InpMaximum = InpNumber
 
                    Else
                        InpTotal = InpTotal + InpNumber
                        InpAverage = InpTotal / InpCount
                        If InpMinimum > InpNumber Then
                            InpMinimum = InpNumber
                            If InpMaximum < InpNumber Then
                                InpMaximum = InpNumber
                            End If
                        End If
                    End If
                    'increment count
                    InpCount += 1
                Else
                    TxtData.Text = ""
                    TxtData.Text = "Blank line - Not stored"
                End If
            End If
        End If
 
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
       
    End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20361736
And then If statement are wrong! Change it to this

If InpMinimum > InpNumber Then
   InpMinimum = InpNumber
End If
If InpMaximum < InpNumber Then
   InpMaximum = InpNumber
End If
0
 

Author Comment

by:ashblynn02
ID: 20361765
Ok... i will try all of that and see if it works! Thanks so much for all your help! :)
If it doesn't work... I'll be back!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20361797
Since InpMinimum will be initialized as zero will be always zero. You have to initialize as 1 or a bigger number on the form load. Then when it starts you will grad the minimum number of your file.

InpMinimum  = 1
0
 

Author Comment

by:ashblynn02
ID: 20370550
Okay so far the majority of it is working out :) yaaah!

There are still two little problems:

1) The average won't come out right though and I can't figure out why?

2) Also when I click the reset button it sets it to zeros and then if I hit read next again everything goes right back into the boxes. How do I get it to where it resets everything?

Thanks again for all your help :) You guys rock!!!
Imports System.IO
Public Class Form1
    Dim InpFileName, Temp As String, FileOpenFlag As Integer
    Dim FileData(100) As String, ItemCount As Integer
    Dim DataFile As StreamReader
    Dim InpNumber, InpTotal, InpCount, InpAverage As Double
    Dim InpMaximum, InpMinimum As Double
 
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim dgrResult As DialogResult
        Dim dlgOpen As New OpenFileDialog
        'If FileOpenFlag > 0 Then DataFile.Close()
 
        dlgOpen.InitialDirectory = "F:"
        dlgOpen.Filter = "Text Files|*.txt|Word Files|*.doc|All Files|*.*"
        dgrResult = dlgOpen.ShowDialog()
        If dgrResult = DialogResult.OK Then
            InpFileName = dlgOpen.FileName
            DataFile = File.OpenText(InpFileName)
            FileOpenFlag = 1
            ItemCount = 0
        Else
            TxtData.Text = "File not specified"
        End If
    End Sub
 
    Private Sub btnReadNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadnext.Click
 
 
        If FileOpenFlag > 0 Then
            If DataFile.Peek <> -1 Then
                Temp = DataFile.ReadLine
                If Temp.Trim <> "" Then
                    ItemCount = ItemCount + 1
                    FileData(ItemCount) = Temp.Trim
                    TxtCount.Text = CStr(ItemCount)
                    TxtData.Text = FileData(ItemCount)
                    InpNumber = CDbl(TxtData.Text)
                    If InpCount = 0 Then
                        InpTotal = InpNumber
                        InpAverage = InpNumber
                        InpMinimum = InpNumber
                        InpMaximum = InpNumber
 
                    Else
                        InpTotal = InpTotal + InpNumber
                        InpAverage = InpTotal / InpCount
                        If InpMinimum > InpNumber Then
                            InpMinimum = InpNumber
                            If InpMaximum < InpNumber Then
                                InpMaximum = InpNumber
                            End If
                        End If
                    End If
                    'increment count
                    InpCount += 1
                Else
                    TxtData.Text = ""
                    TxtData.Text = "Blank line - Not stored"
                End If
            End If
        End If
 
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
 
    End Sub
 
 
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If FileOpenFlag > 0 Then DataFile.Close()
    End Sub
 
    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        Dim Inpdata, inpTotal, InpCount, InpAverage As Double
        Dim InpMaximum, InpMinimum As Double
 
        Inpdata = CDbl(TxtData.Text)
 
        If Inpdata = Inpdata Then
            inpTotal = 0
            InpAverage = 0
            InpMinimum = 0
            InpMaximum = 0
            InpCount = 0
            Inpdata = 0
        End If
 
 
        TxtData.Text = CStr(Inpdata)
        TxtTotal.Text = CStr(InpTotal)
        TxtAverage.Text = CStr(InpAverage)
        TxtMin.Text = CStr(InpMinimum)
        TxtMax.Text = CStr(InpMaximum)
        TxtCount.Text = CStr(InpCount)
    End Sub
 
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadnext.Click
 
    End Sub
End Class

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question