• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

Exporting a table

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
ee can
Asked:
ee can
  • 7
  • 7
  • 6
  • +1
3 Solutions
 
rockiroadsCommented:
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
 
Jim P.Commented:
Yo can export a query instead of the whole table.
0
 
rockiroadsCommented:
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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
ee canAuthor Commented:
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
 
Jim P.Commented:
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
 
ee canAuthor Commented:
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
 
Jim P.Commented:
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
 
ee canAuthor Commented:
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
 
rockiroadsCommented:
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
 
rockiroadsCommented:
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
 
Jim P.Commented:
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
 
ee canAuthor Commented:
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
 
Jim P.Commented:
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
 
rockiroadsCommented:
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
 
rockiroadsCommented:
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
 
ee canAuthor Commented:
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
 
ee canAuthor Commented:
That worked :
DoCmd.RunSQL ("SELECT PART_CODE INTO [SALES_PRICES_LINES] IN '" & filename & "' FROM [SALES_PRICES_LINES-CAN]")
simply added brackets...sigh...
0
 
Jim P.Commented:
For some reason access is the table name as subtract   CAN from SALES_PRICES_LINES.  That is screwy.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
rockiroadsCommented:
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
 
Jim P.Commented:
Glad to be of assistance. May all your days get brighter and brighter.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 7
  • 7
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now