MS Access: Query works fine, but fails when VBA calls it - sql

I may have a workaround as a temporary fix, but this is going to cause problems if I can't get it fixed for good.
I have a query in MS Access that works just fine when it's run on its own, but when I run it using the CurrentDb.Execute command, it gives the error "Run-time error '3146': ODBC--call failed."
I also checked if it fails if it's run from the Immediate window, and it does fail then, too.
The query calls from three different tables plus one local table that was designed by the VBA script. The VBA script works if one particular table is removed.
I know this is very generic, but why would the query work everywhere except if it's called from VBA?
UPDATE: The original question was poorly worded, and I apologize for that. It was written on-the-fly and I was frazzled by the problem I was having.
Here, I think, is the problem in a nutshell: If VBA calls a query that pulls from too many tables (3 or more) on my company's server, it gives the "call failed" error. If the query is just double-clicked, it runs fine.
Now that I'm thinking about it, could it be a server-side issue?
The workaround that I've put in place is multiple queries that create local tables, then running the original query pointing to the local tables instead of the server tables. It works, but is a very time-consuming process, both in writing the code and running the code. For this specific project that I'm doing right now, it'll work, but it would really be good to be able to get this working properly.

I had this issue due to Access having a more functions available from Access than I could use through a SQL statement from Excel. So I would check any methods you're using in your query and see which ones don't work outside Access.

It would be helpful if you post your query so we see its syntax.
But several issues to explore:
CurrentDB.Execute is only for action queries (append, update,
make table). For Select queries, use linked tables or recordsets.
Link your external tables into the current database so they can
be available as any other local table. Access can connect to other
Access files, Excel sources, or any ODBC/OLEDB compliant database (Oracle,
SQL Server, MySQL, etc.)
Use ADO or DAO recordsets to pull in your external data and then
connect the queries locally as needed in VBA.

Related

MS Access Macro/VBA Non-Sense

I have a fairly complex database that has worked for quite a while. Today I went to troubleshoot something and I got a non-sensical error.
In my macro, first I create detail tables. Then I number each row sequentially in the detail table with a VBA script. Then I create a summarized version of that table including the just the key info like SKU and row number. (The rest of the details are added back later using the row number). The numbering script is working. The detail tables have the row numbers. However the summary table does not have the row numbers after running the macro. If I manually click the queries to add the summary tables, the numbers magically appear.
Please help. This database has worked for over a year without this problem.
Honestly it sounds like its corrupt. MS Access uses the Jet Engine which is known for bloating dB size and quite easily becoming corrupt.
You can see by the odd behaviour, the program is having trouble with the underlying file. If you open a new Access DB it won't exhibit the same problems.
The best bet is to recreate the dB from scratch or port it to SQL Server, PostGres, MySQL, SqlLite etc and put the application logic in a program - even if its a VB6 app, though I'd recommend .Net.
I've got the code to strip out every thing and recreate the MS Access dB here:
https://stackoverflow.com/a/16158027/495455

Excel OBDC - SQL Query

I am trying to use Excel pull data from a large Oracle Data Warehouse via an ODBC connection. I have a query that works using the editor in Access. I've tried using Power Query and Microsoft SQL to use this query to get this data into Excel and I get errors.
Therefore:
Does SQL executed from Excel need to be in a different syntax? Shouldn't it still be Oracle?
How can I use this pre-written query to ping the data warehouse and get what I need?
Here is the SQL that I have so far. I had to change some table names...sorry if that makes it weird.
The SQL you posted uses Access-specific functions. That is NOT a valid SQL query if run directly against Oracle. If you have a bunch of linked tables in Access, that would allow this.
The ODBC datasource connection in Excel works differently. ODBC executes the query directly at the datasource. It does some validation first and supports a limited subset of SQL language supported at the destination. What's included in the subset is determined in part by the driver selected for the connection.
So what you want to do is use a tool that lets you build the query directly in an Oracle environment, like Quest Toad or Oracle SQL Developer. Once you have the query working there, it should be easier to port it to Excel.
One thing I like to do is put my query into a view on the database. Then I can just select everything from the view when creating the Excel connection.
It's also worth pointing you to the My Data Sources folder. When you first setup an ODBC connection in Excel, the connection is saved by default in Windows in a folder called "My Data Sources" located just under your user profile folder. For example: C:\Users\UserName\My Data Sources\Data Source Name.odc.
You can open these *.odc files in any text editor, and you should be able to manually edit the SQL here. Especially look for the <odc:CommandText> element. In this way you can build a simple query up front, and then improve on the SQL command in your favorite environment and easily move the updated SQL to the existing ODBC connection.

