Results 1 to 13 of 13

Thread: [RESOLVED] Trouble opening files

  1. #1

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Resolved [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

  2. #2
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

  4. #4

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Trouble opening files

    Quote Originally Posted by Spoo View Post
    ... 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 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


    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

    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 )
    3) if i was daring enough I could implant optional background graphs of previous years to show a trend

    - takes a breather - .............. I hope this has now set the right scene
    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

  5. #5

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Trouble opening files

    Quote Originally Posted by Doogle View Post
    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

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  8. #8

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Trouble opening files

    Quote Originally Posted by Spoo View Post
    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 ? 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\")

    and TY for the date extraction coding
    A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov

  9. #9

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Trouble opening files

    Quote Originally Posted by MartinLiss View Post
    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

  10. #10
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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 View Post
    Quote Originally Posted by Spoo View Post
    ... 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

  11. #11

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    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

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Trouble opening files

    It may be an issue of case
    Code:
    If Ucase(CusData(9)) = "YES" Then

  13. #13

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    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
  •  



Click Here to Expand Forum to Full Width