[RESOLVED] Trouble opening files
The code bellow basically opens each customer .txt file (each having a 0 to 10 array) I want to extract two entities from each array (depending if array number "9" = yes) but when I run the de-bug comes up on the part of the code which opens the file, the file being opened with the .path and .filename so where am I going wrong?? :ehh:
On Form Load:
Code:
Dim CusData(0 To 10) As String
Dim i As Integer
Dim Customer As Integer
Private Sub Form_Load()
File1.Path = (App.Path & "\SAVED_DATA\CustomerPI")
i = 0
Customer = File1.ListIndex
Customer = 0
For Customer = 0 To File1.ListCount - 1
Open (File1.Path & "\" & File1.FileName) For Input As #1
Do While Not EOF(1)
Input #1, CusData(i)
i = i + 1
Loop
If CusData(9) = "yes" Then
lblM1.Caption = CusData(7)
lblM2.Caption = CusData(10)
End If
Close #1
Next Customer
End Sub
Re: Trouble opening files
Sed
If you put a breakpoint here...
Code:
Private Sub Form_Load()
File1.Path = (App.Path & "\SAVED_DATA\CustomerPI")
i = 0
Customer = File1.ListIndex
Customer = 0
... what is the value of File1.Path?
Does it look something like "c:\myDir\mySubDir\SAVED_DATA\CustomerPI"?
Spoo
Re: Trouble opening files
This code is in the Form_Load event. On Form Load, File1.FileName will be null. (Assuming that File1 is a FileListBox)
You either need to select the Customer'th item before trying to open it in order to set the value of the Filename property.
Code:
File1.Selected(Customer) = True
This will highlight the file being processed in the FileListBox as it goes through each one
or use:
Code:
Open (File1.Path & "\" & File1.List(Customer)) For Input As #1
instead, which will not highlight anything in the FileListBox. I think I'd also set the FileListBox's Enabled property to False so the user is aware that the process is 'automatic' and can't click on an entry, which might mess your loop up if you use the first option above.
BTW It's good practice to use FreeFile to get a File Number rather than assume #1 is available.
Code:
Din InfFile as Integer
intFile = FreeFile
Open .....etc..... As Intfile.
.
. etc
.
Input #intFile ,CusData(i)
Re: Trouble opening files
Quote:
Originally Posted by
Spoo
... what is the value of File1.Path?
2
The "File1.Path" is the first thing I tell the program to get which is (App.Path & "\SAVED_DATA\CustomerPI\") hmmmm I have just realized I should have added the "\" in this part :o but it doesn't matter to much. I have then used this in the OPEN statement by saying:
1) open file1.path (which i gave at the start)
2) add on the file1.FileName (which is what ever it reads in the field depending on the ListIndex the index being "customer")
Doogle, your second code spinet I think might work, are get back to you on the results :thumb:
And just to make this problem a bit more clear, I am basically collecting these 3 entities from each customers file which are all strings and are being used for a simple bar graph which will show profits with in a year (Spoo, this is still the same program for Paving). The entities from each customer are as follows:
1) a "yes" or a "no" (which will show if the customer has paid yet or not)
the second two will be used if the 1st Y/N entity is Yes :bigyello::thumb:
2) The date the job was finished
3) Pay for the job
Each bar will represent a month and the height will be determent on all of the pay added up for each job on that month, which in turn will calculate a overall turn over for the year.
There is also another question I have with a implication I found when planing this form:
How do I split up the date as it comes into the program, as I am not interested in the day just the month and the year at the moment the program should be able to run as follows:
1) program starts and brings in all of the data for the current year
2) the data will then sort its self into months
3) this data can then populate and mold the graph
4) a drop down will allow the user to repeat the process with a previous year
future ideas:
1) each bar could consist of smaller blocks for each customer
2) same as above but with optional further details such as start day and finish and small picture of plan (Spoo will know what I mean by this as he helped the build of the CAD :thumb:)
3) if i was daring enough I could implant optional background graphs of previous years to show a trend :thumb:
- takes a breather - .............. I hope this has now set the right scene :p
Re: Trouble opening files
Quote:
Originally Posted by
Doogle
Code:
Open (File1.Path & "\" & File1.List(Customer)) For Input As #1
instead, which will not highlight anything in the FileListBox. I think I'd also set the FileListBox's Enabled property to False so the user is aware that the process is 'automatic' and can't click on an entry, which might mess your loop up if you use the first option above.
I have tried this but now get a de-bug "run-time error '9' subscript out of range"
on the following line:
Code:
Input #1, CusData(i)
What could this mean??
Re: Trouble opening files
Sed
I'm confused if you are saying that File1.Path = 2
but you and Doogle seem to be connecting on that matter,
so I'll leave it for now.
As for extracting month and year from a date,
you can use the Month and Year functions:
Code:
mydate = #7/2/2011# ' July 2, 2011
mm = Month(mydate) ' returns 7
yy = Year(mydate) returns 2011
Spoo
Re: Trouble opening files
It means that at the time that line was executed that either CusData contains no data or that the value of 'i' is less than the lower bound of CusData() or that it's greater than the upper bound of CusData().
Please show you code as it is now.
Re: Trouble opening files
Quote:
Originally Posted by
Spoo
I'm confused if you are saying that File1.Path = 2
I don't understand why you think that file1.path is being determined as a number ?:ehh: can you please explain as you might be seeing something that I have over looked.
but as far as i am concerned the file1.path = (app.path & "\SAVED_DATA\CustomerPI\") :ehh:
and TY for the date extraction coding :bigyello::thumb:
Re: Trouble opening files
Quote:
Originally Posted by
MartinLiss
Please show you code as it is now.
Hi, MartinLiss this is the code as I currently have it. I will try not to change until someone can come up with answer :)
the bellow code now has no errors which i suppose is a good sign. I realized I made a silly mistake which was not setting the "i" back to 0 before the Next customer.
the problem I now have is it will not write the info into the labels, and this isn't to do with entity (9) being "No" becuase I set them all to "Yes" for test purposes.
Code:
Dim CusData(0 To 10) As String
Dim i As Integer
Dim Customer As Integer
Private Sub Form_Load()
File1.Path = (App.Path & "\SAVED_DATA\CustomerPI")
i = 0
Customer = File1.ListIndex
Customer = 0
For Customer = 0 To File1.ListCount - 1
Open (File1.Path & "\" & File1.List(Customer)) For Input As #1
Do While Not EOF(1)
Input #1, CusData(i)
i = i + 1
Loop
If CusData(9) = "yes" Then
lblM1.Caption = CusData(7)
lblM2.Caption = CusData(10)
End If
Close #1
i = 0
Next Customer
End Sub
Re: Trouble opening files
Sed
Haha .. ok, now we're both confused.
Here is what caused me to reach the conclusion I did in post #6
Quote:
Originally Posted by
seditives
Quote:
Originally Posted by
Spoo
... what is the value of File1.Path
2
The "File1.Path" is the first thing ...
I interpreted that to mean that your reply was 2.
Spoo
Re: Trouble opening files
ok forget the 2 I didn't see that, must be a typo XD sorry, I think this is what my sign qoute means as well, not to sure just put it on to sound cleaver XD but if it does then I am dieing with embarrassment/irony XD
Re: Trouble opening files
It may be an issue of case
Code:
If Ucase(CusData(9)) = "YES" Then
Re: Trouble opening files
Doogle, you have hit the nail directly on the head !! TY =D