Question : Parsing a Text File

HI
How do you parst a text file that looks like this.



     Data file: H000277 15 dec 08

     Date: 12/15/2008

     Time: 6:04:57 AM

Main Controller Module:

 Model:             HRA P36_US
 S/N:               4551 Rev. 0
 Software Version:        8.7

Motor Controller Module:

 Software Version:        14.8

Compression Module Data:

 Total Hours:  460

Shut Down History:

 350 DC Voltage .... 0
 Inlet Pressure .. 0
 HPT Offset ...... 0
 Hose Leak ......  0
 Gas Leak          0
 Gas Sensor Failure 0
 Amb.Out of range  0
 Blowback ........ 0
 Dryer Failure     0
 Motor Controller  0
 Motor Stoped .... 0
 TCO ............. 0
 Combi Valve ..... 0
 Over Current .... 0
 No Air Flow  .... 0
 Sudden Press Drop 0
 Max. Running Time 0
 No Pressure Rise  13
 Start Button Fail.0
 Stop Button Fail. 0
 Over Pressure     0
 High Inlet Press. 0
 External Interlock0
 HPT Controller    0
 Air Flow Sensor   0
 5VDC Defective    0
 EEPROM Error      0
 Program FLASH     0
 RAM Error         0
 Gas Sensor Ref.   0
 Peltier Overcurr. 0
 Condenser Temp.   0
 Evaporator Temp.  0
 Blowdown Sys.     0
 Sudden Press. Rise0

I need to get the following:
Data file:  Name
Date:
Model:
Software Version:
Total Hours:

and values from Line Voltage to Sudden Press

I am using VBA in Access or can use VB
thank you

Answer : Parsing a Text File

Try this:

Call  Parse to fill remaining Variables.

In Module:

Option Explicit

Type typData
  DataFile          As String * 20
  Date              As String * 20
  Time              As String * 20
  Model             As String * 20
  MainVersion       As String * 10
  MotorVersion      As String * 10
  DCVoltage         As Single
  InletPressure     As Single
  HPTOffset         As Single
  HoseLeak          As Single
  GasLeak           As Single
  GasSensorFailure  As Single
  AmbOutofRange     As Single
  Blowback          As Single
  DryerFailure      As Single
  MotorController   As Single
  MotorStoped       As Single
  TCO               As Single
  CombiValve        As Single
  OverCurrent       As Single
  NoAirFlow         As Single
  SuddenPressDrop   As Single
  MaxRunningTime    As Single
  NoPressureRise    As Single
  StartButtonFail   As Single
  StopButtonFail    As Single
  OverPressure      As Single
  HighInletPress    As Single
  ExternalInterlock As Single
  HPTController     As Single
  AirFlowSensor     As Single
  VDCDefective      As Single
  EEPROMError       As Single
  ProgramFLASH      As Single
  RAMError          As Single
  GasSensorRef      As Single
  PeltierOvercurr   As Single
  CondenserTemp     As Single
  EvaporatorTemp    As Single
  BlowdownSys       As Single
  SuddenPressRise   As Single
End Type

Public m_Data As typData


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
Option Explicit
 
Private Sub Form_Load()
   Dim sLine            As String
   Dim sFileName        As String
   Dim FF               As Integer
   Dim pos              As Long
   Dim bMotor           As Boolean
 
   sFileName = App.Path & "\H000277.txt"
   FF = FreeFile
   Open sFileName For Input As #FF
   Do Until EOF(1)
      Line Input #FF, sLine
      If Not bMotor Then
         If InStr(1, sLine, "Motor Controller Module:", vbTextCompare) Then
            bMotor = True
         End If
      End If
      Parse sLine, "Data file:", m_Data.DataFile
      Parse sLine, "Date:", m_Data.Date
      Parse sLine, "Model:", m_Data.Model
      If bMotor Then
         Parse sLine, "Software Version:", m_Data.MotorVersion
      Else
         Parse sLine, "Software Version:", m_Data.MainVersion
      End If
      Parse sLine, "Sudden Press. Rise", m_Data.SuddenPressRise
   Loop
   Close FF
   Debug.Print "DataFile:", m_Data.DataFile
   Debug.Print "Date:", m_Data.Date
   Debug.Print "Model:", m_Data.Model
   Debug.Print "Main Software Version:", m_Data.MainVersion
   Debug.Print "Motor Software Version:", m_Data.MotorVersion
   Debug.Print "SuddenPressRise", m_Data.SuddenPressRise
End Sub
 
Private Sub Parse(ByVal sLine As String, ByVal sFind As String, item)
   Dim pos As Long
      pos = InStr(1, sLine, sFind, vbTextCompare)
      If pos Then
         item = Trim(Mid(sLine, pos + Len(sFind)))
      End If
End Sub
Random Solutions  
 
programming4us programming4us