Solved

How i'm gonna extract data from text logfile into VB?

Posted on 2003-12-07
8
220 Views
Last Modified: 2010-05-03
Hi experts,

i have this kind of text logfile..

12/12/2003           09.01am            50mV
12/12/2002           09.02am            65mV
12/12/2003           09.03am            72mV

it have 3 columns (date, time , temp). Between date and time, time and temp.. they are seperated with one tab.

How i goona take data in each column at every line? meaning that, at first line, i want to take date, time and temp.
For date and time, i can take the whole string (12/12/2003, 09.01am) because it is not a value. For temp, i need the value to substitute into my formula in VB. so i need to seperate mV from the integer so that it would become a valid value. If that is difficult, how about i want to seperate this .. 50 mV <-- it has one white space between integer and unit. Can it be done?

Please help me..

thanks
0
Comment
Question by:mr_avril
8 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 9894990
Private Sub Form_Load()
    Dim inputString As String
    Dim values As Variant
    Dim timeStamp As Date
    Dim voltage As Integer
   
    Open App.Path & "\Data.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, inputString
        values = Split(inputString, Chr(9))
       
        timeStamp = CDate(values(0) & " " & Replace(values(1), ".", ":"))
        voltage = Val(values(2))
       
        Debug.Print timeStamp, voltage
    Loop
    Close #1
End Sub
0
 
LVL 10

Expert Comment

by:anv
ID: 9895833
'if u can use commas to separate the columns the following code
'wud be easy to use..

Set rsTxt = New ADODB.Recordset
           
'fpath is the full path of the text file you want to open
'cn a connection object
            cn.Open "Provider=Microsoft.Jet" _
                    & ".OLEDB.4.0;Data Source=" & fpath _
                    & ";Extended Properties='text;HDR=NO;" _
                    & "FMT=Delimited'"

'fname is the Name of the text file to open

rsTxt.Open "select * from [" & fName & "#txt]", cn, adOpenStatic, _
                  adLockReadOnly, adCmdText
'now use the recordset to fetch data from the file
'as u use 4 DBMS..
0
 

Author Comment

by:mr_avril
ID: 9895897
sorry anv.. i'm very new to VB.. i don't understand what u means..
and idle_mind.. great coding.. but i cannot run it.. it give error..
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!

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9896565
What was the error?
0
 
LVL 27

Expert Comment

by:planocz
ID: 10217761
Hi mr_avril,

This  code will read in file into a buffer and read one line at a time.Then it will oarse the line to find
your mV value.

Private Sub Parsing_to_New()
  Dim sRecord         As String
  Dim sParse          As String
  Dim sBuffer         As String
  Dim iReadfile       As Integer
  Dim iMyValue      As Integer
 
 
  'Open and read old file
  iReadfile = FreeFile
  Open "c:\YourFile.TXT" For Input As #iReadfile
  Do Until EOF(iReadfile)
     '******** read file into a buffer string ******************
     Select Case LOF(iReadfile) - Seek(iReadfile)
       Case 0
              If sParse = "" Then
                Exit Do
              End If
              If InStr(sParse, vbLf) = 0 Then
                Exit Do
              End If
      'This size is for a 1.44 Disc
      Case Is >= 142000
               sBuffer = Input(LOF(iReadfile) - Seek(iReadfile) + 1, iReadfile)
               sParse = sParse & sBuffer
      Case Is < 142000
              On Error Resume Next
              sBuffer = Input(LOF(iReadfile) - Seek(iReadfile) + 1, iReadfile)
              sParse = sParse & sBuffer
   End Select
    '*********** read string *******************************
   While InStr(sParse, vbLf)          '<----- Read one line at a time
       sRecord = Left(sParse, InStr(sParse, vbLf) - 1)  '<--- This will show you each line
       sParse = Mid(sParse, InStr(sParse, vbLf) + 1)    '<--- This will show you the whole file
       Debug.Print sRecord      '<---- Add to VB Watch to see how it works
       Debug.Print sParse      '<---- Add to VB Watch to see how it works
   If InStr(sRecord, "mV") Then
         iMyValue = Mid(sRecord, InStr(sRecord, "mV") - 2, 2)
   End If
   Print #iOutfile, sRecord
   Wend
Loop
     Close #iReadfile
  End Sub
0
 
LVL 27

Expert Comment

by:planocz
ID: 10217776
I for got to remove line....
 
Print #iOutfile, sRecord

from the example code...
Please remove that line for the code to run without errors.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 41
VBA filters 2 70
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 60
VBA: Insert New column with specific format type 12 52
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

713 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