473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can my form allow the user to select fields to sort on?

sueb
379 256MB
I have a database that was created by a SQL programmer, and it's all wonderful, except that the programmer never completed it, and it's pretty sophisticated for my user community, so that means no one can complete it or maintain it.

I'm re-writing its functionality (and completing it!) so that it just uses familiar Access utilities. It doesn't need anything fancy, so although it's taking me a little time to accomplish, it's going to be simpler for the users to maintain, to the extent that they are able.

Currently, there is a form that presents one unbound control that functions as a filter for a specific field, and three pairs of controls, each pair of which allows the user to select a field and a sort order for that field. (I've attached a picture of the form.)

The underlying code simply runs a query and presents the data table, but I want to present the data in a sensibly-designed form instead of just in an unwieldly line of fields marching off to the right. Here's the code, and how do I make the code use the form I've created instead of just the raw query:

Expand|Select|Wrap|Line Numbers
  1. 'Open the selected query.  Replace the order by clause with the order selected by user.
  2. Private Sub cmdViewCategoryData_Click()
  3.  
  4.     Dim DB
  5.     Dim qd As DAO.QueryDef
  6.     'Dim rs As Recordset
  7.     Dim Sort As String
  8.     Dim sql As String
  9.     Dim queryName As String
  10.     Dim querySql As String
  11.     Dim OrderByPosition As Integer
  12.  
  13.     Sort = " ORDER BY [" & Me.cboSort1.Value & "] " & Me.FirstSortAscDesc.Value & ", [" & Me.cboSort2.Value & "] " & Me.SecondSortAscDesc.Value & ", [" & Me.cboSort3.Value & "] " & Me.ThirdSortAscDesc.Value
  14.     'Sort = " ORDER BY [" & Me.cboSort1.Value & "], [" & Me.cboSort2.Value & "], [" & Me.cboSort3.Value & "]"
  15.     queryName = "qry" & Me.cboHospCat.Value
  16.  
  17.     Set DB = CurrentDb()
  18.     Set qd = DB.QueryDefs(queryName)
  19.     querySql = qd.sql
  20.  
  21.     'If there is an Order By clause, replace it with new order by, or else add it to end:
  22.     If InStr(querySql, "ORDER BY") > 0 Then
  23.         OrderByPosition = InStr(querySql, "ORDER BY")
  24.     Else
  25.         OrderByPosition = InStr(querySql, ";") 'Len(querySql)
  26.     End If
  27.  
  28.     querySql = Left(querySql, OrderByPosition - 1)
  29.     querySql = querySql & Sort & ";"
  30.     qd.sql = querySql
  31.  
  32.     DoCmd.OpenQuery queryName
  33.     Set qd = Nothing
  34.     Set DB = Nothing
  35.  
  36. End Sub
** Edit **

Attached Images
File Type: jpg BillHold form.jpg (38.3 KB, 821 views)
Aug 17 '11 #1
20 3602
NeoPa
32,556 Expert Mod 16PB
This sounds like some fun Sue.
  1. Start by designing a form that will show the data as you want it. This is what you will use to display the data that you have filtered and sorted by your three fields.
  2. For the filtering, I suggest you go and have a play in Example Filtering on a Form. It includes an example database you can play with, and works like a tutorial for the concept of filtering. It covers most of what you need to do this whole job.
  3. The sorting of the data using the three fields specified in your three pairs of controls is similar but slightly different. You will use the data in these controls to form an OrderBy string which matches the ORDER BY clause of the SQL of the underlying query. Unfortunately, whereas the filtering is supported by a parameter to DoCmd.OpenForm(), the sorting is not. This means you will need to apply this after the form has already been opened. This can be done by passing the data as OpenArgs or setting the properties explicitly in the calling code after the form's been opened. We can cover details for this later should you decide this route appeals to you.

Actually, that will be it. It's a good approach and makes it look professional. I like it.
Aug 17 '11 #2
sueb
379 256MB
Thanks for this, NeoPa! I certainly will dive into this this evening after work, and will report back (of course, you know I will, since there will be further questions! :D)
Aug 18 '11 #3
sueb
379 256MB
I thought I'd just check out that thread now, even though I can't start on it yet, but something must be wrong with the link or page or something cuz I get an error every time I click on it.
Aug 18 '11 #4
NeoPa
32,556 Expert Mod 16PB
I've heard some people are temporarily having problems on the site with IE 8. I tested it here and all seems fine to me with Mozilla Firefox 5. Let me know how you manage (and if there are further questions them happily bring them on).
Aug 18 '11 #5
sueb
379 256MB
I still can't access that page today, and I'm not allowed to use another browser here at work. I don't suppose you could sort of paste the gist of it in a message could you? Or maybe email it to me or something?
Aug 19 '11 #6
NeoPa
32,556 Expert Mod 16PB
Done Sue.
Aug 19 '11 #7
sueb
379 256MB
Thanks! You're the best, NeoPa!
Aug 19 '11 #8
NeoPa
32,556 Expert Mod 16PB
I'm very happy to have helped Sue :-)
Aug 19 '11 #9
sueb
379 256MB
Let me apologize, starting off, for not having had time to do my homework with the filtering example you posted, NeoPa, but maybe you'll be willing to clear up this one thing for me anyway.

I'm borrowing some code from the original database, but I'm getting an error that I don't understand. Here are the lines of code in question:

Expand|Select|Wrap|Line Numbers
  1.     Dim DB
  2.     Dim qd As DAO.QueryDef
  3.     Dim Sort As String
  4.     Dim sql As String
  5.     Dim queryName As String
  6.     Dim querySql As String
  7.     Dim OrderByPosition As Integer
  8.  
  9.     Sort = " ORDER BY [" & Me.cboSort1.Value & "] " & Me.FirstSortAscDesc.Value & _
  10.             ", [" & Me.cboSort2.Value & "] " & Me.SecondSortAscDesc.Value & _
  11.             ", [" & Me.cboSort3.Value & "] " & Me.ThirdSortAscDesc.Value
  12.     queryName = "qry" & Me.cboHospCat.Value
  13.  
  14.     Set DB = CurrentDb()
  15.     Set qd = DB.QueryDefs(queryName)
  16.  
When it hits line 16, it complains that "Item not found in this collection." This code works perfectly in the original database. I don't really understand the whole "Dim DB" thing, so I don't know why my version doesn't seem to have ".QueryDefs". (I'm assuming that's the "item" that isn't being found.) Maybe you can point me in the right direction about what I'm missing in my version that is in the original?

