Solved

SQL Question

Posted on 2007-12-04
6
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 66

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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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.

696 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