Solved

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

Posted on 2003-12-07
8
218 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 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