Re: How to get specific data from .txt file in VB6?

Tech-Archive recommends: Fix windows errors by optimizing your registry



<FeedingFrenzy1947@xxxxxxxxx> wrote in message news:1191263929.125752.69400@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Hi Rick, Thanks for the help. Would it be possible to
limit it to the first 10 lines?

On Oct 1, 12:29 pm, "Rick Rothstein \(MVP - VB\)"
<rickNOSPAMn...@xxxxxxxxxxxxxxxxx> wrote:
> I am trying to develop an application in lab which could extract only
> certain data from a .txt file.

> This is how the .txt file looks like

> **** Instruments Corp.****
> Software Version 3.93
> Sample Identification:Test 1
> Operator Identification: BC
> Notes: sample conc
> Measurement Date: Aug 31, 2007
> Measurement Time: 10:51:11
> Batch: 0

> **** Measurement Parameters ****
> Temperature = 25.0
> Suspension = Water
> Viscosity = 0.890 cp
> Ref.Index Fluid = 4.56
> Angle = 90.00
> Wavelength =455.0 nm
> Runs Completed = 1
> Run Duration = 00:01:00
> Total Elapsed Time = 00:01:00
> Average Count Rate = 456 kcps
> Ref.Index Real = 45
> Ref.Index Imag = 0.000

> **** Measurement Results ****
> Effective Diameter: 118732.3
> Polydispersity: 0.005
> Baseline Index: 0.0

> I have opened the .txt file using a common dialog. I want all the info
> after : and = to be displayed in text boxes. Any suggestion would be
> greatly appreciated.

I presume you have 22 TextBox'es... one for each data line in the file. You
should make those 22 TextBox'es a Control Array, if you haven't done so
already. If you don't know how to do this, the following procedure will work
for your TextBox'es (which I presume are already on your form). Set the
Index (not TabIndex) of the (first) TextBox corresponding to the Software
Version (the first piece of data in the file) to 0 (zero). Next, in the
correct order corresponding to the data in your file, set the **Name**
property of the next TextBox to the Name you gave your **first** TextBox.
This will set its Index property to 1 (one) automatically. Next, change the
Name property of the next TextBox to the Name you gave your **first**
TextBox which will automatically change its Index property to 2. Continue
changing the Name property for the rest of your TextBox'es as above. When
you have finished doing that, you will be able to address your TextBox'es by
their index numbers in the same way you can do with a normal array. Here is
how to make use of the control array to do what you originally asked...

The subroutine below references the name of the TextBox Control Array one
time, so you need to change that reference to the name you gave your TextBox
Control Array. I called mine Text1 and it is located inside the For-Next
loop. Also, you will have to change the dummy filename and path assignment I
used to the actual filename and path where your data file is located. You
would do this on the first assignment statement for the variable
PathandFileName. I have included some comments to help you understand what
the subroutine is doing.

That is it. Add the subroutine to your project, make the two indicated
changes above and call the subroutine in your code at the point where you
want to populate the TextBox'es.

Rick

Sub LoadTextBoxes()
Dim X As Long
Dim FF As Long
Dim TextBoxIndex As Long
Dim PathandFileName As String
Dim TotalFile As String
Dim Fields() As String
Dim Records() As String
' Substitute your file's path/name here
PathandFileName = "C:\TEMP\Test.txt"
' Let VB handle assigning the file number
FF = FreeFile
' The next section will load the entire
' text file into the TotalFile variable
Open PathandFileName For Binary As #FF
TotalFile = Space(LOF(FF))
Get #FF, , TotalFile
Close #FF
' Change the equal signs to colons so that
' we have only a single delimiter to look for
TotalFile = Replace(TotalFile, "=", ":")
' Next line will break the file up line by line
Records = Split(TotalFile, vbNewLine)
' Next, we look at each line and assign
' any data to the correct TextBox
For X = 0 To UBound(Records)
' The 2 in the next statement means the Split
' function will only operate on the 1st colon
' it sees... this protects the colons in the
' time values from being split apart.
Fields = Split(Records(X), ":", 2)
' Data lines will have two 'fields' in them...
' indexes 0 and 1
If UBound(Fields) = 1 Then
' Index 1 holds the actual data (possibly
' with a leading space; hence the Trim function)
Text1(TextBoxIndex).Text = Trim$(Fields(1))
' Keep the TextBox index counter up-to-date
TextBoxIndex = TextBoxIndex + 1
End If
Next
End Sub
> I am trying to develop an application in lab which could extract only
> certain data from a .txt file.

> This is how the .txt file looks like

> **** Instruments Corp.****
> Software Version 3.93
> Sample Identification:Test 1
> Operator Identification: BC
> Notes: sample conc
> Measurement Date: Aug 31, 2007
> Measurement Time: 10:51:11
> Batch: 0

