Results 1 to 4 of 4

Thread: transferring an excel file into a csv file using vba

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Posts
    28

    transferring an excel file into a csv file using vba

    I successfully saved my excel file into a csv using vba. (because you cant have import specs on an excel file) But my problem is is that when i import the resulting csv file into my database using import specs, the information is all jibberish and not the data that it was when it was an excel file. Here is my code:


    Private Sub ImportContract_Click()
    Dim db As Database
    Set db = CurrentDb
    Dim Sql As String
    Dim FileName As String
    Dim CsvFile As String
    Dim xlApp As Excel.Application, xlBook As Excel.Workbook
    Dim TemplateLoc As String
    Const InitialPath = "R:\Pricing\Access\SAPEnglishTool\"

    db.Execute ("DELETE tblSAPImport.* FROM tblSAPImport;")
    db.Execute ("DELETE tblContractImport.* FROM tblContractImport;")
    db.Execute ("DELETE [English Version Template].* FROM [English Version Template];")

    TemplateLoc = "R:\PRICING\Access\SAPEnglishTool\ZCDT Test.xlsx"
    CsvFile = "R:\PRICING\Access\SAPEnglishTool\ZCDT Test.csv"

    Set xlBook = GetObject(TemplateLoc)
    Set xlApp = xlBook.Parent
    xlBook.SaveAs CsvFile

    Set xlBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing

    FileName = adhCommonFileOpenSave(, "CSV Files (*.csv)|" & "*.csv", , , , "Import CSV File", InitialPath, , False)
    If Right(FileName, 3) <> "csv" Then
    GoTo NotTxtFile
    End If
    If FileName <> "" Then
    DoCmd.TransferText acImportDelim, "SAPImportSpec", "tblSAPImport", FileName, True
    End If

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: transferring an excel file into a csv file using vba

    That isn't VB. I think you want the Office forum.

  3. #3
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: transferring an excel file into a csv file using vba

    Agreed should be in office

    the op needs to put some examples of the initial file and the resultant csv

    there is probably no need for any of it

    will keep an eye open for it

    here to help

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

    Re: transferring an excel file into a csv file using vba

    xlBook.SaveAs CsvFile
    i assume this save an excel workbook file with a csv extension
    you would need to specify the file type for a csv file as well as the filename for your saveAs
    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

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