?
Solved

Simply Reading Off a File

Posted on 2003-02-27
7
Medium Priority
?
160 Views
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:
'-----------------------
'pic1.jpg
'pic2.jpg
'Which picture has an apple in it?
'Picture1

'-----------------------

'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)
  Loop
 
  sTmp = aData.sPic1(1) 'I will assign it to something else, for now, just testing
 
 Close
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.
0
Comment
Question by:fantasymick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 101

Expert Comment

by:mlmcc
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.

mlmcc
0
 
LVL 44

Expert Comment

by:Arthur_Wood
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



AW
0
 
LVL 18

Expert Comment

by:Sethi
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.
0
Independent Software Vendors: 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 1

Expert Comment

by:sazhagianambi
ID: 8033897
Hi,


   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
Next


Regards,
Nambi




0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 8033946
Hi,

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.

Regards,
Nambi
0
 
LVL 44

Expert Comment

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

sTmp = aData.sPic1(1)

to:

sTmp = aData(1).sPic1

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

aData(Index).------


not

aData.-----(Index)


AW

0
 

Accepted Solution

by:
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
loop

close #1
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 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