Solved

SQL Question

Posted on 2007-12-04
6
210 Views
Last Modified: 2010-03-20
This is the SQL behind an Access Insert Query...The Query works perfectly...When I try to run the same SQL with VBA using the DoCmd.RunSQL " Blah blah blah" I get a Type Mismatch Error.

In the line marked ///// MyType is a String variable

INSERT INTO DEPT_ACCOUNT_EXPENSE ( DepartmentID, GLDescription, Incoming_ActivityID )
SELECT ACTIVITY_SERVICE.ServiceID AS DeptID, [ELEMENTS_1]![ELEMENT] & " - " & [Activity] AS GLDescription, ACTIVITY_SERVICE.ActivityID
FROM ELEMENTS INNER JOIN ((ACTIVITIES INNER JOIN ELEMENTS AS ELEMENTS_1 ON ACTIVITIES.Element=ELEMENTS_1.ElementID) INNER JOIN ACTIVITY_SERVICE ON ACTIVITIES.ActivityID=ACTIVITY_SERVICE.ActivityID) ON ELEMENTS.ElementID=ACTIVITY_SERVICE.ServiceID
WHERE (((ELEMENTS.ElementType) Not Like " & Chr(34) & MyType & Chr(34) & ")); ////

Any thoughs you SQL guru's



0
Comment
Question by:dwood1112
6 Comments
 
LVL 44

Expert Comment

by:GRayL
ID: 20406012
Wrap the whole string in double quotes and replace the double quotes within the string with single quotes.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20406020
>Not Like " & Chr(34) & MyType & Chr(34) & "));
Access uses asterisks ( * ) for wildcard searches, which you are missing uness it's part of MyType

Also, a double-quote within a double-quote in Access SQL dictates that you use a single quote
Not Like '" & MyType &  "')); ////

Also, since your INSERTing, you can lose the alaises in your SELECT clause (As DeptID, AS GLDescription, ...) as they don't do anything for you.

Also, what data type is ElementType and MyType?
0
 
LVL 44

Accepted Solution

by:
GRayL earned 250 total points
ID: 20406085


For example, using line continuation characters (_) and the single quotes around the hyphen:
 

MySQL="INSERT INTO DEPT_ACCOUNT_EXPENSE ( DepartmentID, GLDescription, " _ 

& "Incoming_ActivityID) SELECT ACTIVITY_SERVICE.ServiceID AS DeptID, " _

& "[ELEMENTS_1]![ELEMENT] & ' - ' & [Activity] AS GLDescription, " _

& "ACTIVITY_SERVICE.ActivityID FROM ELEMENTS INNER JOIN ((ACTIVITIES INNER JOIN ELEMENTS " _ 

& "AS ELEMENTS_1 ON ACTIVITIES.Element=ELEMENTS_1.ElementID) INNER JOIN ACTIVITY_SERVICE " _ & "ON ACTIVITIES.ActivityID=ACTIVITY_SERVICE.ActivityID) ON ELEMENTS.ElementID = " _

& "ACTIVITY_SERVICE.ServiceID WHERE (((ELEMENTS.ElementType) Not Like " _

& Chr(34) & MyType & Chr(34) & "));"
 

Docmd.RunSQL mySQL

Open in new window

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:dwood1112
ID: 20406104
I originally started with a wildcard...see last line marked /////....What's the best way to write that line?


INSERT INTO DEPT_ACCOUNT_EXPENSE ( DepartmentID, GLDescription, Incoming_ActivityID ) " & _
"SELECT Val([ServiceID]) AS DeptID, [ELEMENTS_1]![ELEMENT] & " - " & [Activity] AS GLDescription, " & _
"Val([ActivityID]) AS ActID FROM ELEMENTS INNER JOIN ((ACTIVITIES INNER JOIN ELEMENTS AS ELEMENTS_1 ON " & _
"ACTIVITIES.Element = ELEMENTS_1.ElementID) INNER JOIN ACTIVITY_SERVICE ON ACTIVITIES.ActivityID = " & _
"ACTIVITY_SERVICE.ActivityID) ON ELEMENTS.ElementID = ACTIVITY_SERVICE.ServiceID " & _
"WHERE (((ELEMENTS.ElementType) Like 'c*')); //////
0
 
LVL 16

Assisted Solution

by:Rick_Rickards
Rick_Rickards earned 250 total points
ID: 20406193

Dim strSQL as String
 

strSQL = "INSERT INTO DEPT_ACCOUNT_EXPENSE (DepartmentID, GLDescription, Incoming_ActivityID) " & _ 

	 "SELECT ACTIVITY_SERVICE.ServiceID AS DeptID, [ELEMENTS_1]![ELEMENT] & "" - "" & [Activity] AS GLDescription, ACTIVITY_SERVICE.ActivityID " & _

	 "FROM ELEMENTS INNER JOIN ((ACTIVITIES INNER JOIN ELEMENTS AS ELEMENTS_1 ON ACTIVITIES.Element=ELEMENTS_1.ElementID) INNER JOIN ACTIVITY_SERVICE ON ELEMENTS.ElementID=ACTIVITY_SERVICE.ServiceID " & _ 

	 "WHERE (ELEMENTS.ElementType) Not Like '"" & Replace(MyType,""'"",""'') & ""';"""

Open in new window

0
 

Author Comment

by:dwood1112
ID: 20406513
ok..it's working...the problem ended up being the double qoutes on the second line (marked below)...I was looking in the wrong place.

Thanks for your help

NSERT INTO DEPT_ACCOUNT_EXPENSE ( DepartmentID, GLDescription, Incoming_ActivityID ) " & _
"SELECT Val([ServiceID]) AS DeptID, [ELEMENTS_1]![ELEMENT] & " - " &   <<<<<<<
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Library not Registered 16 49
Sub Reports 8 22
Updating a table from a temp table 4 29
access 7 25
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

920 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

15 Experts available now in Live!

Get 1:1 Help Now