Thanks for any advice!
Aug 31 '11 #10
NeoPa
32,556 Expert Mod 16PB
The error message indicates that the QueryDef (saved query), called qryX where X represents the current value of cboHospCat, doesn't exist in your database. Does that help?

Your code doesn't make a lot of sense out of context I'm afraid, so I'm not in a good position to determine what you're trying to do or suggest any alternatives at this stage.
Sep 1 '11 #11
sueb
379 256MB
Well, my form does have all the combo boxes referenced in the code. I couldn't copy the whole form (attached to my original post) from the original database into mine, but I could (and did) copy the combo boxes into a new form in my new database. All the properties copied, so I assumed that the code would work (certainly at least to the point of being able to create the query).
Sep 1 '11 #12
sueb
379 256MB
When I set a break at line 18, queryname = "qryEmergency", which is what I would expect, and Sort = " ORDER BY [DSCHRG_DT] ASC, [ACCT_BAL] ASC, [PAT_ACCT_NBR] ASC", which is also what I would expect. However, qd = Nothing -- does that seem quite right? When I hover over qd.sql in line 19, it says "qd.sql = <Object variable or With block variable not set>". Does that just mean that .sql hasn't been set yet? Or does it mean something more sinister about the validity of qd's declaration?
Sep 1 '11 #13
NeoPa
32,556 Expert Mod 16PB
I'm still reading that as saying the database doesn't contain a QueryDef object called [qryEmergency].

