How to Query and Dump Results Into Table In Access - sql

Need to query my SQL server from Access using an ADO connection (for example), and then using something like:
Currentdb.CreateTableDef()
in Access to create a table (in Access) with the query results.
How can I do this?

Using DAO:
currentdb.execute "SELECT * INTO LocalTableName FROM SQLServerTable;"
The string inside the quotes should be identical in ADO but I don't use ADO much.

You could consider SQL DDL CREATE TEMPORARY TABLE syntax. From the Access 2007 Help:
When a TEMPORARY table is created it
is visible only within the session in
which it was created. It is
automatically deleted when the session
is terminated. Temporary tables can be
accessed by more than one user.
... my tongue is firmly embedded in my cheek :) This syntax doesn't exist in the Access Database Engine and never has. Instead, it's another example of the appalling state of the Access documentation on the engine side of the house. Caveat emptor.

Related

CREATE VIEW in MS ACCESS with SQL returning Syntax error CREATE TABLE

I am trying to CREATE VIEW in access in SQL view but I am getting a syntax error for CREATE TABLE which is highlighting the word VIEW. This is in Access 2016 via Office 365 (latest update as of 2/11/2019). The SELECT statement works by itself, but the CREATE VIEW command isn't. My book (Concepts of Database Management) is designed for use specifically alongside Access. My code is as such:
CREATE VIEW TopLevelCust AS
SELECT CustomerNum, CustomerName, Street, Balance, CreditLimit
FROM Customer
WHERE CreditLimit>=10000
;
As already stated in Lynn's answer, if you want to execute this query, you can do that after turning on SQL server compatible syntax.
However, you can also execute the query using an OLEDB connection to the Access database.
You can even do this using VBA and the already preset CurrentProject.Connection object:
CurrentProject.Connection.Execute "CREATE VIEW Query1 AS SELECT 1"
Without turning on SQL server compatible syntax, DDL statements executed from Access itself are fairly limited (for example, you can also not use the Decimal data type). But these DDL statements are not really meant to be executed from Access itself, VBA provides way better tools to create queries (that also allows creating pass-through queries, for example).
According to the asker and other users, enabling ANSI-92 SQL in the database options will allow you to execute the DDL statement CREATE VIEW.
File > Options > Object Designers > Query Design.
According to Wolfgang, under the hood, this actually creates a query.
<SoapBox>
It surprises me that your text reference requests you to execute statements that aren't enabled by default in Access, especially without a special note screaming at you that you need to enable a special option before database creation. ¯\_(ツ)_/¯
</SoapBox>

Join MS Access Table to Oracle table

I'm playing around with a table in an MS Access database. The table has a primary key of CLIENT_NUMBER. My corporation maintains an Oracle database that has a table which contains clients contact information (address, phone numbers, emails, etc). It also has the CLIENT_NUMBER field. I got to thinking that maybe I can join the 2 tables from the different databases and run some queries. I dug around on the net and I couldn't really find any reference, so I think this is a long shot and a silly question, but is that possible? Maybe through a DB link or something? For reference, I use SQL Developer 3.2.xx for sql developing.
I would copy the table in oracle to Access using what's called a sqlpassthrough query in Access. linked tables to oracle in my experience, perform very poorly, and if you are also thinking about joining to a local table in Access, probably much worse.
Passthrough queries are very quick since Access simply just sends the query for execution to the target server/database based on the connection you identify for the passthrough query, hence the name "pass-through".
The driver in the connect string may not work for you, and it may need more info depending on how things are setup in your environment, so you will have to work that out.
'creates the passthrough query to oracle
With CurrentDb.CreateQueryDef("qOracleConn")
.Connect = "ODBC;Driver={Microsoft ODBC for Oracle};Server=oracleservername;Uid=oracledbusername;Pwd=oracledbpassword;"
.sql = "SELECT * FROM tableinoracle"
End With
'creates the local table in access
CurrentDb.Execute "SELECT * INTO OracleClients FROM qOracleConn"

How to query PostgreSQL database table from Access?

I am very new to SQL, MS Access & PostgreSQL. So this might be a very silly question but somehow I can't figure it out. I'm trying to run SQL queries in access and my data is in a PostgreSQL database table which was linked to access by my colleague earlier. When I make this simple query why do I get an error that the table doesn't exist? Is the syntax different for linked database tables? Or is the link not yet established?
You have created a Pass-Through query. This query is executed on the server, not in Access, so you need to use the original table names from the PostgreSQL database.
So it's not FROM public_tb_change but FROM tb_change.
Or maybe FROM public.tb_change, if public isn't the default schema.
I advise to rename your linked tables to the original name (remove public_), that makes things much less confusing. The schema name is automatically added by Access when linking the tables.

Access Pass Through & Access table

I need to use a pass through query using an ODBC connection, however I need to further refine the SQL using another tables within the same Access database.
How do I reference the Access table? Access keeps thinking I am looking for a table via the ODBC connection rather than the database itself.
First, you create a Linked Table in access, using the ODBC connection. Then, you just write a query against the Access database using whatever name you give your Linked Table, and the names of the access tables.
Be warned: if the linked table contains a large number of rows, Linked Tables can be very inefficient.

ADO to query MDB and SQL Server

I've got a database that resides on an SQL server box, and another in a separate mdb file.
Both contain similar data, but I'd like to run a query that checks unmatched records from a field that exists in both.
Is this something that's easy enough to do using ADO (VBA)? If so can you point me in the right direction?
The easiest would be to create a new Access Database or ADP, and link the tables from both the SQL server and the other MDB. That way you've got an interface to both from within the same instance, which allows you to query or join different tables.