473,405 Members | 2,300 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,405 software developers and data experts.

Adding additional options to combo box based on SQL statement

46
Hello,

Suppose I have combo box that is linked to the following SQL statement: "SELECT DISTINCT Column1 FROM Tbl1;"

Suppose the results are 3 fields: Red, Green, Yellow

What if I wanted to add an additional field (say, Blue) to this list, although it doesn't appear in the table? I know I can type in the values myself, but I want the combo box to look the table (which is dynamic/changing), and add a few additional options.

Any advice is very much appreciated.
Nov 19 '09 #1

✓ answered by ajalwaysus

What I have done in the past, and I don't know if there is a better way, but I UNION ALL the values I want together.

First, I created a dummy table called DUAL (it's an oracle thing), that has only one column and no values in it.

Second, I write my query as such:
Expand|Select|Wrap|Line Numbers
  1. SELECT "Blue" FROM DUAL
  2. UNION ALL
  3. SELECT DISTINCT Column1 FROM Tbl1;
This way my combo box has all the values in the table, plus the value "blue". I personally would say an actual value should be added to the table. I derived this idea so that I could include a blank in my combo boxes.

Hope this helps,
-AJ

16 3759
ajalwaysus
266 Expert 100+
What I have done in the past, and I don't know if there is a better way, but I UNION ALL the values I want together.

First, I created a dummy table called DUAL (it's an oracle thing), that has only one column and no values in it.

Second, I write my query as such:
Expand|Select|Wrap|Line Numbers
  1. SELECT "Blue" FROM DUAL
  2. UNION ALL
  3. SELECT DISTINCT Column1 FROM Tbl1;
This way my combo box has all the values in the table, plus the value "blue". I personally would say an actual value should be added to the table. I derived this idea so that I could include a blank in my combo boxes.

Hope this helps,
-AJ
Nov 19 '09 #2
bbatson
46
That's a great idea and I'm sure that will work. Thanks AJ.
Nov 19 '09 #3
Megalog
378 Expert 256MB
Actually you dont need to create a dummy table, you can alias the value in there.
This should work:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT "Blue" AS [Column1] FROM Tbl1
  2. UNION ALL
  3. SELECT DISTINCT Column1 FROM Tbl1;
Nov 19 '09 #4
ajalwaysus
266 Expert 100+
@Megalog
That would work too, I would just wonder which would be faster. Now faster doesn't mean much in this case, but my mind goes straight to worst case scenarios, like a massive table.

-AJ
Nov 19 '09 #5
Megalog
378 Expert 256MB
Hrm performance-wise, I'm not sure.

I would add another table like you suggested if the added values would possibly change down the road. But in cases where the data isnt going to be immense, or you simply want to add an "All" option, it's a quick permanent fix. Two different solutions for two separate scenarios.
Nov 19 '09 #6
ajalwaysus
266 Expert 100+
@Megalog
It is a good argument that I would like to look into more later, but for this case, I don't see a downside to either solution. Nice catch Megalog.

-AJ
Nov 19 '09 #7
NeoPa
32,556 Expert Mod 16PB
Interesting point. As I just happen to have a table lying around with 149,981 records in it I thought I'd try it out. I can confirm, without any shadow of a doubt, that trawling through these records, even with the DISTINCT predicate, takes a while to complete. Performance-wise it's not good. This is a bit of a shame as that's how I normally do it. I see a change of thinking coming my way :(

Having said that, if you know you have a local table with not too many records then this is a quick and easy method.
Nov 23 '09 #8
NeoPa
32,556 Expert Mod 16PB
Well. Maybe not.

For absolute thoroughness I decided to test on the same table after closing and re-opening the database to lose any caching advantages. The results surprised me a little after my earlier tests.

I used two sets of SQL and they both came in at 5 seconds (as exactly as I could tell).
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 'All' AS Result
  2. FROM tblFileStructure
  3. UNION ALL
  4. SELECT DISTINCT Drive AS Result
  5. FROM tblFileStructure
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT Drive AS Result
  2. FROM tblFileStructure
I know that using the DISTINCT predicate is not typical in queries generally, but I wanted to include all the work done without skewing the results by displaying time of the various hundreds of thousands of records in the table.

I believe that, as long as the table used is already included in the SQL anyway, the technique of MegaLog's to add a single specific line to your output is actually perfectly workable without any performance delay.
Nov 23 '09 #9
Megalog
378 Expert 256MB
Maybe the SQL isnt optimal in the first place? I just ran a test against a table with a million unique records, using two SQL sets.

I've added a second value (Purple), removed the Distinct parameter except on the actual table lookup, and am using a Union instead of Union All.

Expand|Select|Wrap|Line Numbers
  1. SELECT "Blue" AS [Column1] 
  2. FROM Tbl1 
  3. UNION 
  4. SELECT "Purple" as [Column1] 
  5. FROM Tbl1  
  6. UNION 
  7. SELECT DISTINCT Column1 FROM Tbl1;
Here is the same code posted earlier, with a second value inserted.
If you dont specify DISTINCT for Blue or Purple, then it gets paired up with every value from Tbl1.
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT "Blue" AS [Column1]
  2. FROM Tbl1 
  3. UNION ALL 
  4. SELECT DISTINCT "Purple" AS [Column1] 
  5. FROM Tbl1 
  6. UNION ALL 
  7. SELECT DISTINCT [Column1] FROM Tbl1;
Sample 1 comes out 2 seconds faster than sample 2 in my tests. (13 seconds versus 15 seconds)
Nov 23 '09 #10
Megalog
378 Expert 256MB
Just ran the same test using AJ's method, and it came out to 9 seconds versus the 13 seconds I had earlier. So, it would appear he is correct that in a large dataset there is a more efficient method.

Values inserted using a dummy table named 'Extra':
Expand|Select|Wrap|Line Numbers
  1. SELECT "Blue" FROM Extra 
  2. UNION
  3. SELECT "Purple" FROM Extra
  4. UNION
  5. SELECT DISTINCT Column1 FROM Tbl1;
Nov 23 '09 #11
NeoPa
32,556 Expert Mod 16PB
Maybe my testing wasn't significant then.

I have a couple of comments/questions though :
  1. What is the significance of your first set of SQL where each record is returned for the colours Blue & Purple? I see no relevance to that at all and can't imagine a scenario where it may be required.
  2. Did you close and re-open the database between running your sets of SQL? I know when I tried rerunning mine it executed in a flash.
  3. I guess from what you've posted the small table version must be considered as (at least theoritically) quicker, even if on some occasions (especially when Access optimises the access) this difference is minimal.
Nov 23 '09 #12
Megalog
378 Expert 256MB
@NeoPa
Not sure what you mean.. "Blue" & "Purple" are only returned once, and then all the distinct values from Tbl1 follow. I'm doing the same thing as before, just adding in another value to try to stress test the scenario a bit more.

BTW, I do this in my db's.. I sometimes have 2 or 3 values I add in on a combo box which trigger actions when selected.

Did you close and re-open the database between running your sets of SQL? I know when I tried rerunning mine it executed in a flash.
Yep I did. I'm guessing the difference was noticeable since I was running it through a little over a million records.

I guess from what you've posted the small table version must be considered as (at least theoritically) quicker, even if on some occasions (especially when Access optimises the access) this difference is minimal.
To be honest, I'm still confused as to why there's a difference at all between these two setups. One requires a blank table that no actual values are stored in, and the other requires no table at all.. both values are aliased into the entire join.
Nov 23 '09 #13
NeoPa
32,556 Expert Mod 16PB
@Megalog
I'm referring to the first set of SQL in post #10. This doesn't match your description. This returns a million Blues, a million Purples and a million other records from my reading of it.
@Megalog
I didn't follow the meaning of this. I wasn't aware anything referred to an empty table. I thought we were dealing with two scenarios :
  1. Linking in to a table with a single record.
  2. Linking in to the same table as used in the following part, but using the DISTINCT predicate to ensure only one record returned.
I think we're somehow talking at cross-purposes here, but I can't see where that started.
Nov 23 '09 #14
Megalog
378 Expert 256MB
@NeoPa
It would return a million of each if it were using "UNION ALL". Basically I tried it two ways: One way has SELECT paired with UNION, the second has SELECT DISTINCT paired with UNION ALL. The last SELECT statement (the one pulling the actual values from the main large table) is a DISTINCT in both cases.

I didn't follow the meaning of this. I wasn't aware anything referred to an empty table. I thought we were dealing with two scenarios :
  1. Linking in to a table with a single record.
  2. Linking in to the same table as used in the following part, but using the DISTINCT predicate to ensure only one record returned.
I think we're somehow talking at cross-purposes here, but I can't see where that started.
Look up at AJ's first post, he says: "First, I created a dummy table called DUAL (it's an oracle thing), that has only one column and no values in it."

I think where the confusion started is that I started using different SQL arrangments, and then applied both arrangements to both solutions, coming up with 4 different tests. And i only posted 3 of those tests. =)
Nov 23 '09 #15
NeoPa
32,556 Expert Mod 16PB
@Megalog
Well, I've learned something. I always assumed that would only lose values that were similar but from different record sources. I never realised it had the effect of the DISTINCT predicate within the individual SELECT queries as well.
@Megalog
I misread that sort of. I just reread it and what you say makes perfect sense. I understood it (because of my previous thinking of what was required anyway) to mean it had no value (Blank or Null) in the one field in its one record, rather than no actual records as such (as you interpreted it by the looks of it). I cannot say that my interpretation is/was correct now I read it again, but perhaps it may have been. I wouldn't expect the process to work without a record to provide the data to be passed as the literal, but I will test that in case it does.
@Megalog
I must take some responsibilty for not knowing some things about SQL that someone of my experience should ;)
Nov 23 '09 #16
NeoPa
32,556 Expert Mod 16PB
I think AJ must have meant a record with a Null value rather than no records at all - as an empty table produces no literal value in the output.

