Solved

Excel VBA Error "Subquery returned more than 1 value"

Posted on 2010-08-29
6
859 Views
Last Modified: 2013-11-10
So I have a query in Excel VBA code that's usign a subquery which is returning more than 1 row, so I get this:

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression"

How do I go about handling this error? I'd like to have a form popup (which Ive already designed" which prompts the user for a name. I'd then run another query specifying that name.

How do I check to see if more than 1 row was returned, or how do I check for this error so I can have the other form display? Thanks!
0
Comment
Question by:asphaltninja
  • 3
  • 2
6 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33553745
You must have a subquery somewhere.
To quick-fix it, add a TOP 1 in there, e.g. for a query like

select customername, (select contactname from contacts where contact.customerid=customer.customerid)
from customer
where customer.id=123

You can change it to

select customername, (select TOP 1 contactname from contacts where contact.customerid=customer.customerid)
from customer
where customer.id=123

The question is would it matter which record of the subquery (in this case contact) is showed?
0
 

Author Comment

by:asphaltninja
ID: 33553770
Hello and thanks for your reply. Yes it would matter; what I'm dealing with here are two separate records that have the same reference number, which is what the query is using in its where clause.

They aren't supposed to have the same reference number but someone (or some people) dropped the ball. I'll eventually have to fix it but for now I'd liek them to be able to then specify the name if there are two records returned to be more specific about the query.

Any suggestions?
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33553784
Using the same example, I would fire a query

select contactname from contacts where contact.customerid=123

And check record count.  If it is >1, loop through the recordset and extract the list of contactname to present to the user. Once a selection is made, add that back to the modified query

select customername, (select contactname from contacts where contact.customerid=customer.customerid and contactname='John')
from customer
where customer.id=123
0
 

Author Comment

by:asphaltninja
ID: 33553808
So you're saying I should use the built-in SQL rowcount function? Should I store the value in a variable and then check the value in my vb code, ie:

Run query, check rows affected
select  @variabletoShowNumberofRowsAffected
strRows = query.ExecSQL("Query statement here")
If Rows > 1 Then
'Display form asking for Name value
'Run new query with name value
End If
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 500 total points
ID: 33553816
>> Run query, check rows affected
That will give you the error.  Which is fine if you use error handling and detect some part of the error text like "Subquery returned more than 1 value"

Otherwise, run the subquery part only.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

808 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