473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

SQL in Access: INSERT INTO not working

33
I'm probably doing something wrong, since I'm pretty inexperienced with SQL, but I'm trying to insert values into one table from another, where 3 other column values are equivalent.

Expand|Select|Wrap|Line Numbers
  1. Dim vInsertLOI As String
  2. vInsertLOI = "INSERT INTO TBL_DocReview_byStudy([Date of LOI]) " _
  3. & "SELECT [DATE SIGNED] " _
  4. & "FROM [TBL_INVITATION LETTERS] " _
  5. & "INNER JOIN TBL_DocReview_byStudy " _
  6. & "ON [TBL_INVITATION LETTERS].[DATE SIGNED]=TBL_DocReview_byStudy.[Date of LOI] " _
  7. & "AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study " _
  8. & "AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB"

and then later

Expand|Select|Wrap|Line Numbers
  1. CurrentDb.Execute vInsertLOI, dbFailOnError And dbSeeChanges

It's not throwing any errors, but when I open up the target table, nothing has been added. I'm thinking that maybe it is not recognizing any pair of records where the columns in question match up (though there are many).

Thanks for your help!
Jan 13 '16 #1

✓ answered by Rabbit

The syntax of an UPDATE query with a JOIN in Access is thusly:
Expand|Select|Wrap|Line Numbers
  1. UPDATE Table1 
  2. INNER JOIN Table2 
  3. ON Table1.Field2 = Table2.Field2 
  4. SET Table2.Field1 = Table1.Field1;

17 6200
zmbd
5,501 Expert Mod 4TB
On the line following your string vInsertLOI, please insert
(somewhere right before your 1.CurrentDb.Execute )
Debug.Print "rslvd_vInsertLOI==" & vInsertLOI

You can either step thru the code, or simply attempt to run it at this point.
Press <ctrl><g> after running/stepping the code to see the string
You should have something like:

rslvd_vInsertLOI==INSERT INTO TBL_DocReview_byStudy([Date of LOI]) SELECT [DATE SIGNED] (...)

Double check that the string is resolving correctly.

Another hint is to build the basic select query first, make sure that it is returning the desired records. It's usually simple to convert the query to an action query afterwards and then use the SQL from the query builder in the code.

Also you should get into the habit of setting an object for the current database to ensure that you are actually referring to the correct object and to help with memory overhead...:
Expand|Select|Wrap|Line Numbers
  1. Dim zDB as DAO.Database
  2. Dim vInsertLOI As String
  3. '(...)
  4. set zDB = CurrentDB
  5. '(...)
  6.  vInsertLOI = "INSERT INTO TBL_ (...)
  7. '
  8. '(...)
  9. zDB.Execute vInsertLOI (...)
  10. '
  11. '(...)
  12. If not zDB is Nothing Then set zDB=Nothing 
  13. '
  14. '(...)
-z
Jan 13 '16 #2
abcrf
33
Returns:

Expand|Select|Wrap|Line Numbers
  1. rslvd_vInsertLOI==INSERT INTO TBL_DocReview_byStudy([Date of LOI]) SELECT [DATE SIGNED] FROM [TBL_INVITATION LETTERS] INNER JOIN TBL_DocReview_byStudy ON [TBL_INVITATION LETTERS].[DATE SIGNED]=TBL_DocReview_byStudy.[Date of LOI] AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB
So it's working fine, the problem is in the SQL. Might it make sense to reclassify this as a SQL Question?
Jan 14 '16 #3
Rabbit
12,516 Expert Mod 8TB
Change it into a select and run it in a query to see if it actually returns rows.
Jan 14 '16 #4
abcrf
33
It throws runtime error 3065 (cannot execute a select query). I'm guessing that I'm misunderstanding or messing up the syntax of the INNER JOIN...ON...AND, because that's the only thing I haven't used before.
Jan 14 '16 #5
Rabbit
12,516 Expert Mod 8TB
That's probably because you're trying to run it in VBA. Run it in a query instead.
Jan 14 '16 #6
zmbd
5,501 Expert Mod 4TB
Open query editor
cancel add tables dialog
switch to SQL view
Insert just the select portion of your insert action query
Expand|Select|Wrap|Line Numbers
  1. SELECT [DATE SIGNED] 
  2. FROM [TBL_INVITATION LETTERS] 
  3. INNER JOIN TBL_DocReview_byStudy 
  4.     ON [TBL_INVITATION LETTERS].[DATE SIGNED]=TBL_DocReview_byStudy.[Date of LOI] 
  5.         AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study 
  6.             AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB
Do any records return?
No return = no insert :)

