I have a vb6 program that imports a comma delimited data file for calculations and formulas and then has a save as option. The program only saves in text format and I can't locate the code for the save as option
I take it you did not try a simple search for saveas ?
On the main form there is a menu option for save as
Code:
Private Sub mnuSaveAs_Click()
'//what kind of active form do we have?
If Me.ActiveForm Is Nothing Then
Exit Sub
End If
If TypeOf Me.ActiveForm Is frmDRFFile Then
'// save a drf file
Dim fdrf As frmDRFFile
Set fdrf = Me.ActiveForm
fdrf.SaveAs
ElseIf TypeOf Me.ActiveForm Is frmTemplate Then
'// save a template file
Dim ft As frmTemplate
Set ft = Me.ActiveForm
ft.SaveAs
End If
End Sub
It calls either this routine in the frmdrffile
Code:
Public Sub SaveAs()
On Error GoTo ErrLabel
cd.ShowSave
Dim fso As New FileSystemObject
If fso.FileExists(cd.FileName) Then
Dim nRet As Integer
nRet = MsgBox("The file already exists, overwrite?", vbQuestion Or vbYesNo, "Overwrite")
If nRet = vbNo Then
Exit Sub
End If
End If
Dim ts As TextStream
Set ts = fso.OpenTextFile(cd.FileName, ForWriting, True)
ts.Write Me.txtDRFFile.Text
ts.Close
Set ts = Nothing
Set fso = Nothing
Exit Sub
ErrLabel:
End Sub
or this one in the frmtemplate file depending on which form is active
Code:
Public Sub SaveAs()
On Error GoTo ErrLabel
cd.ShowSave
Dim fso As New FileSystemObject
If fso.FileExists(cd.FileName) Then
Dim nRet As Integer
nRet = MsgBox("The file already exists, overwrite?", vbQuestion Or vbYesNo, "Overwrite")
If nRet = vbNo Then
Exit Sub
End If
End If
Dim ts As TextStream
Set ts = fso.OpenTextFile(cd.FileName, ForWriting, True)
ts.Write Me.txtTemplate.Text
ts.Close
Set ts = Nothing
Set fso = Nothing
Exit Sub
ErrLabel:
End Sub
Not sure why whoever coded it did it that way. The only difference in the two routines is which text box it uses. Would have made more sense to have a single routine that accepts the textbox to use as a parameter. Then again I would not be using FSO either
Not sure why whoever coded it did it that way. The only difference in the two routines is which text box it uses. Would have made more sense to have a single routine that accepts the textbox to use as a parameter. Then again I would not be using FSO either
How would I change the code to save as a comma delimited file or have the option for both csv and txt
the above codes only save the content of the textbox to file, i very much doubt anyone here can help without knowing exactly what the content of the textbox would be, like where commas have to be inserted or if some character (eg TAB) can be replaced, either in the textbox or when writing to file
if you post a sample of the textfile as saved and another with required result someone may help
the posted project is quite large and we have no real idea what it is supposed to do
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
here is a data file that the program opens and extracts fields from for analyzing. It has the .DRF extension. There are 1435 fields in the data file. when you run the program and click the save button the text file is the result. The reason I want a comma delimited file is to import to excel.
Of course given that the original DRF file appears to be a csv file and your goal is to import to excel then why not just skip the program completely and import the drf file into excel?
tried to PM you but i think you folder is full? Yes I have imported the file directly to excel. the problem became getting all the formulas on a spreadsheet formatted properly. very difficult with no excel experience
tried to PM you but i think you folder is full? Yes I have imported the file directly to excel. the problem became getting all the formulas on a spreadsheet formatted properly. very difficult with no excel experience
I generally do not respond to PM requests. Best to post your questions and answers in the forums where others can help and benefit.
If you can not get it to work in Excel then your option is to add the required commas and quotes back into the data and then write it to a csv that can be imported if that is what you want to do.
here is a data file that the program opens and extracts fields from for analyzing. It has the .DRF extension. There are 1435 fields in the data file. when you run the program and click the save button the text file is the result. The reason I want a comma delimited file is to import to excel.
As DataMiser noted, you can import that file directly into Excel. But with over 1400 columns, older Excel versions will stop reading at 256 columns. Is that the reason you cannot import it?
Btw csv is a very problematic file format. Use tab-delimited, it's much easier to handle.
if you can post a sample of the text file that is saved and make a matching sample csv as desired, to compare, someone may be able to do the conversion, for you
but running the project or inspecting the input data seems to be irrelevant to your request
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