Results 1 to 4 of 4
  1. #1
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56

    tranferpreadsheet with Range not working


    I am trying to use transferspread sheet in a loop to transfer data from an excel workbook containing 3 "Named" sheets each into there own access table. After running the macro, the table definitions are created but the tables have no data.
    In the Range parameter I am trying to pass in the variable tSheetName which contains the name of the current worksheet.

    Code:
    Open File Dialog to choose file to process
    Set fd2 = Application.FileDialog(msoFileDialogFilePicker)
    'tSheetCount = 1
    'tNameCount = 1
    
    With fd2
      .Show
      For Each vrtSelected In .SelectedItems
        For k = 1 To ws_count
            xlWrkBk.Activate
            'store the name of the current worksheet
            tSheetName = xlWrkBk.Sheets(k).Name
            'create a table definition based on the name of the current worksheet
            Set tb = db.TableDefs(tSheetName)
            
            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")
         'Loop First Row in Each Excel Worksheet which contains header column names and write to the current table defention
    
         For strColumn = LBound(strColumns) To UBound(strColumns)
       
            Set columnName = xlWrkBk.Sheets(k).Cells(1, strColumn)
           
            If columnName = "Member Birthdate" Or columnName = "Member Effdt" Then
               Set FieldName = tb.CreateField(columnName, dbDate, 10)      'insert Date Type Fields into the table def
               tb.Fields.Append FieldName
            Else
               Set FieldName = tb.CreateField(columnName, dbText, 200)     'insert text type fields into table def
               tb.Fields.Append FieldName
            End If
               
         Next strColumn
          
         'transfers data from excel file and imports it into the access table
         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, TableName:=tSheetName, FileName:=vrtSelected, HasFieldNames:=True, Range:="tSheetName!"
       
         tSheetName = Nothing
        Next k
        Next vrtSelected
        End With

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Don't enclose variables in quotes, that results in the literal text "tSheetName!" instead of the value in tSheetName. Concatenate variables with literals to get the desired construct.

    Range:=tSheetName & "!"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Nice catch, June7!

  4. #4
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56
    Thank You Much!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Parameters (Date Range) not working
    By smarty84handsome in forum Reports
    Replies: 4
    Last Post: 07-24-2013, 11:36 AM
  2. Replies: 3
    Last Post: 01-29-2013, 04:34 AM
  3. Replies: 1
    Last Post: 08-08-2012, 02:02 PM
  4. Date range not working
    By victoria61485 in forum Queries
    Replies: 4
    Last Post: 09-08-2011, 08:56 AM
  5. Replies: 2
    Last Post: 11-25-2010, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums