Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 17th, 2007, 02:44 AM
Authorized User
 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Fail to auto import excel to access table

Dear All,

I use the follow macro and the sub can be run but data in strpath is not imported to tbldata table.

Sub Import_to_access()
Dim strPath As String
Dim strFileName As String

strPath = "D:\data\try\"

myFile = Dir(strPath & "\*.xls")
Do While myFile < ""
DoCmd.TransferSpreadsheet acImport, "tbldata", "data", True
myFile = Dir
Loop
End Sub

My situation: there is many excel files in same format. Each excel file contains three spread sheets and I want to upload one of the spreadsheet named "data" into a tbldata table in access.

*tbldata is in "data.mdb" file

Thanks,

 
Old April 17th, 2007, 06:56 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I don't think you can pass a wildcard to DoCmd.TransferSpreadsheet, but in any event, you are not telling it where to get the data from.

The syntax is:

DoCmd.TransferSpreadsheet TransferType, SpreadsheetType, TableName, FileName, HasFieldNames, Range, UseOA

FileName requires the path and filename. You would at least want:

DoCmd.TransferSpreadsheet acImport, , "tblData", "D:\data\try\FileName.xls"

But even if you did this:

DoCmd.TransferSpreadsheet acImport, , "tblData", myFile

You are not getting to your worksheet.

You will have to do something like:

Sub DoExcel()
  Dim appExcel As Excel.Application
  Dim wb As Excel.Workbook
  Dim sh As Excel.Worksheet
  Dim strValue As String
  Dim intRow As Integer
  Dim intCol As Integer
  Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("YourTable")
Set appExcel = CreateObject("Excel.Application")
Set wb = appExcel.Workbooks.Open(myFile)
 For Each sh In wb.Sheets
   If sh = "data" Then
    strValue = sh.Cells(intRow, intCol)
    rs.AddNew
    'Add Fields
    rs.Update
  End If
 Next

rs.Close
wb.Close
appExcel.Quit
End Sub

Anyway, I don't do this much, but you can see that you need to refer to specific sheets as you iterate through the directory looking for files.

Did that help any?


mmcdonal
 
Old April 19th, 2007, 09:34 PM
Authorized User
 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear mmcdonal,

Thanks for your support.
I change my module and add "data$" as shown below. It seems that it works but i need to keep observation as I am too new in vba
filename = path & strFileList(intFile)
DoCmd.TransferSpreadsheet acImport, , "tbldata", filename, True, "data$"






Similar Threads
Thread Thread Starter Forum Replies Last Post
import excel to access Vision G Access 1 October 15th, 2008 02:12 PM
using vbscript in asp to import excel into access wpiass Access 11 April 26th, 2007 04:35 PM
Access import and table manipulation scubasteve Access 1 June 12th, 2006 12:57 PM
Table output to Excel and auto naming file newbe Access 6 December 13th, 2005 06:54 AM
Import an excel file into access arjunvs Access VBA 1 September 15th, 2004 07:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.