Solved

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

Posted on 2003-12-07
8
217 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
Comment Utility
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
Comment Utility
'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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
What was the error?
0
 
LVL 27

Expert Comment

by:planocz
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now