Solved

SQL Question

Posted on 2007-12-04
6
209 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

706 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

14 Experts available now in Live!

Get 1:1 Help Now