Peculiar happenings in SQL Server

I have just started an internship in I've had to learn a lot on my own. I'm learning MS SQL Server, but having a strange problem. I have a DB that has four small tables. Each one has a script to drop the table, recreate the table (I've avoided FK dependencies for the time being), and execute a demo query.
Problem 1: When I first started SQL Server Managmenent Studio would execute the script, but one table didn't show up in the Object Explorer. If I tried to execute a demo query from the same .sql file, it executed with no problem. If I tried to access it from another .sql, the table didn't exist. After many times of successfully executing the script, it finally just showed up.
Problem 2: Similar problem. When I updated one table, the changes wouldn't be reflected in queries.
Problem 3: Queries will fail, but if I click execute again with no changes being made, it will usually work correctly.
Problem 4: When I use an alias for a field name, sometimes the alias is recognized and sometimes it isn't. I've literally had single query in which the alias would work in one place, but not work in another and I had to use a fully qualified name.
I've tried the refresh and refresh local cache, but those seem to have no effect. If I exit Management Studio, that seems to usually fix the first two problems.
Am I going nuts or am I just in the dark about some weird specifics of SQL Server?
First of all, when you make a schema change you need to right-click on the "Tables" node for the database in management studio and hit refresh.
If you change a column or something in a table, right-click the table and refresh.
The refresh local cache only updates the intellisense stuff, and the refresh only updates the GUI. If you modify a table with SQL and do not refresh it in the UI, the query can still use the updated table.
If you query fails, you either have a bad query or it's not pointed at the database or connection you think it is.
For aliases, there are places where they will not work (update statements, for example) but if you don't post queries where they don't work we cannot read your mind and tell you what's wrong.
If you have specific queries that are failing, post them.

SQL query and MS Access query returns different data

I use MS Access as a front end to my MS SQL server.
When I use a SQL query in ms Access that is (in my opinion) equivalent to a query I run from the SQL server, Access returns different data. The pictures below illustrate the difference. I use Access 2003 and I have connected the Access table to the SQL server (2008) through ODBC.
Anybody got an idea how to fix this??
Thanks in advance
/Iljitj
The SQL and Access Query and result:
(It's the "Projekt" column that is different)
http://i.imagehost.org/0218/SQL_access_query_and_result.jpg
Okay I solved the problem. Apperently Access won't do anything without a unique identifier. Since I didn't have that in my table, Access used CVRnr as identifier, but since it isn't unique, Access only pulled the first row and dublicated it, hence the "incorrect" data.
I added the unique identifier, and linked it correctly to Access and now it works.
Thanks a lot guys for all your help and suggestions, I don't think I would have ever figured out the cause was the identifier if some of you haven't suggested it.
You saved me ;)
Recreate Access linked table. Sometimes Access internal definitons go bad, especially when there are some changes in linked server data structures, tables/columns are recreated/altered or similar.
BTW, Access itself is certainly not bad, just it is meant for simple desktop 'databasing'. It has many limitations and some quirks, but it is quite usable.
I would just stop using Access. It appears to be very broken. First off let's find out what select statement access is really using.
In SSMS (SQL Server Management Studio) go Tool -> SQL Server Profiler.
Attach to the same database then see what Access is doing. File -> New -> connect -> run.
Ok, I see a couple of things...
Do you have a Primary Key setup for the table on MS SQL? That might be confusing MS Access.
The boolean column do have different values, but I think that's because MS Access likes to represent them differently than the MS SQL. That you're going to have to live with.

SQL behind an external data query in Excel

I have inherited an Excel spreadsheet which contains an external data query. I can edit the query, which provides a list of the columns returned, but it does provide the table that this data comes from. Is there any way to retrieve the actual SQL behind the query?
I think this may depend on the version of office you're using and the type of external data you're connecting to.
I believe if you're using Office 2003, and you're seeing columns like you say, there may be a SQL icon in the toolbar.
Click that to view the SQL used to return the data.
If you're using Office 2007, Go to the Data tab, click connections, then properties, then the definition tab, and change the command text.
However I believe you're using the first method.
This is from what I can remember, so I hope it's correct.
It could also be somewhat obsfuscated by using an ODBC link or something. Another way to get it is to run SQL Profiler on the SQL Server to see the exact SQL being executed. Of course, this won't help you edit it but you'll know what it is doing.