Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access query SQL format into VB SQL format

Posted on 2007-03-26
8
Medium Priority
?
1,271 Views
Last Modified: 2013-12-25
I want to copy a recordset to excel and the sql statement I'm using for the recordset is based on a query I already had made.  I thought I'd made all the necessary format changes between the SQL statement in the query and the VB code SQL statement but I can't get the VB code to run the sql statement and my best guess is that it is stumbling in the SELECT portion on the fields called Expr1 and Expr2 from the original query.  
Expr1 is getting multiple fields of info converted into 1 field for excel with spaces in between the info
Expr2 is an immediate if-then statement converting multiple descriptions into 2 part# types

the error message i've been consistently getting is "Run-time error '-214721908 (80040e0c)': Command text was not set for the command object."

Here is the code
partSQL = "SELECT tblItemDetail.[Damage Type], tblItemDetail.PO, tblItemDetail.[Supplier / Salvage / Junk], tblEnteredClaims.Pro, _

tblItemDetail!Qty & ‘ ‘ & tblParts!UM & ‘ ‘ & tblItemDetail![Part #] & ‘ ‘ & tblParts!Description AS Expr1, _

IIf([Damage Type]=’damage’,’88888K52’,IIf([Damage Type]=’concealed damage’,’88888K52’,IIf([Damage Type]=’empty_ carton’,’88888K52’,’88888K42’))) AS Expr2 _

FROM (tblEnteredClaims INNER JOIN tblItemDetail ON tblEnteredClaims.Pro = tblItemDetail.Pro) INNER JOIN tblParts ON_ tblItemDetail.[Part #] = tblParts.[Part #] _

Having (((tblEnteredClaims.Pro) = ‘” & ProIn & “’))"
0
Comment
Question by:efedora
[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
  • 4
  • 3
8 Comments
 
LVL 20

Expert Comment

by:gatorvip
ID: 18795550
Did you leave the "group by" clause out on purpose?
0
 
LVL 1

Author Comment

by:efedora
ID: 18795744
Sorry I should have included that,
I did have this one in but I had tried removing it as part of troubleshooting here it is
GROUP BY tblItemDetail.[Damage Type], tblItemDetail.PO, tblItemDetail.[Supplier / Salvage / Junk], tblEnteredClaims.Pro, tblItemDetail!Qty, tblParts!UM, tblItemDetail![Part #], tblParts!Description, IIf([Damage Type]=’damage’,’88888K52’,IIf([Damage Type]=’concealed damage’,’88888K52’,IIf([Damage Type]=’empty carton’,’88888K52’,’88888K42’))) AS Expr2
0
 
LVL 20

Expert Comment

by:gatorvip
ID: 18796751
What is your exact code where you create&open the connection  and execute the SQL statement?
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18797926
>>Command text was not set for the command object."<<
I would have to guess you are missing the line (or similar):
cmd.CommandText = partSQL
0
 
LVL 1

Author Comment

by:efedora
ID: 18799423
OK, I think I fixed the problem opening the RecordSet, I  tested it on a table instead of a SQL statement and that worked, I still included that code below. However when I plugged the SQL statement back in I recieved a new error message:
Run-time error ‘-2147217900 (80040e14)’:

Syntax error (missing operator) in query expression “IIf([Damage Type]=’damage’,’88888k52’,IIf([Damage Type]=’concealed damage’,’88888k52’,IIf([Damage Type]=’empty carton’,’88888k52’,’88888k42’)))’.

Code to open Record set:

'establishes the Part line recordset
Dim cnn2 As ADODB.Connection
Set cnn2 = CurrentProject.Connection
Dim PARTrs As New ADODB.Recordset
PARTrs.ActiveConnection = cnn2

'establishes Parts line sql statement
PARTrs.Open "SELECT....

'copies the part line record set to the spreadsheet
xlsh.Range("d3").CopyFromRecordset PARTrs






0
 
LVL 20

Accepted Solution

by:
gatorvip earned 2000 total points
ID: 18799499
>>>
Syntax error (missing operator) in query expression “IIf([Damage Type]=’damage’,’88888k52’,IIf([Damage Type]=’concealed damage’,’88888k52’,IIf([Damage Type]=’empty carton’,’88888k52’,’88888k42’)))’.
<<<

I don't know if this is your actual code but the opening and closing quotes are different in what's pasted above.
0
 
LVL 1

Author Comment

by:efedora
ID: 18801189
Okay, after taking a fresh look at this whole problem I was able to get around the If-then statement issue that was causing the previous error message by creating a new table with the values I needed and then referencing that in the SQL
My problem now is with concatenating multiple fields into one field in order to copy it to excel.  
People have had similiar questions to this and I did what they were instructed to do but now I get no error message but it just not pasting the concatenated statement to excel and I don't know why.
here is the code:

(tblItemDetail.Qty + ' ' + tblParts.um + ' ' + tblItemDetail.[part #] + ' ' + tblParts.description) As partnote
0
 
LVL 1

Author Comment

by:efedora
ID: 18801980
I solved this one myself the + should be & and everything else stayed the same.  I'm using an ADO database and the people before me who asked a similar question were using DAO.  Thanks for the input on the question.  I appreciate it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

670 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