473,608 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ 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 1364
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
6199
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
5920
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 are ignored? I have tried entering data such as `/bin/date > /tmp/1234` and this does not create a temporary file (which is what I would expect, meaning that my form is safe).
1
1705
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 processing of which is handled by someone else. What I need to do though, is to have a text link which simulates one of these image inputs being clicked, as some kind on onclick event. However, I've hit a brick wall looking for example code of how...
15
4741
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 button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
1
4465
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 input elements in a form. I'm trying to just hide all the form input elements
6
1884
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? input_name.value returns undefined value and input_name + ".value" returns the string document.myform.ip.value Any help will be highly appreciated. Thanks,
0
1051
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 response.control = private and response.buffer = true but it didn't work out though thanks
8
2769
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 static html templates and the form input is checked using a validation class. Basically each form field is checked, every error is stored to an array and at the end of checking of the complete form, the array is output neatly at the top of the form. ...
2
1882
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, however it does help with browsers that do support it. Firefox will check it's valid before the form can submit and iPhones will provide the E-mail keypad which I always find helpful. I was storing an E-mail address and tried to change it but it...
1
1922
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 email address. I was thinking of using hidden input fields in the form, but am having difficulty transferring the values. Im thinking I'll have to go javascript, but am not as comfortable with it as I am with PHP. If anyone has a better idea,...
0
8000
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8330
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3960
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1328
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.