Solved

Access query SQL format into VB SQL format

Posted on 2007-03-26
8
1,254 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 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