Question:
Loading from a text file in Visual Basic?
Dillingo
2011-05-27 21:30:37 UTC
I want to save highscores of a game I made in Visual Basic to a text file, so even when the program is ended, the next time you run it the scores will load too. I have the saving part down, I tested it and it saves to the text file I have created:

Dim sFSpec As Object = "C:\Users\Ryan\Documents\Random\Highscores\highscores1.txt"
Dim sUsrInp As Object = "Highscore: " & hsname & ": " & hs
CreateObject("Scripting.FileSystemObject") _
.CreateTextFile(sFSpec, True) _
.Write(sUsrInp)

I just don't know how to load the information from that text file and print it on a certain label every time the program is ran. Can anymore help me out?
Three answers:
2011-05-29 07:37:31 UTC
Ask Kevin Jin.
2011-05-28 05:37:16 UTC
This is how I did it in VB6, the output to a message box can easily be improved upon. The program is split into two parts. When reading and writing information from a file which you wanted saved for future use, I found that the best way to do this and give you complete control is through an array.



If you look at the first part, I've created it as a function because you can use it for anything once you got the hang of it. Just type 'Call Populate" When you want to use it and it'll write whatever you want to a text file.



There are several key points, firstly the size of the array, strCust(2,3) means two rows and 3 columns big. If you can picture that in your head then you should no problem changing it to fit your needs.



The next point is if you increase the column width you can just copy and paste a bit of code that fills up one column and change the value where it says e.g. intCounter2 = 2, i.e. you created an array like this strCust(2,4), change this to 4 and adjust the source variable for whats going into the array. If you increase the rows, say str(5,2), if you look for the FOR loop and just change it to For intCounter = 0 to 3. Now if you go to the bottom of the function, it begins writing the information to the file, again it's a copy and paste job and change the inCounter2 to equal the new column you maybe adding. Just remember to add a ; sign to the previous statement but leave it out on the last because otherwise it'll write the information to the same line.



Then we look at the second part, this part reads the information back into an array, this allows you manipulate and put the information where you want. I've fed it into a variable using concatenation to turn it into a scoreboard within a message box.





Public Function Populate()



Dim strCust(2, 3) As String

Dim strName As String

Dim intCounter As Integer, intCounter2 As Integer, intAutonumber As Integer

Dim strScore As String



Open "C:/test.txt" For Output As #1



For intCounter = 0 To 1



intCounter2 = 0

intAutonumber = intAutonumber + 1

strCust(intCounter, intCounter2) = intAutonumber



intCounter2 = 1

strName = InputBox("First name?")

strCust(intCounter, intCounter2) = strName



intCounter2 = 2

strScore = InputBox("Please enter score?")

strCust(intCounter, intCounter2) = strScore





Next intCounter



intHolder = intCounter - 1

intCounter = 0

For intCounter = 0 To intHolder



intCounter2 = 0

Write #1, strCust(intCounter, intCounter2);



intCounter2 = 1

Write #1, Tab(12); strCust(intCounter, intCounter2);



intCounter2 = 2

Write #1, Tab(24); strCust(intCounter, intCounter2)





Next intCounter



Close 1



End Function



------------------------------------------------------



Private Sub Command1_Click()



Dim strCust(2, 3) As String, details As String

Dim intCounter As Integer, intCounter2 As Integer



Open "C:/test.txt" For Input As #1

intCounter = 0

intCounter2 = 0



Do While Not EOF(1)

Input #1, details



strCust(intCounter, intCounter2) = details



Scoreholder = (strCust(intCounter, intCounter2))



If intCounter2 = 2 Then

Scoreboard = Scoreboard & " " & Scoreholder & vbCrLf



Else



Scoreboard = Scoreboard & " " & Scoreholder

End If



intCounter2 = intCounter2 + 1

If intCounter2 = 3 Then

intCounter2 = 0

intCounter = intCounter + 1

End If



If intCounter = 3 Then

Exit Do

End If



Loop



Close 1



MsgBox (Scoreboard)



End Sub
mancos
2016-10-02 12:50:11 UTC
Make it a source. even nevertheless, you won't be in a position to call the "report" (now a source) using a report call, you will have the skill to examine the contents rapidly out of your application. If the "source" ought to be changed, it could be a putting particularly, or purely flow back to the implementation of the report and create the report defaults out of your application.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...