|
-
Jul 2nd, 2011, 11:01 AM
#1
Thread Starter
Addicted Member
[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??
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
Last edited by seditives; Jul 2nd, 2011 at 11:06 AM.
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
-
Jul 2nd, 2011, 12:12 PM
#2
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
-
Jul 3rd, 2011, 02:37 AM
#3
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)
Last edited by Doogle; Jul 3rd, 2011 at 03:53 AM.
Reason: Modified answer to match question properly
-
Jul 3rd, 2011, 09:48 AM
#4
Thread Starter
Addicted Member
Re: Trouble opening files
Last edited by seditives; Jul 3rd, 2011 at 09:53 AM.
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
-
Jul 3rd, 2011, 10:17 AM
#5
Thread Starter
Addicted Member
Re: Trouble opening files
 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??
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
-
Jul 3rd, 2011, 10:48 AM
#6
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
-
Jul 3rd, 2011, 10:54 AM
#7
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.
-
Jul 3rd, 2011, 10:56 AM
#8
Thread Starter
Addicted Member
Re: Trouble opening files
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
-
Jul 3rd, 2011, 11:04 AM
#9
Thread Starter
Addicted Member
Re: Trouble opening files
 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
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
-
Jul 3rd, 2011, 11:08 AM
#10
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
 Originally Posted by seditives
 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
-
Jul 3rd, 2011, 11:11 AM
#11
Thread Starter
Addicted Member
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
Last edited by seditives; Jul 3rd, 2011 at 11:16 AM.
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
-
Jul 3rd, 2011, 11:29 AM
#12
Re: Trouble opening files
It may be an issue of case
Code:
If Ucase(CusData(9)) = "YES" Then
-
Jul 3rd, 2011, 11:31 AM
#13
Thread Starter
Addicted Member
Re: Trouble opening files
Doogle, you have hit the nail directly on the head !! TY =D
A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov
Tags for this Thread
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
|