Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post

    Vb-Write Multiple lines to a text file

    Hey,

    I have a porgram that writes user entered info into a text file from a text box, it also writes each new submission on a new line, however I would like to expand that from writing one line at a time to multiple lines at once for more information like for example, a name one one line the persons phone number on the next...that sorta thing. Ive googled around and this is the closest thing I can find, the only problem is that it overwrites the info thats already there.

    Code:
    'VBScript
    'Create a text file
    
    DIM fso, GuyFile
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set GuyFile = fso.CreateTextFile("e:\ezine\newsletter13.txt", True)
    GuyFile.WriteLine("Ezine 13 Available Jan 11th") 
    GuyFile.Write ("Next week will be about creating user accounts") 
    GuyFile.Write (" More on same line") 
    GuyFile.Close
    is there a way to modify the above script so that it writes after the information that was already entered?
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check out the documentation for fso.CreateTextFile(), the second parameter is overwrite, so if you set it to false it won't.

    shmoove

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    ah thanks

    EDIT: Hmm actually, that jsut doent overwrite the file istelf, when the file exists the program freaks out and dies. I want to be able to add additional information into the file without losing whats in there already
    Last edited by Bry Man; 12-29-2004 at 07:53 PM.
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Thats because you are using the CreateTextFile method. You need to use the OpenTextFile method if you want to append data.
    OracleGuy

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    Hmm I tried that but it is giving me an error, here what I did, first I put the origonal code that I posed into a button, then I put the same code into another button only changed it to opentextfile instead of create. heres the code for the two buttons

    Button 1:
    Code:
    'VBScript
    'Create a text file
    
    DIM fso, GuyFile
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set GuyFile = fso.CreateTextFile("c:\test.txt", True)
    GuyFile.WriteLine("Ezine 13 Available Jan 11th") 
    GuyFile.Write ("Next week will be about creating user accounts") 
    GuyFile.Write (" More on same line") 
    GuyFile.Close
    Button 2:

    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set GuyFile = fso.OpenTextFile("c:\test.txt")
    GuyFile.WriteLine("Ezine 13 Available Jan 11th") 
    GuyFile.Write ("Next week will be about creating user accounts") 
    GuyFile.Write (" More on same line") 
    GuyFile.Close
    When i hit button 2 it gives me this error' run time error 54: Bad file mode' and then highlights this line in button 2

    Code:
    GuyFile.WriteLine("Ezine 13 Available Jan 11th")
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Try specifying the IOMode parameter on the OpenTextFile method. The details for it are on the MSDN. If you specify the mode that you will be appending, it will probably work because the default might be ForReading.
    OracleGuy

  • #7
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Yep, and I would do this just in case the file isn't there. And rather than writing over and over, I would concatenate the strings and add in a \r\n for the line breaks as needed. That way you could easily prepare the string before passing it to the function.

    Code:
    <html>
    	<head>
    		<script type="text/vbscript">
    		
    			Function txtLine(txt,fpath)
    			
    			'VBScript
    			'Create or append a text file
    			
    				DIM fso, flg, floc
    				Set fso = CreateObject("Scripting.FileSystemObject")
    				fpath="c:\test.txt"
    				flg=fso.FileExists(fpath)
    			
    				If flg Then
    					Set floc= fso.OpenTextFile(fpath, 8)
    					floc.WriteLine(txt)
    					floc.Close
    				Else
    					Set floc = fso.CreateTextFile(fpath,true)
    					floc.WriteLine(txt) 
    					floc.Close
    				End If
    			
    			End Function
    		
    		</script>
    	</head>
    	<body>
    		<form>
    			<input type="button"  onclick='vbscript:txtLine "Its beddah with Cheddah From The Nebbah Kinezah","C:\Test.txt" ' value="Gibberish" />
    		</form>
    	</body>
    </html>
    Basscyst
    Last edited by Basscyst; 12-31-2004 at 01:17 AM.
    Helping to build a bigger box. - Adam Matthews

  • #8
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    Ill look into that in a bit ive been a little busy with my web host, they are kinda givin me some troubles with my account and resetting it for me. Thanks Basscyst I was going to look into making a script that checked if the file exists and if not create one that should be quite useful. Whats with all the html in there tho?
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #9
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    I added to it a bit, it's a bit more re-useable now. I just threw the HTML in so you could see that it was working.

    Basscyst
    Last edited by Basscyst; 12-31-2004 at 01:27 AM.
    Helping to build a bigger box. - Adam Matthews

  • #10
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    Cool, this is going to be used in a prog that keeps track of members in my clan for Kings Of Chaos because the leaders are having a time keeping track of all of us. So far I have it so you jsut type in a name into a text box and hit enter but id like to have a bit more than that like their name their link and their rank etc. Im also looking to add the ability to have more than one file, possibly by diming a variable for it and such which souldnt be very hard at all.
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #11
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    You could just use a flat text db, and store the data that way. . .if I'm understanding correctly, you are just keeping track of people. . .

    So you could setup your text file like this:

    Name|Age|Rank|Location|Whatever
    Adam|27|General|CA|Happy
    Bob|99|Peon|WA|Sad

    See where I'm going with this? That way you only need one file. It's a nice cheap and easy alternative to a database when you just have a bit of data. . . .well it is a database. .

    Basscyst
    Last edited by Basscyst; 12-31-2004 at 01:29 AM.
    Helping to build a bigger box. - Adam Matthews

  • #12
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You could use a database to store all of this instead of a text file. (just a suggestion i'm sure you have a reason why your using a text file I just thought i'd throw that out there)

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    uhm, no need for all that code to check if the file exists.
    opentextfile takesathird (boolean) argument which indicates the creation of the file if it doesn't exist

    Code:
    Set floc= fso.OpenTextFile(fpath, 8, True)
    will do the whole trick for you
    I am the luckiest man in the world

  • #14
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    Sorry fot not repling my gmail notifyer...didnt notify me of new posts. Im using a text file because I load the text file intoa list, but now that think of it having more than one line per user being loaded nt the list may be a bit confusing unless I could find a way where I could click the users name and have it display their information somehow....im kinda getting ahead of myself but Im not sure when I can get to work on all this becuase I wrenched my back and cant sit at the computer for a long time...I have a program however that has three text boxes , one for your name email and password and each text box corresponds to its own colum in the list..only thing is I didnt make it so I dunno how it was done but I think that would be almost perfect.
    Last edited by Bry Man; 01-01-2005 at 06:01 AM.
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #15
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    write to text box

    hi
    i need a little help.
    i have this
    Private Sub write_to_file(ByVal fname As String, ByVal buf As String)
    Dim FS
    Dim s
    Dim t
    Set FS = CreateObject("Scripting.FileSystemObject")
    Set s = FS.OpenTextFile(fname, 8, True)
    s.WriteLine (buf)
    s.Close
    Set FS = Nothing
    End Sub

    but i want to write in a textbox instead of a text file!
    how is it possible. any help would be useful


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •