Link to home
Start Free TrialLog in
Avatar of ee can
ee can

asked on

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
SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yo can export a query instead of the whole table.
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
Avatar of ee can
ee can

ASKER

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 ?!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ee can

ASKER

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
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

Avatar of ee can

ASKER

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
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

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"


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.
Avatar of ee can

ASKER

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.
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.
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]")



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.
Avatar of ee can

ASKER

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 !
Avatar of ee can

ASKER

That worked :
DoCmd.RunSQL ("SELECT PART_CODE INTO [SALES_PRICES_LINES] IN '" & filename & "' FROM [SALES_PRICES_LINES-CAN]")
simply added brackets...sigh...
For some reason access is the table name as subtract   CAN from SALES_PRICES_LINES.  That is screwy.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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

Glad to be of assistance. May all your days get brighter and brighter.