Results 1 to 16 of 16

Thread: Creat a file (RESOLVED}

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32

    Creat a file (RESOLVED}

    Why doesn't this code work?
    I have a form on it are a txtBox (for input) three cmdButtons (Save ,Open ,Exit) i am trying to create and save a file using what i typed into the txtBox as the name.

    What i eventualy want to do is create a folder using the input from one txtBox and then create files to put in that folder using another txtBox input sequentually numbered 1 to 3.

    I just made this Form up as a test to see what i need to do .
    VB Code:
    1. Private Sub cmdExit_Click()
    2. 'End the program
    3.     End
    4.    
    5. End Sub
    6.  
    7.  
    8. Private Sub cmdOpen_Click()
    9.     Dim fs As New FileSystemObject
    10.     Dim ts As TextStream
    11.      Set ts = fs.OpenTextFile("C:\", txtName.Text, ".Txt")
    12.      MsgBox ts.ReadLine
    13.      ts.Close
    14.      
    15. End Sub
    16.  
    17. Private Sub cmdSave_Click()
    18.     Dim fs As New FileSystemObject
    19.     Dim ts As TextStream
    20.         Set ts = fs.CreateTextFile("C:/", txtName.Text, ".txt")
    21.         ts.WriteLine "Did this save me"
    22.         ts.Close
    23.        
    24. End Sub
    Last edited by mikrow; Nov 30th, 2002 at 11:48 AM.
    Mike Rowland

  2. #2
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674
    Without trying it the only thing I see is this line

    VB Code:
    1. Set ts = fs.OpenTextFile("C:\", txtName.Text, ".Txt")
    2.  
    3. 'Should be
    4.  
    5. Set ts = fs.OpenTextFile("C:\" & txtName.Text & ".Txt")

    Same goes for the cmdSave_Click

    JO
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32
    Thank you

    I at first thought that was the character that was needed but didn't try it thought i would ask first.
    Mike Rowland

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32
    I tried it and it worked .

    Now i tried this code an got an error ( type mismatch)
    VB Code:
    1. Private Sub cmdExit_Click()
    2. 'End the program
    3.     End
    4.    
    5. End Sub
    6. Private Sub cmdOpen_Click()
    7.     Dim fs As New FileSystemObject
    8.     Dim ts As TextStream
    9.      Set ts = fs.OpenTextFile("C:\My Documents\"  & txtSSNum.Text & "\" & txtName.Text & ".Txt")
    10.      MsgBox ts.ReadLine
    11.      ts.Close
    12.      
    13. End Sub
    14. Private Sub cmdSave_Click()
    15.     Dim fs As New FileSystemObject
    16.     Dim ts As TextStream
    17.         Set ts = fs.CreateFolder("C:/My Documents/" & txtSSNum.Text)  ' Here where it points to (type MisMatch)
    18.         MsgBox "Created folder"
    19.         ts.Close
    20.         Set ts = fs.CreateTextFile("C:/My Documents/" & txtSSNum.Text & "/" & txtName.Text & ".txt")
    21.         MsgBox "created the file" & txtName.Text
    22.         ts.WriteLine "Did this save me"
    23.         MsgBox "Wrote to file"
    24.         ts.Close
    25. End Sub
    Last edited by mikrow; Nov 28th, 2002 at 09:15 AM.
    Mike Rowland

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    hi there mikrow

    when posting code, use te vbcode tags.


    [vbcode]
    MsgBox "u'r code goes here"
    [/vbcode]

    will look Like this

    VB Code:
    1. MsgBox "u'r code goes here"

    makes it a lot clearer
    -= a peet post =-

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32
    Thank you i didn't know how to get to display like vbcode untill you showed me.
    Mike Rowland

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by mikrow
    I tried it and it worked .

    Now i tried this code an got an error ( type mismatch)
    VB Code:
    1. Private Sub cmdExit_Click()
    2. 'End the program
    3.     End
    4.    
    5. End Sub
    6. Private Sub cmdOpen_Click()
    7.     Dim fs As New FileSystemObject
    8.     Dim ts As TextStream
    9.      Set ts = fs.OpenTextFile("C:\My Documents\"  _
    10.      & txtSSNum.Text & "\" & txtName.Text & ".Txt")
    11.      MsgBox ts.ReadLine
    12.      ts.Close
    13.      
    14. End Sub
    15. Private Sub cmdSave_Click()
    16.     Dim fs As New FileSystemObject
    17.     Dim ts As TextStream
    18.         Set ts = fs.CreateFolder("C:/My Documents/" _
    19.        & txtSSNum.Text)      ' Here where it points to (type MisMatch)
    20.         MsgBox "Created folder"
    21.         ts.Close
    22.         Set ts = fs.CreateTextFile("C:/My Documents/"  _
    23.         & txtSSNum.Text & "/" & txtName.Text & ".txt")
    24.         MsgBox "created the file" & txtName.Text
    25.         ts.WriteLine "Did this save me"
    26.         MsgBox "Wrote to file"
    27.         ts.Close
    28. End Sub
    at what line do you get the error ?
    -= a peet post =-

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32
    I pointed it out in the code where i gor the type mismatch error.
    Mike Rowland

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    "ts" is a text stream, and so cannot be used to create a folder.

    It might be simpler to use the MkDIR standard VB function to create the directory, and then use the FSO for the write functions.

    Of course, it might be that you don't need the FSO at all, and standard VB Open, Write/Print, Close functions are enough.

  10. #10
    Member
    Join Date
    Feb 2002
    Posts
    46
    Try this:

    Dim fs

    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.createfolder "C:\My Documents\NewDir"


    notice I'm only using fs with createfolder. I also noticed, not sure if this is causing the problem but certainly will cause another one, you are using forward slashes "/", instead of back slashes "\" in your path.

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32
    Jordan:
    You where right i changed it to MkDir and it worked.

    Now i can't get the if statment to work . I keep getting an undefined error.

    [Highlight=VB]
    Private Sub cmdExit_Click()
    'End the program
    End
    End Sub
    Private Sub cmdOpen_Click()
    Dim fs As New FileSystemObject
    Dim ts As TextStream
    'Open the file and get the contents
    Set ts = fs.OpenTextFile("C:\My Documents\" & txtSSNum.Text & "\" & txtName.Text & ".Txt")
    MsgBox ts.ReadLine
    ts.Close
    End Sub
    Private Sub cmdSave_Click()
    Dim fs As New FileSystemObject
    Dim ts As TextStream
    Dim txtSSNum As Boolean
    'Check to see if folder exist
    If txtSSNum.Value = True Then End Sub 'What is wrong with this line?
    'Make the folder and name it
    MkDir ("c:/My Documents/") & txtSSNum.Text
    MsgBox "Created the folder"
    'Create the file name and put something in it
    Set ts = fs.CreateTextFile("C:/My Documents/" & txtSSNum.Text & "/" & txtName.Text & ".txt")
    MsgBox "created the file" & txtName.Text
    'Put it in the file
    ts.WriteLine "Did this save me"
    MsgBox "Wrote to file"
    ts.Close
    End Sub
    [vb/code][COLOR=red]
    Last edited by mikrow; Nov 29th, 2002 at 06:51 PM.
    Mike Rowland

  12. #12
    Member
    Join Date
    Oct 2002
    Posts
    33
    Try using "Exit Sub" instead of "End Sub"

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32
    SergioV:

    That might work! But i think that my code in the if statement is wrong other than the end/exit Sub statement i think i dimmed txtSSNum twice. And that will give an error.

    What i am saying is that i think checking to see if that folder already exists will take more code than i have now and i don't know how to do it!
    Mike Rowland

  14. #14
    Member
    Join Date
    Oct 2002
    Posts
    33
    Sorry, I really didn't read all your code... simply the "End Sub" jumped to my eyes

    You are checking the value of a variable that I can't understand. You'll need to check by hand if the folder was created or not.

    It might not be the best way to do it, but you can use the MkDir and then check it with a ChDir with On error to do whatever you need in any case.

    That's the way I've done that, is easy, short and it works

    Hope that helps enough.

    Have a nice weekend !

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32
    Thanks for all the help.
    Last edited by mikrow; Nov 30th, 2002 at 11:52 AM.
    Mike Rowland

  16. #16
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    To check if a file exists:
    VB Code:
    1. Function FileExist(ByVal strPath As String) As Boolean
    2.     FileExist = Len(Dir$(strPath)) <> 0
    To check if a Directory exists:
    VB Code:
    1. Function DirExist(ByVal DirPath As String) As Boolean
    2.     On Error Resume Next
    3.     'The above line is only required on Win NT4, and 2K. Remove on Win9x
    4.     DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0
    5. End Function
    To check to see if strFile is a directory or not:
    VB Code:
    1. If (GetAttr(strPath & “\” & strFile) AND vbDirectory) = vbDirectory then
    2. ‘ it’s a directory, not a file

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