|
-
Nov 27th, 2002, 03:14 PM
#1
Thread Starter
Member
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:
Private Sub cmdExit_Click()
'End the program
End
End Sub
Private Sub cmdOpen_Click()
Dim fs As New FileSystemObject
Dim ts As TextStream
Set ts = fs.OpenTextFile("C:\", txtName.Text, ".Txt")
MsgBox ts.ReadLine
ts.Close
End Sub
Private Sub cmdSave_Click()
Dim fs As New FileSystemObject
Dim ts As TextStream
Set ts = fs.CreateTextFile("C:/", txtName.Text, ".txt")
ts.WriteLine "Did this save me"
ts.Close
End Sub
Last edited by mikrow; Nov 30th, 2002 at 11:48 AM.
Mike Rowland
-
Nov 27th, 2002, 03:49 PM
#2
Fanatic Member
Without trying it the only thing I see is this line
VB Code:
Set ts = fs.OpenTextFile("C:\", txtName.Text, ".Txt")
'Should be
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
-
Nov 27th, 2002, 04:04 PM
#3
Thread Starter
Member
Thank you
I at first thought that was the character that was needed but didn't try it thought i would ask first.
-
Nov 27th, 2002, 06:02 PM
#4
Thread Starter
Member
I tried it and it worked .
Now i tried this code an got an error ( type mismatch)
VB Code:
Private Sub cmdExit_Click()
'End the program
End
End Sub
Private Sub cmdOpen_Click()
Dim fs As New FileSystemObject
Dim ts As TextStream
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
Set ts = fs.CreateFolder("C:/My Documents/" & txtSSNum.Text) ' Here where it points to (type MisMatch)
MsgBox "Created folder"
ts.Close
Set ts = fs.CreateTextFile("C:/My Documents/" & txtSSNum.Text & "/" & txtName.Text & ".txt")
MsgBox "created the file" & txtName.Text
ts.WriteLine "Did this save me"
MsgBox "Wrote to file"
ts.Close
End Sub
Last edited by mikrow; Nov 28th, 2002 at 09:15 AM.
Mike Rowland
-
Nov 27th, 2002, 06:20 PM
#5
-= B u g S l a y e r =-
hi there mikrow 
when posting code, use te vbcode tags.
[vbcode]
MsgBox "u'r code goes here"
[/vbcode]
will look Like this
VB Code:
MsgBox "u'r code goes here"
makes it a lot clearer
-
Nov 27th, 2002, 07:22 PM
#6
Thread Starter
Member
Thank you i didn't know how to get to display like vbcode untill you showed me.
-
Nov 28th, 2002, 03:16 AM
#7
-= B u g S l a y e r =-
Originally posted by mikrow
I tried it and it worked .
Now i tried this code an got an error ( type mismatch)
VB Code:
Private Sub cmdExit_Click()
'End the program
End
End Sub
Private Sub cmdOpen_Click()
Dim fs As New FileSystemObject
Dim ts As TextStream
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
Set ts = fs.CreateFolder("C:/My Documents/" _
& txtSSNum.Text) ' Here where it points to (type MisMatch)
MsgBox "Created folder"
ts.Close
Set ts = fs.CreateTextFile("C:/My Documents/" _
& txtSSNum.Text & "/" & txtName.Text & ".txt")
MsgBox "created the file" & txtName.Text
ts.WriteLine "Did this save me"
MsgBox "Wrote to file"
ts.Close
End Sub
at what line do you get the error ?
-
Nov 28th, 2002, 09:12 AM
#8
Thread Starter
Member
I pointed it out in the code where i gor the type mismatch error.
-
Nov 28th, 2002, 10:02 AM
#9
Frenzied Member
"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.
-
Nov 28th, 2002, 10:12 AM
#10
Member
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.
-
Nov 29th, 2002, 06:48 PM
#11
Thread Starter
Member
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
-
Nov 29th, 2002, 07:26 PM
#12
Member
Try using "Exit Sub" instead of "End Sub"
-
Nov 29th, 2002, 07:44 PM
#13
Thread Starter
Member
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!
-
Nov 29th, 2002, 08:21 PM
#14
Member
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 !
-
Nov 30th, 2002, 11:09 AM
#15
Thread Starter
Member
Last edited by mikrow; Nov 30th, 2002 at 11:52 AM.
Mike Rowland
-
Dec 2nd, 2002, 04:28 AM
#16
Frenzied Member
To check if a file exists:
VB Code:
Function FileExist(ByVal strPath As String) As Boolean
FileExist = Len(Dir$(strPath)) <> 0
To check if a Directory exists:
VB Code:
Function DirExist(ByVal DirPath As String) As Boolean
On Error Resume Next
'The above line is only required on Win NT4, and 2K. Remove on Win9x
DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0
End Function
To check to see if strFile is a directory or not:
VB Code:
If (GetAttr(strPath & “\” & strFile) AND vbDirectory) = vbDirectory then
‘ 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|