|
-
Apr 22nd, 2002, 05:00 PM
#1
2 questions
this has to do with my last post about the project where it would write to a textfile then read it in the next form. Anyway my teacher is teaching us a certain way and so I managed to make my program almost work. I have 2 problems and would hopefully like it if someone could help me.
1. Right now I have it saving to a specific location, I need it to save to the area which the user specifies in the path textbox on the form.
2. I can get it to retrieve now, but when I click retrieve next it reads the last entry in the textfile. I need it to read line by line until the end. I'll post my new code I hope someone can help.
Code:
Option Explicit
Private Sub cmdExit_Click()
Unload frmMain
Unload frmSecond
End Sub
Private Sub cmdRetrieve_Click()
frmSecond.Show
frmMain.Hide
End Sub
Private Sub cmdSave_Click()
Dim CurPrice As Currency
Dim strTitle As String
Dim strArtist As String
strTitle = txtTitle.Text
strArtist = txtArtist.Text
CurPrice = Val(txtPrice)
Open "h:\test.txt" For Append As #1
Write #1, strTitle, strArtist, CurPrice
txtPrice = Format$(CurPrice, "$##,##0.00")
Close #1
txtTitle.Text = ""
txtArtist.Text = ""
txtPrice.Text = ""
End Sub
Private Sub Form_Load()
End Sub
' this is the first form
Code:
Private Sub cmdExit_Click()
Unload frmMain
Unload frmSecond
End Sub
Private Sub cmdRetrieveNext_Click()
Dim strTitle As String
Dim strArtist As String
Dim CurPrice As Currency
Dim int1 As Integer
int1 = FreeFile
Open "h:\test.txt" For Input As #1
Do While Not EOF(1)
Input #1, strTitle, strArtist, CurPrice
Loop
txtTitle = strTitle
txtArtist = strArtist
txtPrice = Format$(CurPrice, "$##,##0.00")
Close #1
End Sub
Private Sub Form_Load()
Dim strTitle As String
Dim strArtist As String
Dim CurPrice As Currency
Dim int1 As Integer
int1 = FreeFile
Open "h:\test.txt" For Input As #1
Input #1, strTitle, strArtist, CurPrice
txtTitle = strTitle
txtArtist = strArtist
txtPrice = Format$(CurPrice, "$##,##0.00")
Close #1
End Sub
' this is the 2nd form.
-
Apr 22nd, 2002, 05:22 PM
#2
Hyperactive Member
Okay... This seems VERY similar to a Grade 11 assignment that I'm helping people do at my school (in fact, we save to h:\ too! are you using SchoolVista?).
First off, why do you use:
and then turn around and say
Code:
Open "h:\test.txt" For Append As #1
?
Anyways, why couldn't you store the path in a global variable (that would be in a module, of course). Then just use the Common Dialog Box to get an appropriate path. Or, you could have the Common Dialog Box save the file and then also let the user find the file!
As for problem two, I would probably not have written it that way at all , but I'm not sure what your teacher wants (it looks like a cheap tactic employed the Ontario government), so I cannot help you. If it was me, I would have the program just parse the file into a gigantic array and then when the user clicked the button, just move to the next element in the array!
Hopefully no one posted while I was writing this message. I WAS FIRST
-
Apr 22nd, 2002, 05:23 PM
#3
Hyperactive Member
Hey - If this is for school, isn't this kind of cheating?
-
Apr 23rd, 2002, 05:35 PM
#4
lol
lol not cheating, it is for school but I am trying to do past projects which I missed that have to be done on our own time.
It is for a grade 12 project here. Anyway the int1 = freefile is just something i stuck in hoping it would do something to help my project. We haven't learnt anything about using modules. But i will try what you said for the first suggestion
-
Apr 24th, 2002, 04:49 PM
#5
Hyperactive Member
Okay:
assigns an available file handle to the variable int1. Normally it will assign file handle 1, since 1 is the first . Then you could say:
Code:
open "filename.ext" for operation as int1
.
Print #int1, "This text is printed ;)"
.
close (int1)
The above is prefered because you ensure that you get a free file handle (if you don't, bad things happen).
As for modules, they are where you store global variables, API delcares, DirectX, OGL code, etc. (i.e. when you work with many forms they store code that more than one form will use - basically). To declare one, right click in the Project Explorer and select Add >> Module.
Then just type:
Code:
global strFileName as String 'This is variable will hold the file name
Hope your program [eventually?] works.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|