Visual Basic

Posted by SpaceAce 
Visual Basic
Date: August 21, 2004 03:05PM
Posted by: SpaceAce
Me Here
Visual Basic in IT we are doing
and i need a simple code
to have a save/open command
i have a text box and a label that i want to save in a file
and then be able to reopen them
Please Help
Cheers
Matt
Re: Visual Basic
Date: August 21, 2004 03:31PM
Posted by: JPM
Do you have to use a text file to store data in? It's much easier to just use a database. All you do is add the data control to the form, then in properties select the database as the file to use. Then you can use the normal VB DB commands using the '.' operator. The intellisense box drops down with the commands such as add new, append edit etc. If you have to use the text file, then ure best bet is to ask the guys @ [www.vbforums.com]. Opening the file is easy but we never did it.

JPM
Re: Visual Basic
Date: August 21, 2004 04:34PM
Posted by: Glyn
I personally wouldn't say a database is the answer, for 2 reasons. First, unless you know how to use Access (and convert the DB to Access 97 if you don't have VB SP5) or are pretty clueless at databases you are doomed, and secondly if it's for an IT project it needs to be done from a file really.

Saving a file is pretty easy though. On the button click event for the save button, add the following code:

Open myFile For Output As #1
    Print #1, fileContents
Close #1

What we do is tell VB to open myFile, which is a variable to the filename, such as C:\myfile.txt. We then tell it what you want to do with the file. In this case, open the file for output so we can write stuff to it. If the file doesn't already exist, VB creates it, but be warned, when you open it for output the contents are automatically ditched into cyberspace. The #1 is the file handle. This basically identifies the file number, so you basically calling the connection to the file number 1. You can use any number there, or a variable (assigning 'FreeFile' to a variable gives you the next available file number so you don't have to worry about it). When the file is open, we can write stuff to it. In this case we print to file number 1, the contents of the variable 'fileContents'. Again this can be text in "" or whats in a text box, it doesn't matter. The last line basically closes the file. Kinda important, or nothing will be able to open the file again until you restart Windows.

To open a file, stick this code in the button event for loading:

Open myFile For Input As #1
    fileContents = Input(LOF(1), 1)
Close #1

Same as saving, the first line opens the file, but this time for input. The second line reads the whole contents of the file into the variable fileContents, and the third line closes the file.

Hope this helps. Post again if you have any probs.

Re: Visual Basic
Date: August 21, 2004 05:34PM
Posted by: skel
pfft....visual basic :p use a proper language like c :) glyn knows his way around VB, just don't ask him about c unless its badger related :)

-------------------------------------------------------
SKEL

You can accuse a person of just about anything and get away with it, as long as you don't call them a lousy lover or bad driver. -Jackie Stewart
Re: Visual Basic
Date: August 21, 2004 08:25PM
Posted by: Glyn
LMAO ;)

You know more C than is healthy :)

class badger
{
	unsigned int x;
	unsigned int y;
	
	void BounceUpAndDownInSillyMethod(void);
};





Edited 1 time(s). Last edit at 08/21/2004 08:45PM by Glyn.
Re: Visual Basic
Date: August 22, 2004 04:12AM
Posted by: JPM
well opening a file is a lot easier in C:p

FILE *fp;

fp = fopen(input.dat, "r";);

Or if ure lazy:

FILE *fp = fopen(input.dat, "r";);

To close it is even easier:

fclose(fp);

Re: Visual Basic
Date: August 22, 2004 07:25AM
Posted by: ZaZ
Glyn Wrote:
-------------------------------------------------------
> LMAO
>
> You know more C than is healthy
>
class badger
> {
> unsigned int x;
> unsigned int y;
>
> void BounceUpAndDownInSillyMethod(void);
> };
>
>
>
>
>
ummm... that's not C :)
back to school for you :P





I'd rather have a bottle in front of me than a frontal lobotomy
Re: Visual Basic
Date: August 22, 2004 08:57PM
Posted by: Glyn
Grr, I knew that would come up :)

But in any case, I did the badgers game in C++, so thats an error on Skels part :P

Sorry, only registered users may post in this forum.

Click here to login

Maintainer: mortal, stephan | Design: stephan, Lo2k | Moderatoren: mortal, TomMK, Noog, stephan | Downloads: Lo2k | Supported by: Atlassian Experts Berlin | Forum Rules | Policy