Solved

VB Code- HELP!

Posted on 2007-11-27
10
183 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
 

Author Comment

by:ashblynn02
ID: 20361428
0
 
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 38

Accepted Solution

by:
PaulHews earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now