Solved

Excel VBA Error "Subquery returned more than 1 value"

Posted on 2010-08-29
6
860 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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