Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Input Data From .TXT File ( VB 6.0 and 500 Points )

Posted on 2009-02-18
7
Medium Priority
?
654 Views
Last Modified: 2012-05-06
Hi experts, i am trying to get some data from a .txt file back to my vb 6.0 project. My .txt file contains apprx  the following:

Day 1: At School
Day 2: At work
Day 3: At Home
Day 4: With Friends
Day 5: In the club
Day 6: ............
Day 7: .............

How do i get the results of Day 2 and Day 5 back to my vb 6.0 project?

Example:
Msgbox "At work" & "In the club"
0
Comment
Question by:KingSencat
7 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23671113
' Get a free file number
nFileNum = FreeFile

' Open a text file for input. inputbox returns the path to read the file
Open "C:\Documents and Settings\Sudeep\My Documents\3.txt" For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
   Line Input #nFileNum, sNextLine
   'do something with it
   'add line numbers to it, in this case!
   sNextLine = sNextLine & vbCrLf
   sText = sText & sNextLine

Loop
Text1.Text = sText

' Close the file
Close nFileNum

' Get a free file number
nFileNum = FreeFile
 
' Open a text file for input. inputbox returns the path to read the file
Open "C:\Documents and Settings\Sudeep\My Documents\3.txt" For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
   Line Input #nFileNum, sNextLine
   if left(sNextLine,2) = "Day 1" or left(sNextLine,5) = "Day 1"
   sText = sText & iif(len(stext)>0," & ","") & mid(sNextLine,5)
 
Loop
Text1.Text = sText
 
' Close the file
Close nFileNum

Open in new window

0
 

Author Comment

by:KingSencat
ID: 23671165
Are you sure this line is correct?

   if left(sNextLine,2) = "Day 1" or left(sNextLine,5) = "Day 1"
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23671175
if left(sNextLine,5) = "Day 2" or left(sNextLine,5) = "Day 5"
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:KingSencat
ID: 23671204
i have tried your first code but what I need is to get only the results of the days. and not all the .txt file
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23672001
I thought you only needed those 2.
Code below will read line by line and give a messagebox for each line.
You can do with it everything you want !
' Get a free file number
nFileNum = FreeFile
 
' Open a text file for input. inputbox returns the path to read the file
Open "C:\Documents and Settings\Sudeep\My Documents\3.txt" For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
   Line Input #nFileNum, sNextLine
   msgbox sNextLine
 
Loop
 
' Close the file
Close nFileNum

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23672357
The following reads the whole file and uses Split() to place each line into a separate entry in an Array.

Then you can easily access whatever line you want:
Dim ff As Integer
Dim fileName As String
Dim entireFile As String
Dim lines() As String
    
ff = FreeFile
fileName = "c:\someFile.txt"
Open fileName For Binary Access Read As #ff
entireFile = Input(LOF(ff), ff)
Close #ff
lines = Split(entireFile, vbCrLf)
 
' Example: To get the second line:
Dim line2 As String
line2 = Trim(Split(lines(1), ":")) ' <-- Zero based Array
MsgBox "Line 2 Value is: " & line2

Open in new window

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 2000 total points
ID: 23678026
Another example based on what you have shown in your above example is to make the text file have an .INI format

This is just another way that you can read information all of the above methods for example Idle Minds will work great if you want to read everything at runtime to speed things up and directly access from the array or if you want to just pull one line of information by just saying "Give me Day1" ... etc

Create text file for example (days.txt) with the following format.

[Days]
Day1=At School
Day2=At Work
Day3=At Home
Day4=With Friends
Day5=In the club
Option Explicit
 
Private Declare Function GetPrivateProfileStringW Lib "kernel32.dll" ( _
    ByVal lpAppName As Long, _
    ByVal lpKeyName As Long, _
    ByVal lpDefault As Long, _
    ByVal lpReturnedString As Long, _
    ByVal nSize As Long, _
    ByVal lpFileName As Long) As Long
 
  
Public Function ReadDay(ByVal szFile As String, ByVal szSection As String, ByVal szKey As String) As String
  
  Dim Buffer(1024 - 1)  As Byte
  Dim cbLen             As Long
  
  cbLen = GetPrivateProfileStringW( _
    StrPtr(szSection), _
    StrPtr(szKey), _
    StrPtr("NotFound"), _
    VarPtr(Buffer(0)), _
    UBound(Buffer), _
    StrPtr(szFile))
  
  ReadDay = Left$(Buffer, cbLen)
  Erase Buffer
  
End Function
 
Private Sub Command1_Click()
  ' Target text file path, The section, the key you want to read.
  Debug.Print ReadDay("d:\days.txt", "Days", "Day2")
  Debug.Print ReadDay("d:\days.txt", "Days", "Day5")
  
End Sub

Open in new window

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

578 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