Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-17-2006, 02:25 PM   #1
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
VB6 Save and Open

Hello All,

I am working on a VB6 project, and am about to kill myself over this save and open deal.

What I want to do is this:
- Save the values of many boolean variables, and two integer variables to a text file (.txt)
- When clicked open, I want that data to be accessed and the values to be assigned to the variables

Here is what is happening :
- I am making files and they have the correct contents in them
- The files, when opened, are not passing the value of the variables to the variables in the program


Here is my save code :

Code:
CommonDialog1.DialogTitle = "Save a File"
CommonDialog1.FileName = ""
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNFileMustExist
CommonDialog1.CancelError = True
CommonDialog1.ShowSave

FileName = CommonDialog1.FileName

Open FileName For Output As 1
     ' r is games played, wins is how many won, and playedgames in if a game as been played or not to prevent repeating'
    Print #1, r
    Print #1, wins
     For i = 1 To 43
     Print #1, playedgames(i)
     Next

Close #1
Exit Sub

Here is the code of Opening

Code:
CommonDialog1.DialogTitle = "Open a File"
CommonDialog1.FileName = ""
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNFileMustExist
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen


Open FileName For Input As 1
      'Copy entire contents of File to appropriate variables
    Input #1, r
    Input #1, wins
    For i = 1 To 43
    Input #1, playedgames(i)
    Next

Close 1

Label3.Caption = r
Label5.Caption = wins
And here is that a text file looks like after:
- 30 games opened (line one)
- 0 games won (line two)
- all playedgames booleans set to false because they haven't been completed successfully yet (lines 3 down to line 45, which I wont show)

Code:
 30 
 0 
False
False
False
False
False
False
False
False...

Would someone be able to help me figure out how to make it when I open the file that the variables are then updated with the data from the text file.

Thanks,
Ryan

PS - This is the first time I am using this code, and going off a guide my teacher provided... I really am not even sure I am doing it semi-correctly. But, I know the text files are saving, like I said.
__________________
RiotCats.com, an internet domain specifically fabricated and visually erected for the appreciation of the feline kingdom!
rspassey is offline   Reply With Quote
Old 03-17-2006, 05:51 PM   #2
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
VB doesn't work that way when you are reading. Use a temp variable for your read code:

Code:
Dim blnTemp As Boolean
Dim i As Integer

i = 0
While Not EOF(1) 'use EOF instead of hard coding passes
 Input #1, blnTemp
 playedgames(i) = blnTemp
 i = i + 1
Wend
faulkner132 is offline   Reply With Quote
Old 03-17-2006, 06:13 PM   #3
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
Quote:
Originally Posted by faulkner132
VB doesn't work that way when you are reading. Use a temp variable for your read code:

Code:
Dim blnTemp As Boolean
Dim i As Integer

i = 0
While Not EOF(1) 'use EOF instead of hard coding passes
 Input #1, blnTemp
 playedgames(i) = blnTemp
 i = i + 1
Wend
Are you positive?

I have been playing around with it for a while, and I think I got it to work. When I do the input now, it loads the last game, keeps track of wins and everything. All I was missing was one line of code, which I must have forgotten to type in. Those are the kinds of sloppy programming mistakes I make often when trying to get projects done quickly.

Here is the open code which is working:

Code:
CommonDialog1.DialogTitle = "Open a File"
CommonDialog1.FileName = ""
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNFileMustExist
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
FileName = CommonDialog1.FileName
'RichTextBox1.LoadFile CommonDialog1.FileName, rtfRTF



Open FileName For Input As 1
      'Copy entire contents of File to appropriate variables
    Input #1, r
    Input #1, wins
    For i = 1 To 43
    Input #1, playedgames(i)
    Next
    Input #1, gamenum

Close #1


gameload 'load the game from gamenum = ...etc...

Label3.Caption = r
Label5.Caption = wins
Next, I am going to add the code so that it saves the progress of each game incase someone has to leave part way through. That is what another variable:

Code:
for i = 1 to 81
(input/ output) #1, grid(i)
next
Thanks for the suggestion. I am sure you know more about VB6 than I, so if/when I run into a problem with this code, I can try it your way.

I see how your code is much simpler.. and probably more efficient. I would like to improve my techniques like that... but at the moment, I am content with getting this project up and working. This save and open is just something I wanted to add on to it, so that the program can actually be used by some people, and wasn't a requirement.

Last edited by rspassey; 03-17-2006 at 06:16 PM.
rspassey is offline   Reply With Quote
Old 03-20-2006, 07:52 AM   #4
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
That is how it worked in VB4, so I've always done it with a temp variable. The way you are doing it makes more sense though, so don't mind me!
faulkner132 is offline   Reply With Quote
Old 03-21-2006, 08:01 PM   #5
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
Hmm, everything is working now, but I am running into an issue of it giving the value (true or false) back to the variable, or perhaps I missed something in my coding... Oh well, if I can't figure this one out on my own, then I can just submit the project without this feature, as it isn't required... though I would like to add it in if possible, and I don't have deadlines, so I can play around with it a bit more.
rspassey is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 05:06 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2