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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loops 2

Status
Not open for further replies.

UnknownUser

Programmer
Sep 28, 2001
9
US
Hello.. i am trying to create a program that will loop all the input into a single text file.. in my layout i have txtName, txtSecondName, txtThirdName, and txtNumber.. all of that is self explanitory but the number would be how many numbers to loop for the name entered.. such as if i put into the txtName Hello and 1020 for the txtNumber it would save it in the text file as (1000Hello, 1001Hello, 1002Hello, 1003Hello, etc... up to 1020 or whatever the variable may be..and if i entered names in all three and the number entered it would be up to 1020 for Hello, 1020 for World and 1020 for Test or whatever i have put in there..is that somewhat clear..?

well i hope someone can help me really soon
Du Kannst Mich Heute Töten, Aber Morgen Wird Ich Dich Besiegen
 
I think I vaguely understand what you're looking for:

-------------------------------------
Const MyFileName As String = "c:\windows\desktop\MyTxtFile.txt"

Private Sub cmdGo_Click()
Dim FileNum As Integer
Dim NumOfLoops As Long
Dim loopCtr As Long
Dim FirstName As String
Dim SecondName As String
Dim ThirdName As String

FirstName = txtName.Text
SecondName = txtSecondName.Text
ThirdName = txtThirdName.Text
NumOfLoops = txtNumber.Text
FileNum = FreeFile

Open MyFileName For Output As #FileNum
For loopCtr = 1000 To NumOfLoops
Print #FileNum, loopCtr & FirstName & ", " & loopCtr & SecondName & ", " & loopCtr & ThirdName
Next loopCtr
Close #FileNum

End Sub
-----------------------------

Assuming the user enters "1020", the output of this will be:

1000John, 1000Paul, 1000Smith
1001John, 1001Paul, 1001Smith
*** --Omitted-- ***
1020John, 1020Paul, 1020Smith

Hope that's what you wanted!

-Mike


Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
hey thanks alot, i really appriciate it...that looks just about exactly what im looking for :)
Du Kannst Mich Heute Töten, Aber Morgen Wird Ich Dich Besiegen
 
Well the program works and I am very satisfied but when i open the txt file it shows it as:

1000Hello, 1000, 1001
1002Test, 1000, 1002
1003Default, 1000, 1003

etc.

Im looking for it to say:

1000Hello
1001Hello
1002Hello
1000Test
1001Test
1002Test
1000Default
1001Default
1002Default

etc.

without the unessisary extentions...

thanks for your help anyway
Du Kannst Mich Heute Töten, Aber Morgen Wird Ich Dich Besiegen
 
I took the liberty to revise Mike's code. This will give you what your looking for.

Const MyFileName as string = "c:\WorkProjects\Tester.txt"

Private Sub cmdAction_Click()

Dim FileNum As Integer
Dim NumOfLoops As Long
Dim loopCtr As Long
Dim FirstName As String
Dim SecondName As String
Dim ThirdName As String

' I use control arrays for my textbox that why you see
' txt(0),txt(1)...

FirstName = txt(0).Text
SecondName = txt(1).Text
ThirdName = txt(2).Text
NumOfLoops = Val(txt(3).Text)
FileNum = FreeFile

If Len(txt(0)) > 0 Then
Open MyFileName For Append As #FileNum
For loopCtr = 1 To NumOfLoops
Print #FileNum, 1000 + loopCtr & FirstName
Next loopCtr
Close #FileNum
End If

If Len(txt(1)) > 0 Then
Open MyFileName For Append As #FileNum
For loopCtr = 1 To NumOfLoops
Print #FileNum, 1000 + loopCtr & SecondName
Next loopCtr
Close #FileNum
End If

If Len(txt(2)) > 0 Then
Open MyFileName For Append As #FileNum
For loopCtr = 1 To NumOfLoops
Print #FileNum, 1000 + loopCtr & ThirdName
Next loopCtr
Close #FileNum

End If

End Sub


Let me know how if this solves your problem
 
hey thanks alot for your help...im learning as i go along but i have yet to know what Len is replaced for unless you declared the Len to equal something..ill be tryin out the code now...

if there is any information that you or anyone has left out then let me know.

Du Kannst Mich Heute Töten, Aber Morgen Wird Ich Dich Besiegen
 
Len function determines the length.

example

Len(text1.text)

This is checking the length of the objects.text property.

I recommend using the Len function when trying to determine if a object is Null or blank.


reidfl
 
oh ok.. thanks. im working on the "If FirstName = "" then MsgBox "Must Atleast enter the first name", vbOKOnly + vbExclamation, "Error" ...all of that is working but im having trouble with the others like the NumOfLoops..it comes up with an error for the NumOfLoops = txtNumber.Text..but ill eventually figure it out :) ...thanks for your help
Du Kannst Mich Heute Töten, Aber Morgen Wird Ich Dich Besiegen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top