Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB Code- HELP!

Posted on 2007-11-27
10
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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

636 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