vb6common-dialog

How to save a file in a directory vb6


I have written a code in which data is written to a csv file where the filename and pathname are hardcoded , is it possible to make the button save the file to a user specific location ? Help would be appreciated . Thank you Below is the code of what i have done

Public Sub exportCSV()

    MyRes.MoveFirst  
    strCsvFile = "D:\Mycsv.csv"

    fHndl = FreeFile

        Open strCsvFile For Output As fHndl
         out2 = MyRes.GetFieldNameAt(1)
          Print #fHndl, out2
    MyRes.MoveFirst
     While Not MyRes.IsEOF

        out = MyRes.GetField("ID")
'        Debug.Print out2
       Print #fHndl, out
        MyRes.MoveNext
    Wend

    MsgBox ("Downloaded")

    Close #fHndl

End Sub

Solution

  • You need to insert it prior to assigning your filename. Eg

    MyRes.MoveFirst
    
    CommonDialog1.InitDir = "C:\MyStartFolder"
    CommonDialog1.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
    CommonDialog1.ShowSave
    
    strCsvFile = CommonDialog1.FileName
    
    fHndl = FreeFile
    

    You should include a check that the file name returned is valid.