URGENT!!!! dbf to csv using visual basic 6

  • 13 years ago

    Hello Big Smile

    I am trying to extract all the information from DBF in foxpro to excel's CSV but I have an error and I have not found where the error is, please remember that I am just a beginner Embarrassed

     

    the code is here

    Option Explicit
    Dim mstrConnectionString As String
    Dim FExists As Boolean

    Private Sub Form_Load()
       

      
       Dim DBFileName As String, PathDBFileName As String
      DBFileName = "reg501.dbf"
      PathDBFileName = App.Path

       mstrConnectionString = "Driver={Microsoft Visual FoxPro Driver};" & _
                   "SourceType=DBF;" & _
                   "SourceDB=" & PathDBFileName & ";" & _
                   "Exclusive=No"
    '   dcexport.Visible = False
      
       dcexport.RecordSource = "SELECT * FROM " & DBFileName
      

    End Sub


    Private Sub cmdExport_Click()
      Dim fieldnum As Integer
      Dim cellstring As String
      Dim headstring As String
      Dim daAnswer
     
     cdexport.CancelError = True
     On Error GoTo SaveErr

      With cdexport
        .DialogTitle = "Export to CSV"
        .Filter = "Excel Import File (*.csv)|*.csv"
        .FileName = "tester"
        .ShowSave
      End With
     
      FileExists (cdexport.FileName)
      If FExists = True Then
        daAnswer = MsgBox("File Exists. Overwrite?", vbYesNo + vbQuestion, "File Exists")
        If daAnswer = vbNo Then
          cmdExport_Click
        End If
      End If
        
      Open cdexport.FileName For Output As #1
     Print #1, "SOME TEXT I NEED TO PUT ON"
     
        
      dcexport.Recordset.Bookmark = dgexport.Bookmark
        
      For fieldnum = 0 To dgexport.Columns.Count - 1 'Routine for writing the header to the CSV File
        headstring = headstring & dgexport.Columns(fieldnum).Caption & ","
      Next
      Print #1, headstring

      Do While dcexport.Recordset.EOF = False  'For each row in the datacontrol
        For fieldnum = 0 To dcexport.Recordset.Fields.Count - 1
            cellstring = cellstring & dcexport.Recordset.Fields(fieldnum).Value & "," ' comma is for the csv format
        Next
        Print #1, cellstring
        cellstring = ""

        dcexport.Recordset.MoveNext
      Loop
      Close #1
     

    SaveErr:
        If Err <> 32755 Then ' 32755 : Cancel was selected
        End If
        Exit Sub

    End Sub

    'I claim no credit for this routine. I discovered this on PSC a long time ago, and
    'it has been part of my applications ever since - short and neat!

    Function FileExists(ByVal FileName As String)

       Dim Exists As Integer
      
       On Local Error Resume Next 'If some problem continue, code handles problems inherintly
       Exists = Len(Dir(FileName$)) 'Dir returns either a null string (len 0) or a filename
       On Local Error GoTo 0
     If Exists = 0 Then 'Null string?
        FileExists = False
        FExists = False
    Else
        FileExists = True
        FExists = True
    End If
    End Function

     

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Weeks of coding can save you hours of planning.”