Phew. Something I was right about at last. I was forgetting the feeling ;)
Nov 23 '09 #17

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

Similar topics

5
by: Derek | last post by:
Let's say I have a dynamic list/menu, which gets its values from a database table, for example a list of contact people where the value is an ID number and the label is the name. However, if the...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
6
by: 6thirty | last post by:
Hi, I've created a stocktaking database using Access XP. This is indexed by two fields - part number and shelf location. I am currently inputting all the data via a form. When I have entered a...
6
by: Sebi | last post by:
Hello all, I'm thinking about overwriting the ListItem, so it can contain x additional values (not only one). Has anybody ever tried this? Has someone got an example (C#)? Can DropDownList...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
6
by: vb | last post by:
Hi, I am new to .Net. I am using a Combo Box in my windows forms. I am adding the items by creating the instances and adding the same to the list. My questions/doubts are: 1. If I have 25 to...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
5
by: consonanza | last post by:
I am working on a report filter form. It has 2 combo boxes (cmboSelectSubject and cmboSelectCategory) to select criteria. Selecting an entry in combo 1 restricts the options available in combo 2....
2
by: Wayne | last post by:
I have a combo box and a text box on a form. When I choose a value from the combo box, I want it to be added to the textbox. I'm able to do this by using the following code when I click the combo...
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...
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
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
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...
0
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
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...

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.