Solved

Access query SQL format into VB SQL format

Posted on 2007-03-26
8
1,239 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
Comment Utility
Did you leave the "group by" clause out on purpose?
0
 
LVL 1

Author Comment

by:efedora
Comment Utility
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
Comment Utility
What is your exact code where you create&open the connection  and execute the SQL statement?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:efedora
Comment Utility
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
Comment Utility
>>>
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now