Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with loops

Status
Not open for further replies.

Retar

Technical User
Feb 28, 2001
4
US
below is a description of a loop that i've been trying to do..any help would be appreciated.

I want a loop to read a random type file from the beginning to the end and each time it finds an entry it prints it into a label.caption on my form.

For example let say my file consist of names ( a.name) and number ( a.number). I want that label1 has the value of a.name and label2.caption has the value of a.number, both from the first record of my file. when its done I want it to loop and print in label3 and label4 the values of the second record in my file for a.name and a.number and so on for the rest of the file.

Thanks in advance !
 
What do you mean by a random type file? In what way is your data stored in the file?

Assuming that you have stored the data so that each line contains a comma seperated name and number like so:

aname1,anumber1
aname2,anumber2

etc.

Then you can use this to store everything in an array which you can then use as you please.

Dim Data() As String
Dim TempString As String
Dim i, j As Integer
Open "c:\test.txt" For Input As #1
Do Until EOF(1)
j = j + 1
ReDim Preserve Data(2, j)
Line Input #1, TempString
For i = 1 To Len(TempString)
If Mid(TempString, i, 1) = "," Then Exit For
Next i
Data(1, j) = Left(TempString, i - 1)
Data(2, j) = Mid(TempString, i + 1, Len(TempString))
Debug.Print Data(1, j), Data(2, j)
Loop
Close #1
 
the code supplied by elziko should have some error handling included. I, personally, would do some of the details differerently, especially the for loop:

Code:
    For i = 1 To Len(TempString)
       If Mid(TempString, i, 1) = "," Then Exit For
    Next i

where "i" can be determined with a single statement
i = instr(TempString, ",")

All of that, however is just some (rather picky) details.

Retard
appears to be setting out to fill an a set of labels with an UNKNOWN - and VARIABLE number of "records" from the input file.

Either he is spoofing us (he really knows a lot about VB) or he is venturing way out past his personal edge.

If he is 'spoofing', oh well let him enjoy. Otherwise, the subject needs to address the issues of:

1. How many labels he has or can make on the form to fill w/ info.

2. How to handle the problems of not having (or being able to create sufficient labels to display all of the records.

3. Both of the issues need to be considered in the context of the functionallity necessary here. If it is a student exercise to illustrate I/O, the above are overkill. If is is an exercise to examine othe VB properties/procedures/forms ... - we need to know what the functionallity requirements are.

Without some definition/limit to the situation, I can see this as the never-ending expansion of the single question to envlope the know universe.




MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
- if your file is truly random access then you will know the length of each "record" so then you can open your file and get the number of records by:

Open "filename" For Random as #1 Len = Len(recordType)
iNumRec = LOF(1) / Len(recordType)

with that I would create an array of controls - the array size being the number of records. Of course the difficulty lies in placing them on the form - you will have to develop an equation to get them on the form so that they are aligned correctly.

But why not place the data in a list box or some other control that can accomodate all the records which has the ability to scroll so that if the number of records exceeds the visible space you can still scroll to see them.

HTH,
JC
 
Mongooses,

Also illustrates the inherent pitfalls ahead. You read "Random Access" and leap into the assumption that Retard is using the term technically. We need to stop and consider the situation. Are the "names" likely to just happen to be the same length? Is someone sophisticated enough to create the fixed length record likely to requesting help on how to read these same records?


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Read from the random access file till the end of the file.See the pseudocode below

// i is record number to be incremented
i=1

Loop till end of file

get #1, i , mytype

//type should have name and number like
// TYPE mytype
// mynumb as int
// myname as string * 25
// END TYPE

load label1(label1.ubound + 1)
label1(label1.ubound).caption = mytype.mynumb

load label2(label2.ubound + 1)
label2(label2.ubound).caption = mytype.myname

i = i + 1

End loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top