Question Locked

how to save text box data to a new file,not in same file

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

i am trying to save my text boxes data to a new file through command button,but text boxes data is saved in the same file it saved last this is my code

Public Sub SaveTextBoxData(ByVal formSource As Form, ByVal fileName As String)
'This will save all data currently in ant text boxes on the specified form

Dim counter As Control 'used to cycle through the controls on a form
Dim fileNum As Integer 'holds a handle to the file

'open the file for writing
fileNum = FreeFile
Open fileName For Output As #fileNum

'cycle through the controls on the form
For Each counter In formSource.Controls

'is this control a text box
If TypeOf counter Is TextBox Then

'save its Text property
Print #fileNum, counter.Name + "=" + counter.Text
End If
Next counter
Close #fileNum
End Sub

Private Sub savemnu_Click()
CommonDialog1.ShowSave
Call SaveTextBoxData(Me, App.Path + ".txt")
End Sub

so please help me so that each time when i click save a common dialog box should appear and i can save the forms content to a new file..

4 replies

by gcamilleri

It seems you are using the same filename each time you click the command button. Assigning a different file handler will not make a difference if the filename remains the same.

You could maybe assign the string equivalent of the #fileNum variable to a string variable and use it as the extension of the filename.

by robaaaaaa2

Add the MS Common Dialog conrtol to the project components.
Place a commondialog control on your form.
In your button_click code insert:

cmndlg1.CancelError = True
cmndlg1.showopen
strFileName = cmndlg1.FileName

then call your code.

by justdave

The problem is in this line of code:

Call SaveTextBoxData(Me, App.Path + ".txt")

You need to reference the file name you give it in the CommonDialog1 Change that line to read:

Call SaveTextBoxData(Me, CommonDialog1.filename + ".txt")

This discussion is closed to new replies.