+ Reply to Thread
Results 1 to 2 of 2

msoFileDialogFilePicker read multiple files into seperate tables

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    41

    msoFileDialogFilePicker read multiple files into seperate tables

    I am trying to use msoFileDialogFilePicker to select 2 excel files. I want to open each file and read the first row of the file into its own access table. It works fine for one file but when I have 2 files it only reads data from the one file.


    
    DoCmd.RunSQL ("CREATE TABLE [GPS_NOT_CLIENT]")   'Create the loading table
    DoCmd.RunSQL ("CREATE TABLE [CLIENT_NOT_GPS]")   'Create the loading table
    Set tb1 = db.TableDefs("GPS_NOT_CLIENT")         'Create a table definition to add fields to loading table
    Set tb2 = db.TableDefs("CLIENT_NOT_GPS")         'Create a table definition to add fields to loading table
    Set fd2 = Application.FileDialog(msoFileDialogFilePicker)
    tNameCount = 0
    tSheetCount = 1
    
    With fd2
    .AllowMultiSelect = True
    .Show
    For Each vrtSelected In .SelectedItems
    
        Set xlapp = CreateObject("Excel.application")
       Set xlWrkBk = GetObject(vrtSelected)
       Set xlsht = xlWrkBk.Worksheets(tSheetCount)
       
    'Application.CutCopyMode = False
       
       
       'Loop First Row in Excel File which contains header column names
    
       strcolumns = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ", "CA", "CB", "CC", "CD")
    
    
       For Each strColumn In strcolumns
    
          counter = counter + 1                         'increase the counter to determine the number of columns
          Set columnName = xlsht.Cells(1, strColumn)
    
          If tSheetCount = 1 Then
    
          If columnName = "Member Birthdate" Or columnName = "Member Effdt" Then
                Set FieldName = tb1.CreateField(columnName, dbDate, 10)      'insert Date Type Fields into the table def
                tb1.Fields.Append FieldName
          Else
               Set FieldName = tb1.CreateField(columnName, dbText, 200)     'insert text type fields into table def
                tb1.Fields.Append FieldName
          End If
          End If
          
          If tSheetCount = 2 Then
          
          If columnName = "Member Birthdate" Or columnName = "Member Effdt" Then
                Set FieldName = tb2.CreateField(columnName, dbDate, 10)      'insert Date Type Fields into the table def
                tb2.Fields.Append FieldName
          Else
               Set FieldName = tb2.CreateField(columnName, dbText, 200)     'insert text type fields into table def
                tb2.Fields.Append FieldName
          End If
          End If
             
       Next strColumn
    
    
     If tNameCount = 0 Then
         tName = "GPS_NOT_CLIENT"
       End If
       If tNameCount = 1 Then
         tName = "CLIENT_NOT_GPS"
       End If
       
      MsgBox (tNameCount)
      MsgBox (tName)
       
      ' MsgBox ("Choose Cenus File")
       'transfers data from excel file and imports it into access table
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, TableName:=tName, FileName:=vrtSelected, HasFieldNames:=True
    
       Next i
        tNameCount = tNameCount + 1
        tSheetCount = tSheetCount + 1
    Next vrtSelected
    End With

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    Why are you opening the files in Excel?

    Couldn't you just import them to temporary tables using TransferSpreadsheet?

    Then you could run append queries to add data from the temporary files to the tables you've created.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Read multiple text files into a Spreadsheet
    By fatti in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-14-2012, 10:13 AM
  2. Delete rows in multiple files using criteria in a seperate workbook
    By ian.cook in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2012, 07:59 AM
  3. Read Multiple Text Files to ListBox
    By Nu2Java in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-06-2012, 02:31 PM
  4. Replies: 4
    Last Post: 05-14-2007, 02:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1