Results 1 to 19 of 19

Thread: CSV --> EXCEL --> DBASEIII with Visual Basic 6

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    CSV --> EXCEL --> DBASEIII with Visual Basic 6

    hi there!

    I have a problem converting a CSV file to Excel and then converting it again to DBASEIII.
    So my CSV File looks like this the first row are the column names and the seperator is the semicolon.

    How can i convert it to EXCEL and then to DBASEIII using Visual Basic 6.

    It will be really nice if u could help me with this.

    thanks a lot!

    P.S. I just started programming VB6 im very new to this...

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    Weocome to the Forums.

    I have an Office Development FAQ that has several helpful items.

    How do I convert a csv file to an xls fileformat?

    How do I transfer data between Access and Excel?

    The second example is not exact as you need it but if you used a linked table in Access to your DB III table you can use that linked table as the destination table.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    hi again!

    well the code there is not working well...
    i have an error on the DataType:=xlDelimited
    P.S. i just started programming Visual Basic 6 it will be nicer if u can
    explain in detail to me...

    thanks a lot!

    VB Code:
    1. Private Sub cmdConvert_Click()
    2.  
    3.     On Error GoTo ErrHandler
    4.        
    5.         Dim oApp As Excel.Application
    6.         Dim oWB As Excel.Workbooks
    7.    
    8.         Set oApp = New Excel.Application
    9.    
    10.         Set oWB = oApp.Workbooks.Open(FileName:="& fileSource &", Origin:=xlMSDOS, DataType:=xlDelimited, Comma:=False)
    11.         oWB.SaveAs FileName:="& toFile &", FileFormat:=xlWorkbookNormal
    12.         oWB.Saved = True
    13.         oWB.Close
    14.         Set oWB = Nothing
    15.         oApp.Quit
    16.         Set oApp = Nothing
    17.    
    18.     Exit Sub
    19.    
    20. ErrHandler:
    21.     If Err <> 0 Then
    22.         MsgBox Err.Source & vbCrLf & _
    23.             Err.Description & vbCrLf & _
    24.             Err.Number, , "Error"
    25.     End If
    26. End Sub

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    Did you add a reference to MS Excel xx.0 Object Library like in the FAQ?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    yes i did that but it still not working.
    BTW: by Comma:= True.
    well my delimiter is semicolon should i put this as FALSE?

    thanks a lot!

    can u please look at my code and try it to ur compiler
    VB Code:
    1. Private Sub cmdConvert_Click()
    2.  
    3.     On Error GoTo ErrHandler
    4.        
    5.         Dim oApp As Excel.Application
    6.         Dim oWB As Excel.Workbook
    7.    
    8.         Set oApp = New Excel.Application
    9.         Set oWB = oApp.Workbooks.Open(FileName:="& fileSource &", Origin:=xlMSDOS, DataType:=xlDelimited, Comma:=True)
    10.         oWB.SaveAs FileName:="& toFile &", FileFormat:=xlWorkbookNormal
    11.         oWB.Saved = True
    12.         oWB.Close
    13.         Set oWB = Nothing
    14.         oApp.Quit
    15.         Set oApp = Nothing
    16.    
    17.     Exit Sub
    18.    
    19. ErrHandler:
    20.     If Err <> 0 Then
    21.         MsgBox Err.Source & vbCrLf & _
    22.             Err.Description & vbCrLf & _
    23.             Err.Number, , "Error"
    24.     End If
    25. End Sub

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    Can you telll us what do you mean by "not working"?

    If the delimiter is a semicolon then the other delimiters would be false as semicolon would be true.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    when i convert the file i have this error message: 'look at the jpeg file'
    Err.
    Function or Variable Expected --> higlighted ".OpenText"
    Attached Images Attached Images  

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    Somehow in your code doing the copy/paste you changed the function.

    VB Code:
    1. Set oWB = oApp.Workbooks.[hl]Open[/hl](FileName:="& fileSource &", Origin:=xlMSDOS, DataType:=xlDelimited, Comma:=True)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    hhmm...
    good i'll explain, if i use:
    OpenText: i get the error message: Function or Variable Expected like the jpeg file shows.

    if i use:
    Open: the i got an error on: DataType:xlDelimited.

    dont know how to solve this problem.

    did u tried my code to compile on ur desktop?

  10. #10
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    I actually prefer writing my own routines to import csvs into Excel. I find that they are a lot easier to customize (i.e. skipping columns or adding calculated data). Here's an example that creates a new workbook from a passed filename and application instance. It returns a reference to the sheet (handy for formatting in the calling function).
    VB Code:
    1. Private Function CSVToExcel(oApp As Excel.Application, sFile As String) As Excel.Worksheet
    2.  
    3.     Dim oBook As Excel.Workbook, lRow As Long, lCol As Long, sBuffer As String, iFile As Integer
    4.     Dim sLines() As String, sRow() As String
    5.    
    6.     iFile = FreeFile
    7.     Open sFile For Binary As #iFile
    8.     sBuffer = String$(LOF(iFile), Chr$(0))
    9.     Get #iFile, , sBuffer
    10.     Close #iFile
    11.    
    12.     sLines = Split(sBuffer, vbCrLf)
    13.     Set oBook = oApp.Workbooks.Add
    14.     Set CSVToExcel = oBook.Worksheets(1)
    15.  
    16.     With CSVToExcel
    17.         For lRow = 0 To UBound(sLines)
    18.             sRow = Split(sLines(lRow), ",")
    19.             For lCol = 0 To UBound(sRow)
    20.                 .Cells(lRow + 1, lCol + 1).Value = sRow(lCol)
    21.             Next lCol
    22.         Next lRow
    23.     End With
    24.  
    25. End Function
    But the main question is why you need to go through Excel before putting the data into DBaseIII? Do you actually need to have a copy of it in Excel or were you just planning of using Excel to export to DBase? If not, I'd just use ADO and skip the Excel part.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    xlDelimited is a constant. Take the double quotes off of it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    hi comintern!
    thank you for ur reply! sure it will be much easier for me if its possibly to
    transfer the CSV file Recordset to a DBASE III Table.
    as i've said im very new to this and i dont know to handle things.
    but do u think its easier to get the data from a CSV file instead of a EXCEL file into a DBASE III table?
    it will be great if theres any solution for this.

    thanks a lot!

  13. #13
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    I definately think this would be easier and faster to just open the DB with ADO and write the records directly. You shouldn't have much trouble finding help on using ADO. If the csv columns line up with the database columns, something like this would do the trick:
    VB Code:
    1. 'Add a reference to Microsoft ActiveX Data Objects
    2. Private oConnect As ADODB.Connection
    3.  
    4. Private Sub InsertCSV(sCSVFile As String, sDBFile As String)
    5.  
    6.     Dim oRS As ADODB.Recordset, lRow As Long, lCol As Long, sBuffer As String, iFile As Integer
    7.     Dim sLines() As String, sRow() As String
    8.    
    9.     iFile = FreeFile
    10.     Open sCSVFile For Binary As #iFile
    11.     sBuffer = String$(LOF(iFile), Chr$(0))
    12.     Get #iFile, , sBuffer
    13.     Close #iFile
    14.    
    15.     sLines = Split(sBuffer, vbCrLf)
    16.     Set oRS = OpenDB(sDBFile)
    17.  
    18.     With oRS
    19.         For lRow = 0 To UBound(sLines)
    20.             sRow = Split(sLines(lRow), ",")
    21.             For lCol = 0 To UBound(sRow)
    22.                 .Fields(lCol + 1).Value = sRow(lCol)
    23.             Next lCol
    24.         Next lRow
    25.         .Update
    26.         .Close
    27.     End With
    28.  
    29.     Set oRS = Nothing
    30.     oConnect.Close
    31.     Set oConnect = Nothing
    32.  
    33. End Sub
    34.  
    35. Private Function OpenDB(sDBPath As String) As ADODB.Recordset
    36.  
    37.     Dim sCon As String
    38.    
    39.     On Error Resume Next
    40.        
    41.     sCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDBPath & ";Extended Properties=DBASE III"
    42.     Set oConnect = New ADODB.Connection
    43.    
    44.     With ConnectDB
    45.         .CursorLocation = adUseClient
    46.         .Open sCon
    47.     End With
    48.    
    49.     If Err.Number <> 0 Then
    50.         Set OpenDB = New ADODB.Recordset
    51.         'Might have to change some of the settings.
    52.         With OpenDB
    53.             .CursorType = adOpenStatic
    54.             .CursorLocation = adUseClient
    55.             .LockType = adLockPessimistic
    56.             .Source = "SELECT * FROM Table"     'Replace Table with your table name.
    57.             .ActiveConnection = oConnect
    58.             .Open
    59.             .MoveFirst
    60.         End With
    61.     End If
    62.  
    63. End Function
    If the columns don't line up, it might be easier to parse each individual line and build an insert statement.
    Last edited by Comintern; Oct 24th, 2006 at 08:02 AM. Reason: See below.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    hi comintern!
    Well the bad thing is my CSV file dont have the same columname as my DBASE III Table.
    Do you think its possible for me to insert the Recordset from the CSV file to my DBASE III table?
    How can i say in VB6:
    Take all the data from the COLUMNAME: Name FROM CSV-file and THEN
    INSERT IT TO the COLUMNAME: 2Name FROM DBASE III TABLE.

    BTW: my CSV file is with SEMICOLON delimited. and the first ROW are the columnames.

    Thanks alot!

  15. #15
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    If the header names are the same, you can access the database fields with the field names from the csv file. Just pull the first line from the csv, put it into it's own array, and index the .Field of the recordset with the corresponding column name:
    VB Code:
    1. Dim sHeaders() As String
    2.    
    3.     '...
    4.  
    5.     With oRS
    6.         sHeaders = Split(sLines(0), ";")                    'Build a header array.
    7.         For lRow = 1 To UBound(sLines)
    8.             sRow = Split(sLines(lRow), ";")
    9.             For lCol = 0 To UBound(sRow)
    10.                 .Fields(sHeaders(lCol)).Value = sRow(lCol)  'Index the field off of the corresponding header.
    11.             Next lCol
    12.         Next lRow
    13.         .Update
    14.         .Close
    15.     End With
    16.  
    17.     '...

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    hi comintern!

    Im having trouble with this line

    VB Code:
    1. Set oBook = oApp.Workbooks.Add

    not with a WITH-Clause define.

    what does it mean?

    Thanks a lot!

  17. #17
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    Whoops! Forgot to pull that out when I modified the code for ADO. I edited the post above.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    16

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    hi there comintern!

    still some problem with this! sorry really...
    .Update i received a error message: Not Allowed for a CLOSED object or
    something like that...

    VB Code:
    1. With oRS
    2.         For lRow = 0 To UBound(sLines)
    3.             sRow = Split(sLines(lRow), ",")
    4.             For lCol = 0 To UBound(sRow)
    5.                 .Fields(lCol + 1).Value = sRow(lCol)
    6.             Next lCol
    7.         Next lRow
    8.         .Update ' HERES THE LINE WHERE THE ERROR HAPPENS
    9.         .Close
    10.     End With

  19. #19
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: CSV --> EXCEL --> DBASEIII with Visual Basic 6

    Wow, I really hacked that didn't I. Need to add a new record in the loop before setting values to it. If you still get the same problem you can move the update statement inside the loop too:
    VB Code:
    1. With oRS
    2.         For lRow = 0 To UBound(sLines)
    3.             sRow = Split(sLines(lRow), ",")
    4.             .AddNew
    5.             For lCol = 0 To UBound(sRow)
    6.                 .Fields(lCol + 1).Value = sRow(lCol)
    7.             Next lCol
    8.             .Update
    9.         Next lRow
    10.         .Close
    11.     End With

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