Solved

Exporting a table

Posted on 2006-06-12
22
464 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

706 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

14 Experts available now in Live!

Get 1:1 Help Now