Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Top N, where N is a variable from a form 1

Status
Not open for further replies.

adita1983

Technical User
Dec 28, 2004
7
RO
Hi,

cmb_Procent is a combobox in a form.

Here is my code :

strSQL = "SELECT TOP cint(" & Me.cmb_Procent & " /10) tblTemp.* " & _
"INTO " & strTableName & " " & _
"FROM tblTemp " & _
"ORDER BY tblTemp.RandomNumber;"

The error is : "The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

Thanks in advance.

Regards,
Adrian
 
your syntax is not correct :

try this

ssql = "SELECT tbltemp.*, CInt(" & Me!cmb_Procent & ")/10 AS Expr1 INTO tbltemp2" & _
" FROM tbltemp" & _
" ORDER BY tbltemp.randomnumber;"

 
The calculation of the number of the TOP records must be part of the VBA code, not the SQL statement:

Code:
strSQL = "SELECT TOP " & cint(Me.cmb_Procent/10) & " tblTemp.* " & _
             "INTO " & strTableName & " " & _
             "FROM tblTemp " & _
             "ORDER BY tblTemp.RandomNumber;"
 
Another way:
strSQL = "SELECT TOP " & (Me!cmb_Procent \ 10) & " tblTemp.* " & _

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
adita1983

in addition to the code i posted, the errors i found were a missing comma before the tblTemp.* and the fact that the CREATE TABLE won't allow circular reference (create from tbltemp into tbltemp)
 
Hi,

Thaks guys, it works.The problem was with syntax.

strSQL = "SELECT TOP " & cint(Me.cmb_Procent/10) & " tblTemp.* " & _
"INTO " & strTableName & " " & _
"FROM tblTemp " & _
"ORDER BY tblTemp.RandomNumber;"

is correct.I have one more question.I want someting like that.

strSQL = "SELECT TOP " & cint(Me.cmb_Procent/10*count(*)) & " tblTemp.* " & _
"INTO " & strTableName & " " & _
"FROM tblTemp " & _
"ORDER BY tblTemp.RandomNumber;"

Same problem : The error is : "The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

Tkss a lot,
Adrian

 
Why not simply using the TOP n PERCENT syntax ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

PHV can u be more explicit ?I need Me.cmb_Procent percent from total records of my table.

Tks in advance,
Adrian
 
Something like this ?
strSQL = "SELECT TOP " & Me.cmb_Procent & " PERCENT tblTemp.* " & _

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top