|
-
Apr 5th, 2008, 11:48 PM
#1
Thread Starter
PowerPoster
[RESOLVED] How To Make A File From A Text Entry?
I need a simple text file, with a filename of "Text1" on "Form4." "Text2" found on "Form4", will contain the data for the simple text file. Also I wish to not have the CommonDialog to be shown, but I think that it must be used in the Project. If the control, is used then make it in the background of the Project, if possible.
-
Apr 5th, 2008, 11:51 PM
#2
Re: How To Make A File From A Text Entry?
-
Apr 5th, 2008, 11:58 PM
#3
Thread Starter
PowerPoster
Re: How To Make A File From A Text Entry?
If "Text1", contains HelpMeRightAtThisPoint1 then the file will be named HelpMeRightAtThisPoint1.TXT.
"Text2", will contain, which for example purposes only, will be. My Name Is Buddy. Buddy, is my name. That will be the data of the simple text file, which will be named as above.
-
Apr 6th, 2008, 12:17 AM
#4
Re: How To Make A File From A Text Entry?
Have you read up on VB File I/O ? In particular the Open and Print statements.
Code:
Dim intFile As Integer
intFile = FreeFile
Open "filename" For Output As intFile
Print #intFile, "This is a line in the File"
Close intFile
Will create a file called: filename and write: This is a line in the file to it, then close the file.
-
Apr 6th, 2008, 01:15 AM
#5
Thread Starter
PowerPoster
Re: How To Make A File From A Text Entry?
I have a question about saving file again. I cannot get the path and the file extension right, but everything else is right, as far as I can see.
PS: I have been able to add the section of my source code, so that someone can see it and tell me what I am doing wrong.
Also please teach me to do it, myself. That is the real reason for learning and nothing more.
Last edited by ThEiMp; Nov 9th, 2008 at 07:44 PM.
-
Apr 6th, 2008, 01:40 AM
#6
Re: How To Make A File From A Text Entry?
Ok, You're opening a file, the name of which is in Text1.Text. You have to enter the full path to the file and the filename into Text1.
For example putting: C:\MyDirectory\MyFile.txt ,will create the file MyFile.txt in your MyDirectory folder (the folder must already exist otherwsie you'll get en error). If myFile already exists it will be overwriten since you're opening the File For Output (if you open the file For Append it will add to the existing contents of the file if it exists, otherwise it will create a new file)
The code in your Form looks OK, in terms of Opening and writing to the file.
If you wanted the file to be created in the same folder as your .exe you can use the App.Path property, for example
Code:
Open App.Path & "\" & Text1.Text For Output As intFile
Using this technique, you'd only have to enter the filename into Text1 and your code would append it to the current application's path
eg
If your Program was running from a folder named MyProgram, and you entered: MyFile.txt in Text1, then the program would create the file
C:\MyProgram\MyFile.txt
There's one 'feature' of App.Path that you should be aware of. If you're runing from a root directory (eg C:\) then App.Path will return C:\ if you're running from another directory (eg C:\Mydirectory) App.Path doesn't return the "\" so to be really safe you should test App.Path to see if the "\" is already there
Code:
If Right$(App.Path, Len(App.Path)) <> "\" Then
strFilename = App.Path & "\" & Text1.Text
Else
strFilename = App.Path & Text1.Text
End If
Open strFile For Output As intFile
-
Apr 6th, 2008, 01:53 AM
#7
Thread Starter
PowerPoster
Re: How To Make A File From A Text Entry?
I cannot get the filename right, with the code that I am using. Can you have a look at my project's source code?
Problem: I get the file to be made, but there isn't any filename, only a file extension and nothing more.
Source Code:
Public Sub Command1_Click()
Unload Form1
Unload Form2
Unload Form3
Unload Form4
Unload Form5
Unload Form6
Form1.Logout_Network.Enabled = True
Form1.Login_Network.Enabled = False
Open App.Path & "\" & Text1.Text & ".txt" For Output As intFile
Print #intFile, Text2.Text
Close intFile
Form1.Show
Exit Sub
OnError:
On Error GoTo 0
0 Exit Sub
Exit Sub
End Sub
-
Apr 6th, 2008, 02:22 AM
#8
Re: How To Make A File From A Text Entry?
if the code is not in form4 then you need to specify that the form for the textbox
Open App.Path & "\" & form4.Text1.Text & ".txt" For Output As intFile
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
-
Apr 6th, 2008, 02:33 AM
#9
Thread Starter
PowerPoster
Re: How To Make A File From A Text Entry?
Actually yes, the TextBox is in "Form4" and the code to I wish to be executed are too on "Form4". But I cannot get right filename.
The filename is this: (.txt) and not the data that is entered into "Text1.Text".
-
Apr 6th, 2008, 02:40 AM
#10
Re: How To Make A File From A Text Entry?
Perhaps the fact that you're unloading Form4 in the Command1_Click event before you're executing the code has something to do with the problem
-
Apr 6th, 2008, 03:26 AM
#11
Re: How To Make A File From A Text Entry?
Perhaps the fact that you're unloading Form4 in the Command1_Click event before you're executing the code has something to do with the problem
lol i missed that
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
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
|