473,586 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting Top Values/Records for a query via Form

53 New Member
Hi,

I have a form that opens a report. The form allows the user to pick a particular warehouse or supplier and an order to sort by. Here is the code for it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2.  
  3.     Dim strSQL As String
  4.     If Me.cboSort1 <> "" Then
  5.         strSQL = strSQL & Me.cboSort1
  6.         If Me.Chk1 = True Then
  7.             strSQL = strSQL & " DESC"
  8.         End If
  9.         strSQL = strSQL & ", "
  10.         DoCmd.OpenReport "rptTopSuppliers", acViewPreview
  11.         DoCmd.Maximize
  12.     If strSQL <> "" Then
  13.         strSQL = Left(strSQL, (Len(strSQL) - 2))
  14.         Reports![rptTopSuppliers].OrderBy = strSQL
  15.         Reports![rptTopSuppliers].OrderByOn = False
  16.     Else
  17.         Reports![rptTopSuppliers].OrderByOn = True
  18.     End If
  19.  
  20.     Else
  21.         MsgBox "Please choose a field to sort by.", vbOKOnly, "Missing Information"
  22.     End If
  23. End Sub
  24.  
In this, I want to include a list box that tells the report to only return top 5, 10, 25 or all records (or if possible, have the user enter how many records and if left blank return all records). I need help incorporating this. I tried it briefly, but I am too unfamiliar with VB. I need a good place to start I think, because I'm baffled at the moment.
Jul 24 '07 #1
1 1938
JConsulting
603 Recognized Expert Contributor
Hi,

I have a form that opens a report. The form allows the user to pick a particular warehouse or supplier and an order to sort by. Here is the code for it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2.  
  3.     Dim strSQL As String
  4.     If Me.cboSort1 <> "" Then
  5.         strSQL = strSQL & Me.cboSort1
  6.         If Me.Chk1 = True Then
  7.             strSQL = strSQL & " DESC"
  8.         End If
  9.         strSQL = strSQL & ", "
  10.         DoCmd.OpenReport "rptTopSuppliers", acViewPreview
  11.         DoCmd.Maximize
  12.     If strSQL <> "" Then
  13.         strSQL = Left(strSQL, (Len(strSQL) - 2))
  14.         Reports![rptTopSuppliers].OrderBy = strSQL
  15.         Reports![rptTopSuppliers].OrderByOn = False
  16.     Else
  17.         Reports![rptTopSuppliers].OrderByOn = True
  18.     End If
  19.  
  20.     Else
  21.         MsgBox "Please choose a field to sort by.", vbOKOnly, "Missing Information"
  22.     End If
  23. End Sub
  24.  
In this, I want to include a list box that tells the report to only return top 5, 10, 25 or all records (or if possible, have the user enter how many records and if left blank return all records). I need help incorporating this. I tried it briefly, but I am too unfamiliar with VB. I need a good place to start I think, because I'm baffled at the moment.

You're going to have to split up your starting strSQL this way:

Expand|Select|Wrap|Line Numbers
  1.         If Nz(Me.mylistbox, "") <> "" Then
  2.             strSQL = "select top" & Me.mylistbox & " FROM yourtable "
  3.         End If
  4.  
Remember that combo boxes are 0 based..so the first column is going to be the value used, unless you select the column number me.mylistbox.co lumn(1)

J
Jul 31 '07 #2

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

Similar topics

2
2319
by: Matik | last post by:
Hello everyone, Small and (I think) very simple quesiton;-) which makes me creazy. Let's say I have two tables listed below: T1 ==== IDX ==== 1
1
3540
by: Joseph Del Medico | last post by:
I'm trying to use a query whose SQL view is shown below to get a recordset of all first quarter records from a table for a year that is in the textbox of a form, so I can sum up the totals for the first quarter for every person and display them on the form. The query works fine when I preview it in the query builder, but when I try to open a...
0
1807
by: allyn44 | last post by:
HI--I have 2 tables Cut: cut ID, HistNumb, Block, date: Cut Id is the primary key, the other 3 fileds are indexed to be unique Slides: Cutid SlideID, and various other fields: there can be multiple slide records per CutID. This table is a subdatashet of the Cut table--so the CutId is stored in it I have a form based on a query between...
1
1676
by: Eskil | last post by:
Hi I have a form that supplies my query with information on two different variables. The form uses a lookup to display a list of 5 different customer types and 5 different types of meetings/activities in two different combo boxes. Now, the values of form variables go into a query upon which a report is generated. By using those two...
3
2891
by: M.L. Abram | last post by:
Hello all, I do not know if this question is regarding table design, queries, or programming. Below, I have given a table design using Access 2003. Fields 'Product' and 'Color' are primary keys and 'Active' is a Boolean data type. What I am trying to accomplish is having only one record selected active out of the 'Product' field for each...
2
3642
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code /////////////////////////////////////////////////////////////////////////////////////// <form action="whatever.php" method="post"> <select name="zip_code"...
4
2815
by: greg | last post by:
Hi, I don't think my message posted correctly so here it is. Is there any way to access the individual values of a form text box? I want to iterate through all of the rows and access the values of a text box in my form. thanks.
4
1735
by: Jeffrey Davis | last post by:
I'm hoping that someone here can give me some assistance with a database I'm trying to set up. My skills in Access are fairly basic, and I'm trying to skill up, but some of the stuff is a little opaque. I'm trying to put some data I've got on paper into Access. Recently, I got together with some other people marketing organic produce and...
2
1635
by: rousseaud | last post by:
Hello, I need some help/advise about how to tackle an Access problem. Right now, it's kind of a big idea and I would really appreciate any input about how to do it properly. I essentially want to create a form that queries various records. I then want a user to be able to select various and specific records from the results of the query to...
0
7839
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...
0
8202
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. ...
0
8338
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...
0
6614
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...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
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...

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.