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
Solved

SQL Question

Posted on 2007-12-04
6
213 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

840 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