Results 1 to 7 of 7

Thread: [RESOLVED] Error To Create File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Resolved [RESOLVED] Error To Create File

    Here is very simple question

    I have intention to create a new file if the file is not found in the application path; but its return an error message.

    Here is my code

    [vb]
    Private Sub Form_Load()
    If Dir$("c:\trial.txt") = "" Then
    Open "c:\trial.txt" For Input As #1
    Print #1, FormatDateTime(Now(), vbShortDate)
    Close #1
    Else
    Open "c:\trial.txt" For Append As #1
    Print #1, FormatDateTime(Now(), vbShortDate)
    Close #1
    End If


    End Sub

    [/vb]

    Actually, it is just to store short dates only. If the file is successfully created, how to count the number of days inside (the same date should be entered once only).

    Thanks

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Error To Create File

    You're trying to open a file that doesn't exist for Input, it needs to be opened for Output if you want to create it:
    VB Code:
    1. Private Sub Form_Load()
    2.     If Len(Dir$("c:\trial.txt")) Then
    3.         Open "c:\trial.txt" For Output As #1
    4.             Print #1, FormatDateTime(Now(), vbShortDate)
    5.         Close #1
    6.     Else
    7.         Open "c:\trial.txt" For Append As #1
    8.             Print #1, FormatDateTime(Now(), vbShortDate)
    9.         Close #1
    10.     End If
    I don't really understand your second question (if it was a question)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Error To Create File

    I just want to count how many days the form has been used. Duplicate dates will be assume as one day only. Total days is now()-earliest day. Thats all. How the codes look like?
    Last edited by guy_toforget; Apr 19th, 2006 at 08:23 PM.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Error To Create File

    Do you want the number of days on which your program has been used? Or the number of days since the first and last time it's been used?

    e.g:

    10/04/2006
    15/04/2006
    20/04/2006

    Would be 3 days of use, but 10 days since first used.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Error To Create File

    It should be 3 days, not 10 times

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Error To Create File

    something like this then perhaps:

    VB Code:
    1. Private Function CountDays(ByVal sFile As String) As Long
    2.     Dim sDates() As String, N As Long, I As Long
    3.     Open sFile For Input As #1
    4.         sDates = Split(Input(LOF(1), 1), vbCrLf)
    5.     Close #1
    6.    
    7.     For N = LBound(sDates) To UBound(sDates) - 1
    8.         For I = N + 1 To UBound(sDates)
    9.             If sDates(N) = sDates(I) Then Exit For
    10.         Next I
    11.         CountDays = CountDays + Abs(I > UBound(sDates))
    12.     Next N
    13.     CountDays = CountDays + 1
    14. End Function
    15.  
    16. Private Sub Form_Load()
    17.     Debug.Print CountDays("C:\Dates.txt")
    18. End Sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Error To Create File

    thanks bushmobile. you are so great

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