> **** Measurement Parameters ****
> Temperature = 25.0
> Suspension = Water
> Viscosity = 0.890 cp
> Ref.Index Fluid = 4.56
> Angle = 90.00
> Wavelength =455.0 nm
> Runs Completed = 1
> Run Duration = 00:01:00
> Total Elapsed Time = 00:01:00
> Average Count Rate = 456 kcps
> Ref.Index Real = 45
> Ref.Index Imag = 0.000

> **** Measurement Results ****
> Effective Diameter: 118732.3
> Polydispersity: 0.005
> Baseline Index: 0.0

> I have opened the .txt file using a common dialog. I want all the info
> after : and = to be displayed in text boxes. Any suggestion would be
> greatly appreciated.

I presume you have 22 TextBox'es... one for each data line in the file. You
should make those 22 TextBox'es a Control Array, if you haven't done so
already. If you don't know how to do this, the following procedure will work
for your TextBox'es (which I presume are already on your form). Set the
Index (not TabIndex) of the (first) TextBox corresponding to the Software
Version (the first piece of data in the file) to 0 (zero). Next, in the
correct order corresponding to the data in your file, set the **Name**
property of the next TextBox to the Name you gave your **first** TextBox.
This will set its Index property to 1 (one) automatically. Next, change the
Name property of the next TextBox to the Name you gave your **first**
TextBox which will automatically change its Index property to 2. Continue
changing the Name property for the rest of your TextBox'es as above. When
you have finished doing that, you will be able to address your TextBox'es by
their index numbers in the same way you can do with a normal array. Here is
how to make use of the control array to do what you originally asked...

The subroutine below references the name of the TextBox Control Array one
time, so you need to change that reference to the name you gave your TextBox
Control Array. I called mine Text1 and it is located inside the For-Next
loop. Also, you will have to change the dummy filename and path assignment I
used to the actual filename and path where your data file is located. You
would do this on the first assignment statement for the variable
PathandFileName. I have included some comments to help you understand what
the subroutine is doing.

That is it. Add the subroutine to your project, make the two indicated
changes above and call the subroutine in your code at the point where you
want to populate the TextBox'es.

Rick

Sub LoadTextBoxes()
Dim X As Long
Dim FF As Long
Dim TextBoxIndex As Long
Dim PathandFileName As String
Dim TotalFile As String
Dim Fields() As String
Dim Records() As String
' Substitute your file's path/name here
PathandFileName = "C:\TEMP\Test.txt"
' Let VB handle assigning the file number
FF = FreeFile
' The next section will load the entire
' text file into the TotalFile variable
Open PathandFileName For Binary As #FF
TotalFile = Space(LOF(FF))
Get #FF, , TotalFile
Close #FF
' Change the equal signs to colons so that
' we have only a single delimiter to look for
TotalFile = Replace(TotalFile, "=", ":")
' Next line will break the file up line by line
Records = Split(TotalFile, vbNewLine)
' Next, we look at each line and assign
' any data to the correct TextBox
For X = 0 To UBound(Records)
' The 2 in the next statement means the Split
' function will only operate on the 1st colon
' it sees... this protects the colons in the
' time values from being split apart.
Fields = Split(Records(X), ":", 2)
' Data lines will have two 'fields' in them...
' indexes 0 and 1
If UBound(Fields) = 1 Then
' Index 1 holds the actual data (possibly
' with a leading space; hence the Trim function)
Text1(TextBoxIndex).Text = Trim$(Fields(1))
' Keep the TextBox index counter up-to-date
TextBoxIndex = TextBoxIndex + 1
End If
Next
End Sub

Yes.

Mike


.



Relevant Pages

  • Re: How to get specific data from .txt file in VB6?
    ... Index of the TextBox corresponding to the Software ... how to make use of the control array to do what you originally asked... ... Dim FF As Long ... Dim TotalFile As String ...
    (microsoft.public.vb.general.discussion)
  • Re: How to get specific data from .txt file in VB6?
    ... Index of the TextBox corresponding to the Software ... how to make use of the control array to do what you originally asked... ... Dim FF As Long ... Dim TotalFile As String ...
    (microsoft.public.vb.general.discussion)
  • Re: How to get specific data from .txt file in VB6?
    ... You should make those 22 TextBox'es a Control Array, if you haven't done so already. ... Next, in the correct order corresponding to the data in your file, set the **Name** property of the next TextBox to the Name you gave your **first** TextBox. ... Dim FF As Long ... Dim TotalFile As String ...
    (microsoft.public.vb.general.discussion)
  • Re: Control Arrays as parameters
    ... How can I create a control array within a ... Private Sub TestAs Variant) ... Dim c As Variant ... ElseIf TypeOf c Is TextBox Then ' simple textbox ...
    (microsoft.public.vb.general.discussion)
  • =?Utf-8?Q?RE:_unbekannte_Textbox_l=C3=B6schen?=
    ... welche davon in der betreffenden Zeile in Spalte B liegt. ... wird aber nur diejenige TextBox, deren Adresse mit der der aktiven Zeile ... Dim tbElement As TextBox ... Dim txtBox As Shape, TXTBoxName ...
    (microsoft.public.de.excel)