Can anyone give me a code for when a button is clicked, ie, File, Save As, the files saves to your desired location
I've been reasearching and researching and nothing
05-10-2006 at 11:22 PM
|
yronium Level: Moderator Registered: 14-04-2002 Posts: 907
Re: saving a file
Hello. 'Saving a file' is not an operation. What do you mean to do with your click? Writing the contents of a variable/an array/a picturebox/a control/an entire form/a recordset/whatever else of yours into an existing file? And in what format? Create a new file with the above contents? Provide user the feature to choose the location in which the new file have to be created? Simply copying an existing file in another location? Create a periodical backup of a file? Create an empty file?
In poor words: what are you exactly intending to do?
____________________________
Real Programmer can count up to 1024 on his fingers
06-10-2006 at 06:49 AM
|
JLRodgers Level: Moderator Registered: 04-04-2002 Posts: 1616
Re: saving a file
Just like was said... if you want to save a file, you have to manually write every byte/word to a blank file.
Which means you have to open the filename for writing... print the stuff to the file, and close it.
____________________________ Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
what i basically want to do is, when some results appear in the List Box, I want the user to be able to save those results in, lets say, .txt or .rtf file.
how do I do that?
Private Sub Command1_Click()
Dim ff As Integer, i As Integer
Dim ln As String
'to save it
ff = FreeFile 'that is your file number
Open "C:\test.txt" For Output As #ff 'opening the file (even if it does not exist yet) for writing - output
'write each list item with the loop
Print #ff, Text1.Text
Close #ff 'dont forget to close the file
End Sub
To read the File:
Private Sub Command2_Click()
Dim ff As Integer
Dim ln As String
'to read it
ff = FreeFile
List1.Clear
Open "c:\test.txt" For Input As #ff 'open it for reading - input
Do Until EOF(ff) 'read until the end of file
Line Input #ff, ln 'input each line
List1.AddItem ln 'add it to the list
Loop
Close #ff
End Sub
Or you can use the common dialog control to save it like this:
Module:
---------
Public Function SaveText(Text As String, FileName As String) As Boolean
Dim sTemp As String
sTemp = Text
Open FileName For Append As #1 'Opening the file to SaveText
Print #1, sTemp 'Printing the text to the file
Close #1 'Closing
Exit Function
End Function
Next add A Textbox and a Command button on to your Form. And add the CommonDialog Control on to your Form set the name to cdlg
Add this code on to the Form:
---------------------------------
Private Sub Command1_Click()
cdlg.ShowSave
SaveText Text1, cdlg.FileName & ".txt"
End Sub