Results 1 to 14 of 14

Thread: save as comma delimited csv file

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2016
    Posts
    49

    save as comma delimited csv file

    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
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: save as comma delimited csv file

    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

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: save as comma delimited csv file

    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

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2016
    Posts
    49

    Re: save as comma delimited csv file

    Quote Originally Posted by DataMiser View Post
    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

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: save as comma delimited csv file

    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

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: save as comma delimited csv file

    Quote Originally Posted by jpatierno View Post
    How would I change the code to save as a comma delimited file or have the option for both csv and txt
    A CSV file is a text file so I have no idea what you are really asking there.

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2016
    Posts
    49

    Re: save as comma delimited csv file

    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.
    Attached Files Attached Files

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: save as comma delimited csv file

    Well you would basically need to add the commas (and quotes if needed) back into your data before it gets written to the file.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: save as comma delimited csv file

    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?

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2016
    Posts
    49

    Re: save as comma delimited csv file

    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

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: save as comma delimited csv file

    Quote Originally Posted by jpatierno View Post
    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.

  12. #12
    Addicted Member jj2007's Avatar
    Join Date
    Dec 2015
    Posts
    205

    Re: save as comma delimited csv file

    Quote Originally Posted by jpatierno View Post
    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.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: save as comma delimited csv file

    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

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: save as comma delimited csv file

    The files.zip attachment he posted in post #7 has the input and the output files included.

Posting Permissions

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



Click Here to Expand Forum to Full Width