BTW Are you aware that you can export forms to files and import them again from files? That may be a way to transfer the original form across even if the database versions are different.
Sep 1 '11 #14
sueb
379 256MB
Okay, since it looks to me like I've defined an object called queryname that has a value of "qryEmergency", where in the database do I need to create the QueryDef object, and how do I do it?
Sep 1 '11 #15
NeoPa
32,556 Expert Mod 16PB
Not quite Sue, but you're thinking in the right direction at least.

You have a simple string variable called queryName defined on line #9. This is set on line #15 to be equal to "qry" plus the value in Me.cboHospCat. This indicates to me you have a control (almost certainly a ComboBox from the name) which, at the time of testing, was set to the string value "Emergency" (hence the resulting value of queryName was "qryEmergency").

When, on line #18, you tried to reference QueryDefs("qryEmergency") it said, in computer speak, "Don't be daft girl! We have no [qryEmergency] here".

QueryDefs sound complicated, but they're simply the term that refers to saved query objects. Most people think of them simply as queries (They're even stored under the tab called queries in the database window), but they're not really queries as such. They include queries perhaps, but the object that they are is a QueryDef. In short the error is that the query of that name doesn't exist in your database.
Sep 1 '11 #16
sueb
379 256MB
OMG, so that's what's missing! Yes, in the original database, there are a slew of queries named exactly this kind of thing! Lovely--I'll just copy them all over. Thanks, NeoPa!

(the dawning of that particular light was more of a lighting bolt--almost gave me a headache! :D)
Sep 1 '11 #17
NeoPa
32,556 Expert Mod 16PB
NeoPa:
"Don't be daft girl! We have no [qryEmergency] here"
If the translation helps that much there are threads here that deal with reconfiguring the error messages. You could set up a special version called "Access 2010 With Attitude!".
Sep 1 '11 #18
sueb
379 256MB
Am I able to create queries on the fly? I'm thinking of the day when the user creates a new Hospital Category; it would be lovely if the code could just handle that.
Sep 2 '11 #19
NeoPa
32,556 Expert Mod 16PB
The short answer is Yes, but that doesn't allow for much. The variations on what you could mean when you ask that question (as opposed to the question itself) leave it fairly meaningless. However, the full question should be posted in a separate thread I suspect.
Sep 2 '11 #20
sueb
379 256MB
Okay, will do--thanks!
Sep 2 '11 #21

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

Similar topics

2
by: please-answer-here | last post by:
Scenario: I'm having an asp page with a form with an action pointing to itself. The reason for that, it that the asp page depending on changes in any of two select fields with data from 2...
3
by: RC | last post by:
I can't quite grasp the concept of creating custom reports depending upon what options a user picks on a Form. For example, the user clicks on a "Print Reports" button and a Form pops up. On the...
0
by: Fercat | last post by:
Hi! I receive in an Outlook mail folder some custom forms that contain user defined fields (for example, Client Name, Contract Number...). The problem is that when I finish linking this folder...
1
by: Bob Alston | last post by:
Anyone have experience in building Access apps with user definable fields? (Not the kind of fields where you just let the user define the label for a pre set number of predefined fields.) I...
1
by: Daveo | last post by:
Hi folks, Please see the topic below: ...
5
stephane
by: stephane | last post by:
i've got two table ServicesGroups ( id, name ) and Services ( id, group_id, name ) tables linked on ServicesGroups.id=Services.group_id at first select field i list ServicesGroups table, so...
45
by: dizzydangler | last post by:
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become.... I'm...
3
by: Bigdaddrock | last post by:
I have a form that allows for input of new Customer Billing address fields (B1, B2, B3) as well as fields for the Shipping Address (S1, S2, S3). Sometimes these are the exact same! I have text...
4
by: samuel jenny | last post by:
I'm using an order form where clients may update quantity in a text field. To make it more customers friendly, I would like to have the field with the select format. But doing so in the form,...
3
by: emtang | last post by:
How to refresh to original when form is loaded after the user right click sort Steps: 1. Form is loaded. 2. User right click on sort A-Z in Contributor. 3. User wants to get back the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.