Solved

Access query SQL format into VB SQL format

Posted on 2007-03-26
8
1,246 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

864 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

16 Experts available now in Live!

Get 1:1 Help Now