Read and display specific data from text file into listbox using VB 6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mas Juliza Alias
    New Member
    • Aug 2010
    • 67

    Read and display specific data from text file into listbox using VB 6

    Hi,

    I am building a program on Information System for Reservoir Operation for my Masters research. Now I have a text file which is an output file from a processing software (Terramodel) that lists out Elevation Range and Volume in columns and rows. The text file looks like this;

    -------------------------------------------------
    -software's distributor address-
    -project description-

    Depth range Volume
    (m) (m3)
    0.000 > 0.001 10.0
    0.001 > 0.002 12.0
    . .
    . .
    . .

    cummulative volume
    (m3)
    500
    -------------------------------------------------

    now i've written the code to display the data in listbox using AddItem, but the whole lines are displayed in one single line in the listbox. I want to display only the Depth Range and Volume data, starting from '0.000 > 0.001'. So, how to skip reading the file header (software's distributor address & project description) and the data column's header (Depth range (m) & Volume (m3))?

    Thank You for your time.
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    I hope this will help.
    (see also attachment)

    Code:
    Private Sub Form_Load()
    Dim FILEIN As Integer
    Dim INPUTTEXT As String
    Dim COUNTER As Integer
       FILEIN = FreeFile()
       On Error GoTo Error_Open_File
       Open App.Path & "\test.txt" For Input As #FILEIN
          Do Until EOF(FILEIN)
             Line Input #FILEIN, INPUTTEXT
             If InStr(INPUTTEXT, ">") Then List1.AddItem (INPUTTEXT)
          Loop
       Close #FILEIN
    Exit Sub
    Error_Open_File:
       Close #FILEIN
       MsgBox "There is an error opening the file"
    End Sub
    Attached Files

    Comment

    • Mas Juliza Alias
      New Member
      • Aug 2010
      • 67

      #3
      hi ggeu,

      tq, that works nicely. but still, there is a problem in that. it reads only the depth range, not the volume (value 10.00, next to 0.000 > 0.001). 1 more thing, how can i separate the values before and after the symbol ">" in different columns? the final display should be in 3 columns, 1) initial depth, before ">", 2) end depth, after symbol ">", and 3) volume. all three columns need to be declared into three variables perhaps in arrays, because it will be used in later process. how can i do this?

      your help is most appreciated, TQ.

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #4
        I hope this will help:
        If You want a table with Columns, than You have to use a Grid. (see attachment).
        You can use a array to split the inputtext and use "AddItem" to place the different parts from the array in the grid.
        Attached Files

        Comment

        • Mas Juliza Alias
          New Member
          • Aug 2010
          • 67

          #5
          it works on your test.txt file.but it failed on my text file. i've attached my file here.i think the problem is the data in this file is not saparated by a fixed space.

          thanx for ur help,
          Mas Juliza
          Attached Files

          Comment

          • Guido Geurs
            Recognized Expert Contributor
            • Oct 2009
            • 767

            #6
            The code "Split" can be used if there is only 1 space between the data.
            Here we have to use the "Mid" code but then the data must always be on the same place !!!

            Is this the case in all the documents You have to Analise ??

            If not, we have to find an other way to isolate the data in the TXT file.

            Attached is a program with "Mid" code.
            Attached Files

            Comment

            • Mas Juliza Alias
              New Member
              • Aug 2010
              • 67

              #7
              that works great to saparate the data in columns. but that's not a standard case. they are not fixed. here's the code with another data file (attached) that cannot be read correctly.

              Code:
              Private Sub Form_Load()
                  Dim InputText As String
                  Dim FileNum As Integer
                  Dim Depth1 As Double
                  Dim Depth2 As Double
                  Dim FillVol As Double
                  Dim CutVol As Double
                  With MSFlexGrid1
                  'Set grid
                  .FixedCols = 0
                  .Cols = 4
                  .FixedRows = 1
                  .Rows = 1
                  .TextMatrix(0, 0) = "Initial Depth"
                  .TextMatrix(0, 1) = "End Depth"
                  .TextMatrix(0, 2) = "Cut Volume"
                  .TextMatrix(0, 3) = "Fill Volume"
                  'Fill Grid
                  FileNum = FreeFile()
                  On Error GoTo Error_Open_File
                  Open App.Path & "\cubic.txt" For Input As #FileNum
                  Do Until EOF(FileNum)
                      Line Input #FileNum, InputText
                      InputText = Trim(InputText)
                      If InStr(InputText, ">") Then
                          MSFlexGrid1.AddItem Mid(InputText, 1, 6) & vbTab & _
                          Mid(InputText, 10, 6) & vbTab & Mid(InputText, 32, 4) & _
                          vbTab & Mid(InputText, 50, 4)
                      End If
                  Loop
                  Close #FileNum
                  End With
              Exit Sub
              Error_Open_File:
                  Close #FileNum
                  MsgBox "There is an error opening the file"
              End Sub

              how can i isolate the data and asign them into variables in arrays? these data will be used in later operation to generate graph, so i need to have them in variables as Double.

              your helps are most appreciated.
              Attached Files

              Comment

              • Guido Geurs
                Recognized Expert Contributor
                • Oct 2009
                • 767

                #8
                is it always the "Cut volume" OR "Fill Volume" ?
                Not the 2 in the same line ??

                I have modified the code so it can read any file, if there is a value or not or when the columns are on an other place.
                The data is also placed in an array (as double).

                How it works:

                The data lines of the textfile are read in a textbox.
                On the textbox You have to place the column separators by typing the value or clicking on the arrows (left= down, right= up)(default I have set it to 40 and 60 but You can change it if the most of Your documents have an other layout)

                The separators must be placed in the spaces between 2 data for each line.

                If there is a line in which the separator is in the data, You can modify the line in the textbox by adding or deleting spaces (we use this data from the textbox and not from the file for filling the array and the grid)

                click on "Fill Grid" to see the data (and also in the array because it's captured from it)
                Attached Files

                Comment

                • Mas Juliza Alias
                  New Member
                  • Aug 2010
                  • 67

                  #9
                  WOW! you're such an expert! you creatively solve the problem. the code works very well. it seems complicated and takes time for me to understand line by line. however there is 1 problem here, the same values in both TextBox (TextEdit) and MSFlexGrid1 are not the same. for example, value 5.000 in TextEdit becomes 5000 in the grid. i don't know how to edit your code because it is beyond my basic VB knowledge.

                  tq for your time.

                  Comment

                  • Guido Geurs
                    Recognized Expert Contributor
                    • Oct 2009
                    • 767

                    #10
                    Now You are highly pleased with the concept, we can go on with the tuning-up of the program.

                    I have attached the next step:
                    - with some more explanation on each step in the code.
                    - the cells in the grid are now Strings, any objection?

                    Still to do:
                    - We don't want to edit the VB code if we want to open an other Datafile! therefor we have to implement the CommonDialog so the user can browse and select a Datafile.
                    - The default value of the ColSeparator must be saved.

                    Do You agree with this? ยง;o)>
                    Attached Files

                    Comment

                    • Mas Juliza Alias
                      New Member
                      • Aug 2010
                      • 67

                      #11
                      THANKS for the explanation. it REALLY helps my understanding on your code :)

                      but there's one line i can't understand.
                      Code:
                      FrameCol(Index).Left = TextEdit.Left + 45 + (Val(TextCol(Index)) * 120)
                      why + 45? and why * 120? sorry for my limited knowledge on this.

                      the values in the grid should be in Double, because they will be used in later calculations. i've tried to change String to Double but they turns out values without the decimal points. how can we fix this?

                      yes, i agree with the CommonDialog idea. i already have it in previous dialog of downloading the data in this program. i'll edit your code in reading the file from App.Path to the datafile selected by user.

                      tq, hope to hear from u soon.

                      Comment

                      • Guido Geurs
                        Recognized Expert Contributor
                        • Oct 2009
                        • 767

                        #12
                        Framecol(index) are the red and blue lines over the textbox.
                        To place them on the right place on the textbox: the .Left dimension =
                        - TextEdit.Left = left side of the textbox
                        - + 45 [twips] = 3 pixels for the frame of the textbox
                        - + (Val(TextCol(In dex)) * 120) = the value of spaces we have set * 120 [twips] (= 8 pixels)
                        Remark: You have to use a font in the textbox with for each char the same width = Fixedsys, ... NOT Arial, ...!
                        You can experiment with these values and see what happens.
                        Also use the same font for the ruler !

                        Ok I will place the values in Double and find a way to make them visible with dot-zeros.
                        I wll also implement the commondialog and the settings of the default values.

                        Comment

                        • Guido Geurs
                          Recognized Expert Contributor
                          • Oct 2009
                          • 767

                          #13
                          I don't think it's possible to place the values in the grid in a Double format.
                          The grid has only String formats in the cells.
                          If You want the values later for calculation, You have to read them in the grid as string and transform them with CDbl(...) in the calculation code.

                          Attached are the latest modifications
                          Attached Files

                          Comment

                          • Mas Juliza Alias
                            New Member
                            • Aug 2010
                            • 67

                            #14
                            this code is perfect! now i want to read the data in the grid, do some calculation to the data, and display them in another table- in another form, perhaps. are the data of Depth1, Depth2, CutVol, and FillVol in array? let say in the new table there are 2 columns: 1)Depth 2)Volume
                            here's the whole picture:-

                            MSFlexGrid1____ _______________ ______
                            Depth1 | Depth2 | CutVol | FillVol |
                            ------------------------------------
                            -5.00 | -4.00 | 41.0 | |
                            -4.00 | -3.00 | 51.9 | |
                            -3.00 | -2.00 | 64.1 | |
                            -2.00 | -1.00 | 77.5 | |
                            -1.00 | 0.00 | 92.2 | |
                            ------------------------------------

                            and the new table should be like this:-

                            MSFlexGrid2____ _______________ ______
                            Depth | Volume |
                            -----------------
                            -5.00 | 0.00 | 'Volume(1) always being 0.00
                            -4.00 | 41.0 | '=CutVol(1) + Volume(1)
                            -3.00 | 92.9 | '=CutVol(2) + Volume(2)
                            -2.00 | 157.0 | '=CutVol(3) + Volume(3)
                            -1.00 | 234.5 | '=CutVol(4) + Volume(4)
                            0.00 | 326.7 | '=CutVol(5) + Volume(5)
                            -----------------

                            how to read the data from MSFlexGrid1, do some calculation on them, and display the result in MSFlexGrid2?

                            thanks a lot for your continuous help. i REALLY appreciate that.

                            Comment

                            • Guido Geurs
                              Recognized Expert Contributor
                              • Oct 2009
                              • 767

                              #15
                              I hope this will help ( see attachment)
                              Attached Files

                              Comment

                              Working...