Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old November 4th, 2003, 09:23 AM
xyfix xyfix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 245 xyfix User rank is Private First Class (20 - 50 Reputation Level)xyfix User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 8 h 5 m 11 sec
Reputation Power: 5
visual basic in Access

Hello, i have a access db and i want to copy the contents of an excel file and paste it into a table in the db. The copy-paste procedure is prefered, because when i import the excel file into the table, the records with error concerning referentiel integerity or key fields don't come along. And if i use the copy-paste method, i get a temperary table with past errors , so i can see what the records are in the excel file, that didn't came along.
__________________

Reply With Quote
  #2  
Old November 4th, 2003, 12:44 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,171 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 37 sec
Reputation Power: 110
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
as you can with most of the office objects, you can instantiate an Access object, and one of the methods of that object is to copy from an xl spreadsheet to a table in your database. I use it a lot to populate "Staging Tables" in my Access Databases so that I can then populate the relational tables without my database "blowing up" . It's a really helpful tool... here's an example.

Code:
Private Sub Form_Load()
Dim objAccess As Access.Application
Dim cnConnection As ADODB.Connection
Set objAccess = New Access.Application
objAccess.Visible = True
objAccess.OpenCurrentDatabase "C:\PathToYourDB", True, "YourPasswordifNeccessary"
'objaccess.docmd.transferspreadsheet import/export, spreadsheet type, Destination table, Folder/File, HasFieldNames - if column headers are field names
objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTemp", "C:\YourFolder\YourSpreadsheetname.xls", "True"
Set objAccess = Nothing
'Do Sql processing here to move data from staging table "tblTemp" to real tables

Set cnConnection = New ADODB.Connection
cnConnection.ConnectionString = "Your Connection String"
cnConnection.Execute "Delete tblTemp"
cnConnection.Close
Set cnConnection = Nothing

End Sub


for this example to work, you'll need a project with references to the Microsoft Access Library, and Microsoft ADO Library
__________________
Fisherman

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein

Reply With Quote
  #3  
Old November 4th, 2003, 08:50 PM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
Use TransferSpreadsheet method:
TransferSpreadsheet Method

The TransferSpreadsheet method carries out the TransferSpreadsheet action in Visual Basic.

expression.TransferSpreadsheet(TransferType, SpreadsheetType, TableName, FileName, HasFieldNames, Range, UseOA)

expression Required. An expression that returns one of the objects in the Applies To list.

TransferType Optional AcDataTransferType.

AcDataTransferType can be one of these AcDataTransferType constants.
acExport
acImport default
acLink
If you leave this argument blank, the default constant (acImport) is assumed.


SpreadsheetType Optional AcSpreadSheetType.

AcSpreadSheetType can be one of these AcSpreadSheetType constants.
acSpreadsheetTypeExcel3
acSpreadsheetTypeExcel4
acSpreadsheetTypeExcel5
acSpreadsheetTypeExcel7
acSpreadsheetTypeExcel8 default
acSpreadsheetTypeExcel9 default
acSpreadsheetTypeLotusWJ2 - Japanese version only
acSpreadsheetTypeLotusWK1
acSpreadsheetTypeLotusWK3
acSpreadsheetTypeLotusWK4
Note You can link to data in a Lotus 1-2-3 spreadsheet file, but this data is read-only in Microsoft Access. You can import from and link (read-only) to Lotus .WK4 files, but you can't export Microsoft Access data to this spreadsheet format. Microsoft Access also no longer supports importing, exporting, or linking data from Lotus .WKS or Microsoft Excel version 2.0 spreadsheets by using this method.

If you leave this argument blank, the default constant (acSpreadsheetTypeExcel8) is assumed.


TableName Optional Variant. A string expression that's the name of the Microsoft Access table you want to import spreadsheet data into, export spreadsheet data from, or link spreadsheet data to, or the Microsoft Access select query whose results you want to export to a spreadsheet.

FileName Optional Variant. A string expression that's the file name and path of the spreadsheet you want to import from, export to, or link to.

HasFieldNames Optional Variant. Use True (1) to use the first row of the spreadsheet as field names when importing or linking. Use False (0) to treat the first row of the spreadsheet as normal data. If you leave this argument blank, the default (False) is assumed. When you export Microsoft Access table or select query data to a spreadsheet, the field names are inserted into the first row of the spreadsheet no matter what you enter for this argument.

Range Optional Variant. A string expression that's a valid range of cells or the name of a range in the spreadsheet. This argument applies only to importing. Leave this argument blank to import the entire spreadsheet. When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.

UseOA Optional Variant.

Remarks
For more information on how the action and its arguments work, see the action topic.

You can leave an optional argument blank in the middle of the syntax, but you must include the argument's comma. If you leave a trailing argument blank, don't use a comma following the last argument you specify.

Note You can also use ActiveX Data Objects (ADO) to create a link by using the ActiveConnection property for the Recordset object.

Example
The following example imports the data from the specified range of the Lotus spreadsheet Newemps.wk3 into the Microsoft Access Employees table. It uses the first row of the spreadsheet as field names.

DoCmd.TransferSpreadsheet acImport, 3, _
"Employees","C:\Lotus\Newemps.wk3", True, "A1:G12"

On msdn page:http://msdn.microsoft.com/library/d...spreadsheet.asp

Reply With Quote
  #4  
Old November 4th, 2003, 11:21 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,171 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 37 sec
Reputation Power: 110
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
Thanks for backing me up, CP

Reply With Quote
  #5  
Old November 5th, 2003, 03:45 AM
xyfix xyfix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 245 xyfix User rank is Private First Class (20 - 50 Reputation Level)xyfix User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 8 h 5 m 11 sec
Reputation Power: 5
visual basic in access

Thanks you guys for replying, but i've tried the usetransfersheet method and it imports the contents of the excel sheet to the table. With this procedure i don't get a table with errors with records that aren't imported ( error concerning referential integerity). And if i use the copy-paste method , i get a temperary table with records that aren't pasted.

Reply With Quote
  #6  
Old November 5th, 2003, 04:08 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
It'swrong that the imported table is not integrated??

Reply With Quote
  #7  
Old November 5th, 2003, 06:59 AM
xyfix xyfix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 245 xyfix User rank is Private First Class (20 - 50 Reputation Level)xyfix User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 8 h 5 m 11 sec
Reputation Power: 5
visual basic in access

there is nothing wrong with the usetransfersheet (import) method, but because i am importing the excel sheet into an table that has relationsships with other tables in the db, i expect to get errors concerning fields in the sheet and i would like to get the errors in a seperate table. So i can fix the errors an correct the import.

Reply With Quote
  #8  
Old November 5th, 2003, 08:10 AM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,171 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 37 sec
Reputation Power: 110
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
xy - I wasn't saying to import into your existing relational table. You can import into a new table and use it like a staging table. I call mine tblTemp. Rather than trying to import into the existing relational tables, I dump all of the data I want from the excel spreadsheet into a temporary table ("tblTemp"), and then use SQL Commands to process that data into my existing relational tables. Then, at the end, you can delete your temporary table, and you're left with an updated, relational database

Reply With Quote
  #9  
Old November 5th, 2003, 10:14 AM
xyfix xyfix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 245 xyfix User rank is Private First Class (20 - 50 Reputation Level)xyfix User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 8 h 5 m 11 sec
Reputation Power: 5
visual basic in access

You're the (Fisher)man. Ofcourse i can do that , why didn't i think of it in the first place.
Thank you

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > visual basic in Access


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