473,480 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using a form to input for TOP x PERCENT in VBA

12 New Member
Hello,

I am trying to use this form:



to select a program type and generate a random list to audit. This part is working correctly. But I would like to use a variable either a number or a percent to input as the TOP x PERCENT|NUMBER in my SQL statement from the form.

My code looks like this:
Expand|Select|Wrap|Line Numbers
  1. strTableName = "tblAuditSample_" & Format(Date, "ddmmmyyyy")
  2.  
  3.     strSQL = "SELECT TOP 10 PERCENT tblTemp.SHC_No, tblTemp.Project_Name, tblTemp.Program_Type, tblTemp.PMC " & _
  4.                 "INTO " & strTableName & " " & _
  5.                 "FROM tblTemp " & _
  6.                 "ORDER BY tblTemp.RandomNumber;"
  7.  
  8.     DoCmd.SetWarnings False
  9.     DoCmd.RunSQL strSQL
  10.     DoCmd.SetWarnings True
  11.  
When I change the hard coding to either a number or percent this works, but I want other people who are unfamiliar with VBA to be able to generate an audit report too.

Any help would be greatly appreciated!
Thanks.
Dec 10 '13 #1
4 1358
zmbd
5,501 Recognized Expert Moderator Expert
morganaj:
1) Your image link is blocked by most company IT policies in the USA and more than likely else where in the world. If the file is small enough, use the advanced editor and then manage attchments to upload the file here.

2) if I understand correctly you want to change this:
strSQL = "SELECT TOP 10 PERCENT tblTemp.SHC_No,
to this:
strSQL = "SELECT TOP X PERCENT tblTemp.SHC_No,

Now because I can't see your form all I can do is provide a basic change. I must assume that there is some sort of command button that executes the VBA Script:

If the form has a text box control named "txtPercent" then you need to validate that the user entry is numeric, positive, and between 1 and 100... that code I leave to you.

Once the validation is done then:
strSQL = "SELECT TOP " & me.txtPercent & " PERCENT tblTemp.SHC_No,
Dec 10 '13 #2
morganaj
12 New Member
When I try to change the SELECT statement to include the variable, that you suggested.

Expand|Select|Wrap|Line Numbers
  1.  strSQL = "SELECT TOP" & Me.strPercent & "PERCENT tblTemp.SHC_No, tblTemp.Project_Name, tblTemp.Program_Type, tblTemp.PMC " &
  2.                 "INTO " & strTableName & " " & _
  3.                 "FROM tblTemp " & _
  4.                 "ORDER BY tblTemp.RandomNumber;"
It all turns red and gives me a "Compile error: Expected: Expression."

And if I try to run it as is I get a "Compile error: Syntax Error."

Is this because I am running Access 2007, or am I missing an object library...
Dec 10 '13 #3
morganaj
12 New Member
Hey zmbd, thanks for the help. I figured it out.

Expand|Select|Wrap|Line Numbers
  1. "SELECT TOP" & " " & Me.strPercent & " " & " PERCENT 
Apparently the syntax wanted a literal space to make it run properly.

Thanks for the help!
Dec 10 '13 #4
zmbd
5,501 Recognized Expert Moderator Expert
Yes, the spacing is very importaint to the SQL engine.
If you will take a very carefull look at the final string in post#2, you may notice this... if one is not expecting to look for the space it is VERY easy to overlook and is one of the more common errors fixed (next the quote marks around strings) in ACC-SQL strings.

BOL W/ Your Project.
-z
Dec 10 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6194
by: icrash2 | last post by:
Hi, I'm using PHP to submit form input to MySQL and need a way to capitalize the first character for certain fields. Does anyone know of some way to achieve this? Much appreciated
8
5913
by: Reply Via Newsgroup | last post by:
Folks, I am using Apache 1.3.x with PHP 4.3.x and MySQL v4. Short question: Before I put my web form available on the internet, how can I test it from mis-use in such that special characters...
1
1690
by: champ.supernova | last post by:
Hi, I have a form which has a few different submit links, all of type image. This is critcal to the operation of the form, as it relies on the image co-ords being passed to another page, the...
15
4709
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
1
4458
by: Rob Mitchell | last post by:
(Sorry if this appears twice) Hi, I know this has probably been beaten to death but I was working with Class by Stickman -- http://www.the-stickman.com which uses DOM to create multiple file...
6
1876
by: neelay1 | last post by:
Hi all, I have a Javascript variable that contains the name of a form input- input_name = "document.myform.ip" How can I get the value of this form input, "ip" using the variable input_name?...
0
1048
by: jchris | last post by:
Hi I'm a newbie in ASP I have a form that everytime after some processing work all its input will disappear. May I know is there any solution that i can keep da form input? i'd tried using ...
8
2762
by: Phil Latio | last post by:
I've been creating an application over the last few weeks and generally pleased with what I have produced but one area is irritating me, form validation. At the moment the forms are simply...
2
1880
KeredDrahcir
by: KeredDrahcir | last post by:
I have a form and I'm using CSS3 to use the form input type "email". This seems to make sense since any browser that doesn't support it would default to text which is what I would otherwise use,...
1
1919
by: Stiofan | last post by:
Hi, This may be a bit of a noob question. I am writing a page which has a list of offers each with a checkbox input and 'id' set. Later in the page a form is used to capture visitors name and...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4495
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.