?
Solved

Not selecting the Top 10 from the VBA/SQL Statement

Posted on 2011-03-21
16
Medium Priority
?
490 Views
Last Modified: 2012-05-11
I have some VBA Code that is doing a Select TOP statement, where it takes the TOP number from a number that the user is prompt for on a form.

For some reason, it is selecting all the records not just the TOP 10.

I have included the VBA Code in the Code section.

Can someone please tell me what the issue might be?

Thanks,

gdunn
Private Sub cmdAssign_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim lngTop As Long
Dim strSQL As String
 
 
Set db = CurrentDb
db.QueryDefs.Delete "qryWorkAssigned"
Set qdf = db.CreateQueryDef("qryWorkAssigned")
lngTop = CLng(InputBox("Enter Amount to Assign"))
strSQL = "SELECT TOP " & lngTop & " numDay, numDay, txtAssignedTo, txtAssignedBy, dtmDateAssigned, numID, txtPmtTyp, txtSuff FROM [qryUnPro] ORDER BY numID"
 
qdf.SQL = strSQL
 
DoCmd.RunSQL "UPDATE qryWorkAssigned SET qryWorkAssigned.txtAssignedBy = [Enter Your User ID], qryWorkAssigned.txtAssignedTo = [Enter User ID Being Assigned], qryWorkAssigned.dtmDateAssigned = [Enter Today's Date]"
 
'Refresh MAPD Form
[Form_frmUnproUnassignedsubform].Requery
 
End Sub

Open in new window

0
Comment
Question by:gdunn59
[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
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35186490
I think it's because the input is a string so it wraps it in quotes when it just needs a straight number.
0
 
LVL 75
ID: 35186592
"when it just needs a straight number."
It's converted in the line of code above >> lngTop = CLng(InputBox("Enter Amount to Assign"))

0
 
LVL 75
ID: 35186610
Have you looked at query qryWorkAssigned once it has been created directly in the query designer and checked how many records are return .... ?
You can do that by commenting out everything after

qdf.SQL = strSQL
 
mx
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 26

Expert Comment

by:Nick67
ID: 35192258
Throw a msgbox right after this line

strSQL = "SELECT TOP " & lngTop & " numDay, numDay, txtAssignedTo, txtAssignedBy, dtmDateAssigned, numID, txtPmtTyp, txtSuff FROM [qryUnPro] ORDER BY numID"

Msgbox strSQL

What is strSQL?  are there quotes or other nonsense in it?

If not, simplify first.
Try the code below.
Sub out the * and start adding in fields.

Something about TOP and Order by is tickling my spider sense, though
Dim db As DAO.Database
Dim rs As Recordset
Dim lngTop As Long
Dim strSQL As String
 
Set db = CurrentDb
lngTop = CLng(InputBox("Enter Amount to Assign"))
strSQL = "SELECT TOP " & lngTop & " *  from qryUnPro;"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
rs.MoveLast
MsgBox rs.RecordCount

Open in new window

0
 
LVL 1

Author Comment

by:gdunn59
ID: 35194541
Nick67:

You're correct in saying that something about the Order by is tickling your spider sense.  So before I did anything out, I commented out the Order by clause, and then it worked like a charm.  Go figure!

Is there anyway around this?  So I can still sort?

Thanks,
gdunn59
0
 
LVL 1

Author Comment

by:gdunn59
ID: 35194543
Oops, meant to say "before I did anything else", not "out".
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35194618
You temptable it

select * from ( "select top x from tblWhatever") order by some column
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35194625

"select * from ( ELECT TOP " & lngTop & " numDay, numDay, txtAssignedTo, txtAssignedBy, dtmDateAssigned, numID, txtPmtTyp, txtSuff FROM [qryUnPro] ) as temptable ORDER BY numID"
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35194630
Well, you get the idea
0
 
LVL 75
ID: 35194658
You never answered the question I asked above:

"Have you looked at query qryWorkAssigned once it has been created directly in the query designer and checked how many records are returned .... ?
0
 
LVL 1

Author Comment

by:gdunn59
ID: 35194764
DatabaseMX:

Sorry, yes I did and it is not just doing the Top number entered.

Based off of what Nick67 was saying, I commented out the "Order by" clause and it worked like a charm after that.

Thanks,

gdunn59
0
 
LVL 75
ID: 35194772
Orderby should not affect this.
Are you saying the actual query does the same thing?
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 1000 total points
ID: 35194938
The spidey sense was going.
Order by shouldn't do that..but it does sometimes... I think just in Access, not in sprocs

http://bytes.com/topic/access/answers/678509-select-top-order
http://www.mrexcel.com/forum/showthread.php?t=496073

I've had it happen, I can't remember where, hence the spidey sense.
0
 
LVL 75
ID: 35194967
".but it does sometimes"
Well ... it works like this - from Help:

"Typically, you use the TopValues property setting together with sorted fields. The field you want to display top values for should be the leftmost field that has the Sort box selected in the query design grid. An ascending sort returns the bottommost records, and a descending sort returns the topmost records. If you specify that a specific number of records be returned, all records with values that match the value in the last record are also returned."

So ... unless you have duplicates in the Field that is being sorted, then you should only get the TOP amount of records - certainly not All.

mx
0
 
LVL 26

Expert Comment

by:Nick67
ID: 35199328
Bugs do happen.
And as @gdunn59 noted
"SELECT TOP " & lngTop & " numDay, numDay, txtAssignedTo, txtAssignedBy, dtmDateAssigned, numID, txtPmtTyp, txtSuff FROM [qryUnPro] ORDER BY numID"
returns all the records, not the top X
"SELECT TOP " & lngTop & " numDay, numDay, txtAssignedTo, txtAssignedBy, dtmDateAssigned, numID, txtPmtTyp, txtSuff FROM [qryUnPro]"
returns the top X and not sorted.

Why?

I can't say.
I've had it happen, but it was quite some time ago.
Most times it doesn't matter.
You can force a report or a form to sort regardless of the underlying query's order, so then it doesn't matter.
If your doing recordset work, or combo boxes and list boxes it can be a pain.

But it happens.

It isn't the first bug that's been encountered, and it won't be the last.
FWIW, my backend is now SSEE 2005 -- and I can't replicate the problem anymore.
But I remember having it :)
0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35199369
Try doing a nested select. So select top 10 from (allyourstuff) order by numid
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

800 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