[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.
Re: How To Make A File From A Text Entry?
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.
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.
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.
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
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
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
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".
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 :)
Re: How To Make A File From A Text Entry?
Quote:
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