Results 1 to 8 of 8

Thread: creating a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    creating a text file

    Hi all,
    I am trying to create a text file in VB, but getting an error ...
    User -defned type not defined. suggest me ...

    Code:
     
    Private Sub Command1_Click()
    
    Dim FS As New FileSystemObject
       Dim Stream As TextStream
    
      Set Stream = FS.CreateTextFile(App.Path & "\ashoks.txt", True)
      
         Stream.Close
     
    End Sub

    Thanks....

  2. #2
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: creating a text file

    This is adding the time and date to it, so that it's a completely unique file. Hope it helps

    vb Code:
    1. Private Sub Command2_Click()
    2. Dim frfile As Long
    3. Dim filetimedate As String
    4.  
    5. frfile = FreeFile
    6. filetimedate = Format(Now(), "(mm dd yyyy) (hh-mm-ss)")
    7.  
    8. Open App.Path & "\log" & filetimedate & ".txt" For Output As #frfile
    9. Print #frfile, Text1.Text
    10. Close #frfile
    11.  
    12. Text1.Text = ""
    13. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    Re: creating a text file

    yeah thats working, thanks.

    but what is the difference b/w above and this process

    how to implemet same with above functions.




    Thanks...

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: creating a text file

    to use FSO you need to add a reference to microsoft scripting runtime
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: creating a text file

    Quote Originally Posted by westconn1
    to use FSO you need to add a reference to microsoft scripting runtime
    The lack of this reference is what cause your error.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    Re: creating a text file

    From above replies I realise that I need to add FSO reference to my code, but i am not aware of the same. please update below code with FSO reference ....

    Code:
    Private Sub Command1_Click()
    
    Dim FS As New FileSystemObject
       Dim Stream As TextStream
    
      Set Stream = FS.CreateTextFile(App.Path & "\ashoks.txt", True)
      
         Stream.Close
     
    End Sub
    Compile erroe: User -defned type not defined


    Thanks....

  7. #7
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: creating a text file

    To add Microsoft Scripting Runtime, click on Projects->References and set the ref to it

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    7

    Re: creating a text file

    Dear all,

    I got the solution for above problem. here is solution


    Code:
    Private Sub Command1_Click()
    Call myfile
    
    End Sub
    
    Public Sub myfile()
    Set myFSO = CreateObject("Scripting.FileSystemObject")
    'Dim ts As TextStream
    
    Set ts = myFSO.CreateTextFile(App.Path & "\ashok.txt", True)
    'ts.Close
    
    'Set ts = myFSO.OpenTextFile(App.Path & "\ashok.txt", Forwriting, True)
    ts.WriteLine "hi ashok"
    ts.WriteLine "bye ashok"
    ts.Close
    End Sub

    Thanks to all....

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