473,511 Members | 15,408 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 1932
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.column(1)

J
Jul 31 '07 #2

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

Similar topics

2
2313
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
3520
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...
0
1788
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...
1
1669
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...
3
2879
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...
2
3631
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...
4
2805
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...
4
1732
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...
2
1631
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...
0
7138
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...
0
7353
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,...
0
7418
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...
1
7075
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
5662
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,...
0
4737
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
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
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.