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!

Basic VBA Help - Looping & Calling

Status
Not open for further replies.

Sunny20

MIS
Apr 4, 2001
1
US
Greetings all:

I am just starting to learn visual basic and am running into a wall with code that I am writing. I need to take data from one file and put it in another. The input file, playerdata1.txt has names, atbats, and hits for a little league team. The output file needs to have a title, and colum names, same as above and the data for the team - same data as above but one derived field... average. I am just having one heck of a time trying to get this thing to loop and read all my input file, and when I call the derived field, it is giving me an error.

I am including the code that I wrote below... if anyone can help me out with this I would greately appreciate it. I am just a beginner so...

Option Compare Database
Option Explicit

Dim strName As String
Dim dHits As Double
Dim dAtBats As Double
Dim dAverage As Double

Public Sub Module()

Open "C:\playerdata1.txt" For Input As #1
Open "C:\playerstats1.txt" For Output As #2

Call Heading

Do While Not EOF(1)
Call ReadData(strName, dHits, dAtBats)
Call PrintLine(strName, dHits, dAtBats)
Loop

Close #1
Close #2

End Sub

Private Sub Heading()
Print #2, Tab; "Yellow Socks"
Print #2,
Print #2, "PLAYER"; Tab; "AT BATS"; Tab; "HITS"; Tab; "AVERAGE"
Print #2,
End Sub

Private Sub ReadData(strName As String, dHits As Double, dAtBats As Double, _
dAverage As Integer)
Input #1, strName
Input #1, dHits
Input #1, dAtBats

End Sub

Private Function dAverage(dHits As Double, dAtBats As Double) As Double
dAverage = dHits / dAtBats
End Function

Private Sub PrintLine(strName As String, dHits As Double, dAtBats As Double, _
dAverage As Double)

Print #2, strName; Tab; dHits, dAtBats; Tab; dAverage(dHits, dAtBats)

End Sub


 
Three things:
1) Nice work for a beginner
2) You are calling ReadData with three parameters, but the function takes 4. Try calling ReadData as:
ReadData(strName, dHits, dAtBats, dAverage(dHits, dAtaBats))
3) If this is a simple text file, you'll probably have a much easier time using the FileSystemObject. There is a lot of information on the Microsoft site (
If you have any questions, just let me know
 
Just an off target thought - what you are 'doing' is probably more suited to a simple database than VB. You could do the whole thing in Ms. Access, including generating 'pretty print' reports for a lot of standard stats just using the built in functions.

The Read function could still be used, but depending on how the input is generated (wheather you control this), you could even have Ms. Access do an import w/o any code. Further, with little modification, the entire data set may easily maintained as one or more tables, so you can have stats fro the whole season and be able to review the data in many different ways.

I relaize this is probably 'an exercise' in learning VB, but as such it doesn't really help you learn the subject. As ToddR says, it looks good for begining programming. It deals with a very narrow range of programming topics.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top