Solved

SQL Question

Posted on 2007-12-04
6
212 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

831 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