Newbie VB code question

KthxBye

Senior member
Aug 7, 2001
404
0
71
How do I get my program to do something when it's opened or closed? I want to write data to a text file when the program is closed, and then read it into a variable when the program is opened again. I know there are plenty of coders here who must know this...
 

cavingjan

Golden Member
Nov 15, 1999
1,719
0
0
do a search in the help file for Input$ and it will give you a nice little example. Just be careful in the text file with your commas
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I don't know VB specifically, but the general idea is to put the stuff you want to do when the program starts at the top, like this:

#!/perl

# this is the first line of my program

open(READ, "the file I saved before");

# rest of stuff goes here...


and then when you want to write the file on exit, you'll probably have to trap the signal that kills the program.

# kinda like this, but in VB....

$SIG{INT}=\&quit; # this line causes the 'quit' subroutine to run when the user kills the program.

sub quit{
open(WRITE, ">filetowrite"):
print WRITE "some stuff";
exit;
}
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
Private Sub Form_Load()
Dim FileName As String
Dim FileNum As Integer
gIndex = 0 'initializes number of students to zero

'Below, variables are set for opening file
FileNum = FreeFile 'Function to find free file number
FileName = App.Path & "\class.txt"

Open FileName For Input As FileNum 'File is opened
Do While EOF(FileNum) = False 'loop that reads all data from file
gIndex = gIndex + 1
ReDim Preserve gStudents(1 To gIndex)
Input #FileNum, gStudents(gIndex).Name, gStudents(gIndex).ID, gStudents(gIndex).score(1), gStudents(gIndex).score(2), gStudents(gIndex).score(3)
Loop

Close #FileNum 'File is closed after all reading has been completed


End Sub

Private Sub Form_Unload(Cancel As Integer)

Dim FileName As String
Dim FileNum As Integer
Dim Counter As Integer

FileNum = FreeFile 'Finds free file number
FileName = App.Path & "\class.txt"
Open FileName For Output As #FileNum

For Counter = 1 To gIndex
Write #FileNum, gStudents(Counter).Name, gStudents(Counter).ID, gStudents(Counter).score(1), gStudents(Counter).score(2), gStudents(Counter).score(3)
Next Counter 'counter used to store all data that has been inputted

Close #FileNum

End Sub

There is some sample code from one of my projects.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0


<< # kinda like this, but in VB....

$SIG{INT}=\&quit; # this line causes the 'quit' subroutine to run when the user kills the program.
>>



In VB, it's not even remotely like that. Keep in mind this is a rather rudimentary question being asked, probably outside the scope of ability to port sample perl code to vb (regardless of how simple). :)

 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<<

<< # kinda like this, but in VB....

$SIG{INT}=\&quit; # this line causes the 'quit' subroutine to run when the user kills the program.
>>



In VB, it's not even remotely like that. Keep in mind this is a rather rudimentary question being asked, probably outside the scope of ability to port sample perl code to vb (regardless of how simple). :)
>>



Well, I don't know VB... hell, look at that crap, not a bracket or semicolon in sight.... WTF?
Anyway, I was trying to be as helpful as I could :)
 

Rallispec

Lifer
Jul 26, 2001
12,375
10
81


<< hell, look at that crap, not a bracket or semicolon in sight.... WTF? >>



LOL
i'd take VB over perl or java or c++ anyday.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<<

<< hell, look at that crap, not a bracket or semicolon in sight.... WTF? >>



LOL
i'd take VB over perl or java or c++ anyday.
>>



A proper block of code ends with "}", not with "End XXX". It's just not right.
 

Rallispec

Lifer
Jul 26, 2001
12,375
10
81


<<
A proper block of code ends with "}", not with "End XXX". It's just not right.
>>



I learned to program in C first.. so i got used to brackets and couldnt ever imagine writing code without them either. But VB rules! It's so easy to learn and use... and its surprisingly powerful for such a simple language. I'm a fan of it at least.