Trying to import csv to excel - why isn't it looking for column names instead doing F numbers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anoble1
    New Member
    • Jul 2008
    • 246

    Trying to import csv to excel - why isn't it looking for column names instead doing F numbers?

    When I am trying to import a .csv into a table in access somehow it is wanting to match my columns not by name but by F1-F41 or the excel sheet. How can I get it to only match on column names that are defined in the excel sheet on the first row? This is a .csv file I am trying to import.

    So right now I am just having excel create it's own table so I can see how it is importing.


    Code:
    Dim strfilename As String
    With Application.FileDialog(msoFileDialogFilePicker)
            .Title = "Select the CSV file to import"
            .AllowMultiSelect = False
            .Filters.Clear
            .Filters.Add "CSV Files", "*.csv", 1
            .Filters.Add "All Files", "*.*", 2
            If .Show = -1 Then
                strfilename = .SelectedItems(1)
                DoCmd.TransferText TransferType:=acImportDelim, _
                    tableName:=acTable, filename:=strfilename
                Else
                Exit Sub
            End If
        End With
        
        
        
        Exit Sub
    Last edited by anoble1; Jul 21 '21, 03:06 PM. Reason: Add additional helpful info
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32661

    #2
    I'm able to confirm that this is a valid question as far as moderation goes but it's so poorly written I have no idea how to help.

    Consider putting a bit more effort into writing a question that makes sense before submitting maybe.

    Comment

    • isladogs
      Recognized Expert Moderator Contributor
      • Jul 2007
      • 483

      #3
      If I understand you correctly the field names are being treated as the first row in your imported data.
      The syntax is explained in the link: DoCmd.TransferT ext.
      It includes a HasFieldNames option which is True or False:
      DoCmd.TransferT ext (TransferType, SpecificationNa me, TableName, FileName, HasFieldNames, HTMLTableName, CodePage)

      If omitted, HasFieldNames is treated as False

      Surely your table name isn't really acTable? If it is, change it!

      Then try changing that code to
      Code:
      DoCmd.TransferText TransferType:=acImportDelim, _
                      tableName:=[B]YourRealTableName[/B], filename:=strfilename,
                     [B]HasFieldNames:=True[/B]
      Does that work?

      You may find this link useful as well: TransferText examples
      Last edited by isladogs; Jul 21 '21, 05:00 PM. Reason: Layout

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        I suspect that IslaDogs' response has already given you what you need for this situaion (So I'll set that as Best Answer) but I would add some general advice that will hopefully help you in future - firstly not to have to post questions as you'll have fewer problems, but then to ask better questions.

        The point is to use the compiler more heavily. In the VBA window select Debug | Compile {Project Name} when you're ready. This will fail if there are any syntax errors in your code and the associated message will lead you to a fix - or at least will be worth including in a question so that we can help you easily. It's never a good idea to post uncompiled code in your questions as it just wastes everyone's time - yours as much as ours. Compiling is easy and if your code doesn't even compile without error then it isn't going to start, let alone work as you intend. Not a good point to ask a question from ;-)

        Comment

        Working...