This is what I meant by building your select query first.
The query editor doesn't do too bad a job building the underlying SQL and once the select query returns the desired records correctly you can change it to the append/insert, switch to the SQL view, and copy the string for use in VBA (or save it and call it from code - I tend to hide action queries in the vba away from little fingers :D )
Jan 15 '16 #7
abcrf
33
Okay, I've got it returning rows (user error, as it ever was - the syntax was fine) but it's inserting the rows into new records rather than the records referred to in the ON portion of the SELECT statement. That makes sense, I suppose, but I can't add a WHERE clause to direct the insertion, since INSERT INTO doesn't support WHERE. Any ideas how I get this to insert correctly?
Jan 15 '16 #8
Rabbit
12,516 Expert Mod 8TB
What you're talking about is an UPDATE query. Not an INSERT query.
Jan 15 '16 #9
abcrf
33
Fair enough. With the update query, though, I'm getting RT error 3075 "syntax error (missing operator) in query expression..."

Referring to:

Expand|Select|Wrap|Line Numbers
  1. Dim vUpdateLOI As String
  2. vUpdateLOI = "UPDATE TBL_DocReview_byStudy " _
  3. & "SET TBL_DocReview_byStudy.[Date of LOI]=[TBL_INVITATION LETTERS].[DATE SIGNED] " _
  4. & "FROM [TBL_INVITATION LETTERS] INNER JOIN TBL_DocReview_byStudy " _
  5. & "ON [TBL_INVITATION LETTERS].[LAST NAME, FIRST NAME]=TBL_DocReview_byStudy.[Member] " _
  6. & "AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study " _
  7. & "AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB " _
  8. & "WHERE [TBL_INVITATION LETTERS].[LAST NAME, FIRST NAME]=TBL_DocReview_byStudy.[Member] " _
  9. & "AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study " _
  10. & "AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB"
Jan 15 '16 #10
zmbd
5,501 Expert Mod 4TB
once again, you need to debug.print the string to insure it is resolving correctly.

This error is quite common with a mis-spaced text or missing comma in the SQL

I also tend to close all of my SQL with a semicolon, although Access doesn't seem to care, it is a good habit to get in to doing as many interpreters are looking for this closing terminator.
Jan 15 '16 #11
abcrf
33
Yeah, I did that, and it's printing the string exactly as expected.
Jan 15 '16 #12
zmbd
5,501 Expert Mod 4TB
then post the resolved string.
Jan 15 '16 #13
abcrf
33
Expand|Select|Wrap|Line Numbers
  1. rslvd_vUpdateLOI==
  2. UPDATE TBL_DocReview_byStudy 
  3. SET TBL_DocReview_byStudy.[Date of LOI]=[TBL_INVITATION LETTERS].[DATE SIGNED] 
  4. FROM [TBL_INVITATION LETTERS] 
  5.   INNER JOIN TBL_DocReview_byStudy 
  6.     ON [TBL_INVITATION LETTERS].[LAST NAME, FIRST NAME]=TBL_DocReview_byStudy.[Member] 
  7.       AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study 
  8.       AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB 
  9. WHERE [TBL_INVITATION LETTERS].[LAST NAME, FIRST NAME]=TBL_DocReview_byStudy.[Member] 
  10.   AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study 
  11.   AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB
Jan 15 '16 #14
zmbd
5,501 Expert Mod 4TB
+ You really should not use a comma (line6 and 9, etc...) in your field names.
I don't care if it is enclosed in square-brackets, this has caused me no end of issues when I inherit or am asked to help with issues in databases.... keep in mind, I'm a Chemist not a DBA/Programmer :)
-alphanumeric:
+Access 2007 reserved words and symbols
+Access reserved words and symbols
+AllenBrowne- Problem names and reserved words in Access

I personally avoid spaces in the field names too, either using the underscore or the capitalized word method. This comes from the old school of programing and naming; however, it certainly makes life much easier. I never use a non-alphanumeric in field names... once again, old school; however, symbols have been the root source of many a hard to find issue.


Expand|Select|Wrap|Line Numbers
  1. rslvd_vUpdateLOI==
  2. UPDATE TBL_DocReview_byStudy 
  3. SET TBL_DocReview_byStudy.[Date of LOI]=[TBL_INVITATION LETTERS].[DATE SIGNED] 
  4. FROM [TBL_INVITATION LETTERS] 
  5.   INNER JOIN TBL_DocReview_byStudy 
  6.     ON [TBL_INVITATION LETTERS].[LAST NAMEFIRST NAME]=TBL_DocReview_byStudy.[Member] 
  7.       AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study 
  8.       AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB 
  9. WHERE [TBL_INVITATION LETTERS].[LAST NAME, FIRST NAME]=TBL_DocReview_byStudy.[Member] 
  10.   AND [TBL_INVITATION LETTERS].STUDY=TBL_DocReview_byStudy.Study 
  11.   AND [TBL_INVITATION LETTERS].[DC TYPE]=TBL_DocReview_byStudy.CDB
