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


Simply Reading Off a File

Posted on 2003-02-27
Medium Priority
Last Modified: 2010-08-05
'I have no experience in VB, so this question should be really simple for the people here...

'Bascially, I want to read off a file and dump it into an array of record and read it to a string variable.

'It is a matching program, the input file is this:
'Which picture has an apple in it?


'The code is this:
Private Type rDatabase 'The Record
       sPic1 As String * 20
       sPic2 As String * 20
       sQ As String
       sA As String
     End Type

Private Sub Form_Load()
Dim aData(1 To 100) As rDatabase 'array of record
Dim iCount As Integer
Dim Index As Integer
Dim sTmp1 As String

 Open "G:\VB\Questions.txt" For Output As 1 'open file to read
 Index = 0 'array index
 Do While (Not EOF(1))
    Index = Index + 1
    Get #1, , aData.sPic1(Index)
    Get #1, , aData.sPic2(Index)
    Get #1, , aData.sQ(Index)
    Get #1, , aData.sA(Index)
  sTmp = aData.sPic1(1) 'I will assign it to something else, for now, just testing
End Sub

'When I run it, I get an invalid qualifier error and aData in the loop is highlighted.  Also, when I trace, codes within the loop does not run (when aData is removed from the loop).  I also get an error in "sTmp = aData.sPic1(1)"

'I realize there must be a lot errors within the codes.  Please help a newbie out.
Question by:fantasymick
LVL 101

Expert Comment

ID: 8033800
Try this

Open "G:\VB\Questions.txt" For Input As 1

You are reading the file INto your program not printing it OUT from your program.

LVL 44

Expert Comment

ID: 8033810
you are using the array incorrectly:

instead of this:

   Get #1, , aData.sPic1(Index)
   Get #1, , aData.sPic2(Index)
   Get #1, , aData.sQ(Index)
   Get #1, , aData.sA(Index)

try it this way:

   Get #1, , aData(Index).sPic1
   Get #1, , aData(Index).sPic2
   Get #1, , aData(Index).sQ
   Get #1, , aData(Index).sA

LVL 18

Expert Comment

ID: 8033864
Use the following method to read a file:
Set References to Microsoft Scripting Runtime in your VB project.

Declare the following variables in Declaration section:
     Dim objFSO As FileSystemObject
Dim objTextFile As TextStream
Dim strRead as String
Create the following Sub to Write to a textfile:
Public Sub WriteToFile()
     strFileName="C:\My Documents\Testing.txt"
         Set objTextFile = objFSO.OpenTextFile(strFileName, ForReading)
strRead = objTextFile.ReadAll
End Sub
'--- strFileName is the file name where you want to write the text along with the full path of the file.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8033897

   Varfile = FreeFile
    FilePath = App.Path & "\Nambi.dat"
    'FilePath = "C:\FrunLog.txt"
    Open FilePath For Input As #Varfile  ' Open File
    Length = LOF(Varfile)  ' Length of File
    Text1 = Input(Length, Varfile) 'Get File Details and Store them into Text1

   Noe You Have the Contents in Text1.
   Dim Arr
   Arr=Split(Text1," ")

Then it will Split the Every Word From the Text1 and Store Eac Wod into the Array Called Arr.
UBound(Arr) is used to findout the No of Elements.

for i=0 to ubound(Arr)
  msgbox arr(i) 'to display Every Word



Expert Comment

ID: 8033946

For reading Contents From File U Must open it on Input Mode. Not in OutPut Mode.

Split Fn is used to split the Content according to Your Delimiter and store tem into Array.

So I Hope Code avail above Will be helpful.

LVL 44

Expert Comment

ID: 8034228
in aqddition to my earlier comment, you should also change this:

sTmp = aData.sPic1(1)


sTmp = aData(1).sPic1

since aData is the ARRAY, you need to specify the index on the ARRAY, in each case:






Accepted Solution

samgiuoco earned 300 total points
ID: 8035104
Open repname For Input As #1

index = 0

do until (EOF(1) = True)
     index = index + 1
     Line Input #1, aData(Index).sPic1
     Line Input #1, aData(Index).sPic2
     Line Input #1, aData(Index).sq
     Line Input #1, aData(Index).sa

close #1

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
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
Course of the Month10 days, 13 hours left to enroll

572 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