Solved

Exporting a table

Posted on 2006-06-12
22
477 Views
Last Modified: 2008-02-01
Hi experts !
I currently export 2 tables to another database that I create on the fly. Now, how do I export certain columns only ? (not all colunms) of both tables ??? Here is my code (I'm using Access 2000) :
============================================================================
Set db = CreateDatabase(filename, dbLangGeneral, dbVersion40)
db.Close
DoCmd.TransferDatabase acExport, "Microsoft Access", filename, acTable, "SALES_PRICES_LINES-CAN", "SALES_PRICE_LINES"
DoCmd.TransferDatabase acExport, "Microsoft Access", filename, acTable, "SALES_PRICES-CAN", "SALES_PRICES"
============================================================================
Thanks
0
Comment
Question by:ee can
  • 7
  • 7
  • 6
  • +1
22 Comments
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 100 total points
ID: 16889245
u could try creating a query to select just the columns you want

then export the query to csv format, then perhaps import that csv file to the new db?

0
 
LVL 38

Expert Comment

by:Jim P.
ID: 16889249
Yo can export a query instead of the whole table.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16892751
ok, a little crude perhaps
but quick and dirty


I created a macro (called Macro1)
two steps

step 1: TransferSpreadSheet
  transfer type - import
  tablename - SALES_PRICES_LINE
  filename - c:\ee\xx.csv
  Has Field Names - Yes

step 2: Quit




Public Sub ExportAndImport()

    Dim db As DAO.Database
    Dim sNewDB As String
    Dim sTable As String
    Dim sCSVFile As String
    Dim sCurrentPath As String
   
   
    'Create New DB
    sNewDB = "c:\ee\temp\db4new.mdb"

    'deletes if it already exists  
    Kill sNewDB
    Set db = CreateDatabase(sNewDB, dbLangGeneral, dbVersion40)
   
    'Set CSV File
    sCSVFile = "c:\ee\xx.csv"
   
    'Create csv file - dump query contents - qryView is the columns I want from the table
    DoCmd.OutputTo acOutputQuery, "qryView", acFormatXLS, sCSVFile
   
    'Export Macro1 - this is set to perform a DoCmd.TransferSpreadsheet of file c:\ee\xx.csv
    DoCmd.TransferDatabase acExport, "Microsoft Access", sNewDB, acMacro, "Macro1", "Macro1"
   
    db.Close
   
    'Now run macro
    Shell ("C:\Program Files\Microsoft Office\Office10\MSAccess.exe " & sNewDB & " /x Macro1")
   
End Sub



The other way is for you to create your DB,then create tables in that new DB
The open up both tables and try reading/writing via recordsets
ive not tried this, as this takes much longer to do, but Im sure its possible
0
 

Author Comment

by:ee can
ID: 16893425
Wow, ok....now I thought I could still use something like DoCmd.TransferDatabase and only put the query name...This is not possible I suppose ? I have to create an intermediate file ?!
0
 
LVL 38

Assisted Solution

by:Jim P.
Jim P. earned 300 total points
ID: 16893573
Simple method:
--------------------------------------------------------
INSERT INTO MyRemoteTable (field1, filed2,.....)
IN 'C:\Documents and Settings\All Users\Desktop\Misc Access DB\MyRemoteDB.mdb'
SELECT field1, filed2,....  
FROM MyLocalTable
0
 

Author Comment

by:ee can
ID: 16893795
It says syntax error on this :

DoCmd.RunSQL ("INSERT INTO SALES_PRICES_LINES-CAN (PART_CODE) IN '" & filename & "' SELECT PART_CODE FROM SALES_PRICES_LINES-CAN")

I need to mention that the db is already created when I do this code but not the table, is the SQL going to create it ?
Also, on my select, does it needs to be equal to the number & name of fields I want to insert or can I just put an * ?
filename variable is the path and filename of the db and it is good.

Thanks
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 16893998
If it is a make table query then it is:
----------------------------------------------------------------------
SELECT *
INTO MyRemoteTable
IN 'C:\Documents and Settings\All Users\Desktop\Misc Access DB\MyRemoteDB.mdb'
FROM MyLocalTable

0
 

Author Comment

by:ee can
ID: 16894211
DoCmd.RunSQL ("SELECT PART_CODE INTO SALES_PRICES_LINES IN '" & filename & "' FROM SALES_PRICES_LINES-CAN")
It says syntax error in the FROM clause.
PART_CODE: field to be added to the new db
SALES_PRICES_LINES: new table in the new db
SALES_PRICES_LINES-CAN: current db table that I'm trying to export.
Any ideas ?
Thanks
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16900735
ok, I found a better method, forget the reliance on macros

try this instead


Public Sub ExportAndImport()

    Dim db As DAO.Database
    Dim sNewDB As String
    Dim app As Access.Application
    Dim sTable As String
    Dim sCSVFile As String
    Dim sCurrentPath As String
   
   
    'Create New DB
    sNewDB = "c:\ee\temp\db4new.mdb"
    Kill sNewDB
    Set db = CreateDatabase(sNewDB, dbLangGeneral, dbVersion40)
    db.Close
   
    'Export data
    'Set CSV File
    sCSVFile = "c:\ee\xx.csv"
   
    'Create csv file
    DoCmd.OutputTo acOutputQuery, "qryView", acFormatXLS, sCSVFile
   
    'Import csv into that new db and call the table fred
    Set app = New Access.Application
    app.OpenCurrentDatabase sNewDB
    app.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "fred", sCSVFile, True
    app.CloseCurrentDatabase
    Set app = Nothing
End Sub

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16900746
Regarding this

DoCmd.RunSQL ("SELECT PART_CODE INTO SALES_PRICES_LINES IN '" & filename & "' FROM SALES_PRICES_LINES-CAN")

Create a predefined query called qryView

In it, it has, u can add more fields if u want to

SELECT PART_CODE FROM SALES_PRICES_LINES-CAN


In the last code I posted,
replace "fred" with "SALES_PRICES_LINES"


0
 
LVL 38

Expert Comment

by:Jim P.
ID: 16901864
Try it as:
---------------------------------------------------
DoCmd.RunSQL ("SELECT PART_CODE INTO SALES_PRICES_LINES_Test FROM SALES_PRICES_LINES-CAN")
---------------------------------------------------
And see if it creates a table in your local DB.  If it fails then it is a problem with the query.  If it works then it is a problem with the FileName parameter.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:ee can
ID: 16902616
jimpen: Same error in the FROM clause...
rockiroads: I'm trying to avoid using intermediate files as much as possible in my db. I'll test it later.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 16902708
You can build the query in the query editor and then go to the SQL view and just cut and paste.

I suspect it is simple as perhaps that the tablename "SALES_PRICES_LINES-CAN" really is  
"SALES_PRICES_LINES_CAN" with an under score instead of a dash.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16902782
In that case, jimpen's query will work

I gave the code because I based it on your original code, where you was creating a new database. I just ended up doing a slightly long winded way.


This query should work

DoCmd.RunSQL ("SELECT PART_CODE INTO SALES_PRICES_LINES IN '" & filename & "' FROM SALES_PRICES_LINES-CAN")

But if you get an error 'cos of the from, it may have something to do with your table
Does it exist? Does it really have a dash before the CAN i.e.  -CAN or should it be _CAN

What might be useful also is if you wrap the table in double quotes and see if that makes a difference

DoCmd.RunSQL ("SELECT PART_CODE INTO SALES_PRICES_LINES IN '" & filename & "' FROM [SALES_PRICES_LINES-CAN]")



0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16902799
damn, I got to learn to read
I just saw Jim's last post
Sorry Jim, I have just reiterated what u said with regards to -CAN/_CAN, my apologies. I missed your post.
0
 

Author Comment

by:ee can
ID: 16902981
The table name copy/pasted as is: F2 key, ctrl+C & ctrl+V here : SALES_PRICES_LINES-CAN
field : PART_CODE
Having said that I created a create table query and it gave :
SELECT [SALES_PRICES_LINES-CAN].PART_CODE INTO test
FROM [SALES_PRICES_LINES-CAN]
I'll try putting the table name between brackets !
0
 

Author Comment

by:ee can
ID: 16903042
That worked :
DoCmd.RunSQL ("SELECT PART_CODE INTO [SALES_PRICES_LINES] IN '" & filename & "' FROM [SALES_PRICES_LINES-CAN]")
simply added brackets...sigh...
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 16903060
For some reason access is the table name as subtract   CAN from SALES_PRICES_LINES.  That is screwy.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 100 total points
ID: 16903131
Hi all

Last I remember, any punctuation in an object name other than an underscore requires square brackets when you refer to it in VBA or SQL, which explains why SALES_PRICES_LINES works by itself, but SALES_PRICES_LINES-CAN does not.

You'll find in pretty much every Access Naming Convention white paper to avoid punctuation.

Here's one of the better ones...
http://www.mvps.org/access/general/gen0012.htm

-Jim
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 16903209
i.e.

SELECT no_brackets_required
FROM [This Needs Brackets]
WHERE [This-needs-brackets-too]=1
AND ThisDoesNotNeedBrackets=True
AND [Hey, look how bad developers can butcher! a field name 2006YR2KX.YOUBF529]=0
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16903969
jimhorn, well done - to the rescue!!!, u said what I forgot to do, I added the sqaure brackets in that post of mine
I have also been adding square brackets in motabobo's other question on the same table!
I just didnt write it down, damn fool

But hey, motabobo is sorted, thats the main thing!

Cool - I wish I was at home so I could watch the footy

0
 
LVL 38

Expert Comment

by:Jim P.
ID: 16904628
Glad to be of assistance. May all your days get brighter and brighter.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now