Jan 15 '16 #15
abcrf
33
Yes, I'm with you. Unfortunately, I'm building on a project that someone else started, and their naming was pretty messy. Commas, spaces, etc. I'd try to go through and standardize it, but it will be pretty involved. I'll start by at least standardizing for these tables, though, to see if that changes the outcome.
Jan 15 '16 #16
abcrf
33
It's now:

Expand|Select|Wrap|Line Numbers
  1. Dim vUpdateLOI As String
  2. vUpdateLOI = "UPDATE TBL_DocReview_byStudy " _
  3. & "SET TBL_DocReview_byStudy.DateLOI = TBL_LOI.DateSigned " _
  4. & "FROM TBL_LOI INNER JOIN TBL_DocReview_byStudy " _
  5. & "ON TBL_LOI.MemberName = TBL_DocReview_byStudy.Member " _
  6. & "AND TBL_LOI.Study = TBL_DocReview_byStudy.Study " _
  7. & "AND TBL_LOI.CDB = TBL_DocReview_byStudy.CDB " _
  8. & "WHERE TBL_LOI.MemberName = TBL_DocReview_byStudy.Member " _
  9. & "AND TBL_LOI.Study = TBL_DocReview_byStudy.Study " _
  10. & "AND TBL_LOI.CDB = TBL_DocReview_byStudy.CDB"

But, no change. Still the syntax error (missing operator), and still prints the following as the string"

Expand|Select|Wrap|Line Numbers
  1. rslvd_vUpdateLOI==UPDATE TBL_DocReview_byStudy
  2. SET TBL_DocReview_byStudy.DateLOI = TBL_LOI.DateSigned
  3. FROM TBL_LOI
  4.   INNER JOIN TBL_DocReview_byStudy
  5.     ON TBL_LOI.MemberName = TBL_DocReview_byStudy.Member
  6.       AND TBL_LOI.Study = TBL_DocReview_byStudy.Study
  7.       AND TBL_LOI.CDB = TBL_DocReview_byStudy.CDB
  8. WHERE TBL_LOI.MemberName = TBL_DocReview_byStudy.Member
  9.   AND TBL_LOI.Study = TBL_DocReview_byStudy.Study
  10.   AND TBL_LOI.CDB = TBL_DocReview_byStudy.CDB

A potentially useful piece of information: the error message telling me there's a missing operator spefically quotes lines 4-7 of the vba code.
Jan 15 '16 #17
Rabbit
12,516 Expert Mod 8TB
The syntax of an UPDATE query with a JOIN in Access is thusly:
Expand|Select|Wrap|Line Numbers
  1. UPDATE Table1 
  2. INNER JOIN Table2 
  3. ON Table1.Field2 = Table2.Field2 
  4. SET Table2.Field1 = Table1.Field1;
Jan 15 '16 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: middletree | last post by:
I am used to SQL Server, no Access, but this one thing has to be done in Access. Can you tell me if this query will work, based on the syntax? I am trying create a new row on the database, in one...
4
by: Pradeep Kumar | last post by:
/*******calc.cpp*********************/ # include <iostream.h> # include"calc.h" int calc::func(date d){ d.num = 31; return (d.num); }
6
by: Susan Bricker | last post by:
Hi. Does anyone have a clue why my mouse wheel stopped working while I was working on the VB behind a form in MS/Access? I would swear that the mouse wheel was working a short time ago. I've...
3
by: Max Riedel | last post by:
Hi! In the company where I'm working there are 5 machines setup with Access 2003 and some with 2002. On all but one access isn't functioning properly.Everytime someone tries to query or create a...
2
by: devprog | last post by:
objConn = New ADODB.Connection objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath _ & ";Jet OLEDB:Database Password=" & dbPassword) This cause error: (VB.NET. ) sqlString...
1
by: codemanthebarbarian | last post by:
Is there an activeX or DLL that I can use that emulates the Insert Hyperlink GUI of MS Access? I'd hate to have to code all the beauty that's already there.
0
by: Korpisoturi | last post by:
Access 97 working little slowly. I have windows 2000. The computer is old..processor is about 300 MHz and RAM- memory capasity is ~164 Mb. Hard disk is 3.0GB. I have only three tables which are...
1
by: dinosors | last post by:
i don't know what to do really i've seen the code 100 times and it seem right so if anyone can help....this's the code public void ajout_dossier_image(string dossier,string matr) { if...
5
by: Ferasse | last post by:
Hi, I'm an occasional Ms-Access developer, so there is still a lot of stuff that I don't get... Right now, I'm working on a database that stores contractual information. One of the form that...
8
by: Joshua Grow | last post by:
Ok, so I am getting the following error when I run the code at the bottom of this page. I am using Visual Studio 2010 Professional, Access 2000 .mdb database. Any suggestions on why this doesn't work...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.