Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 3rd, 2005, 08:58 AM
xpetex xpetex is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 56 xpetex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 50 m 7 sec
Reputation Power: 4
Access Import Half Way There

Could anyone please help me understand why this isnt working, I have tried to comment the lines as much as possible. But when I run it all I get is an empty import from excel into my access table.

Code:
Private Sub Command0_Click()
	Dim rs As ADODB.Recordset
	Dim strShtName
	Dim cn As ADODB.Connection
	Dim oXL As Object, oWbXL As Object
	Dim myfile As String, mypath As String
	'
	Set cn = CurrentProject.Connection
	Set rs = New ADODB.Recordset
	'
	'The following opens "profits" table for import
	rs.Open "SELECT * FROM profits", cn, adOpenDynamic, adLockOptimistic
	'
	'Set Excel objects
	On Error Resume Next
	Set oXL = GetObject(, "Excel.Application")
	If CBool(Err.Number) Then
		Set oXL = CreateObject("Excel.Application")
		Err.Clear
	End If
	oXL.Visible = True
	'
	mypath = "n:\profit\" 'The location of the xls files
	myfile = Dir(mypath & "*.xls") 'To import ALL xls files there is a *
	Do While CBool(Len(myfile))
		Set oWbXL = oXL.Workbooks.Open(mypath & myfile)
With oWbXL
Dim strShName As String
For eng = 3 To Excel.Application.Worksheets.Count
strShtName = .sheets(eng).Name 'you may want to put a break here to see if the name is assigned correctly
For lng = 2 To 100 'This loop is to ensure all records are imported as there are never more than 100
rs.AddNew
rs.Fields("Date") = .sheets(strShtName).Range("A" & lng) 'import cell A4-100 into Field "Date"
rs.Fields("Cost") = .sheets(strShtName).Range("G" & lng) 'import cell A4-100 into Field "Cost"
rs.Fields("Jobtype") = .sheets(strShtName).Range("I" & lng) 'import cell A4-100 into Field "Jobtype"
'this imports all the relevant information for this engineer needed to complete the calculations
rs.Update 'in ADO, not necessary
Next lng 'Repeat the import untill lng reaches 100
Next eng

.Close SaveChanges:=False
End With
		myfile = Dir
	Loop
	'
	rs.Close: Set rs = Nothing
	Set oWbXL = Nothing: Set oXL = Nothing
End Sub


It works fine normally, but I need it to loop through sheets and also import miltiple lines per sheet hence the loops. pete.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Access Import Half Way There


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT