Solved

Excel VBA Error "Subquery returned more than 1 value"

Posted on 2010-08-29
6
853 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

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

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

757 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

19 Experts available now in Live!

Get 1:1 Help Now