473,608 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

union query - populating a listbox

tuxalot
200 New Member
I have a listbox with a row source using a union query:
Expand|Select|Wrap|Line Numbers
  1. SELECT [quniReports].ReportID, [quniReports].ReportName, [quniReports].ReportCaption, [quniReports].ReportCriteriaFlags, [quniReports].ReportRemote FROM [quniReports] ORDER BY [quniReports].ReportCaption;
The union query sql:
Expand|Select|Wrap|Line Numbers
  1. TABLE [tblReports] UNION SELECT * FROM [tblReportsState];
  2.  
tblReportsState is a linked table.

Question:

If the db is opened and the linked table is not linked:
Expand|Select|Wrap|Line Numbers
  1.     Const conLINKED_SR As String = "tblReportsState"
  2.  
  3.     If Len(CurrentDb.TableDefs(conLINKED_SR).Connect) = 0 Then
  4.  
how can I then change the listbox row source to only show data in tblReports? I assume I would put the code in the listbox before update event.

Thanks for the look.
Mar 19 '09 #1
13 2856
ChipR
1,287 Recognized Expert Top Contributor
Try something like:

Expand|Select|Wrap|Line Numbers
  1. Dim strRowSource as String
  2.  
  3. strRowSource ="SELECT ReportID, ReportName, ReportCaption, ReportCriteriaFlags, ReportRemote FROM ([tblReports]"
  4.  
  5. If Len(CurrentDb.TableDefs(conLINKED_SR).Connect) > 0 Then
  6.    strRowSource = strRowSource & "UNION SELECT * FROM [tblReportsState]) "
  7. else
  8.    strRowSource = strRowSource & ") "
  9. end if
  10.  
  11. strRowSource = strRowSource & "ORDER BY ReportCaption"
Mar 19 '09 #2
tuxalot
200 New Member
Thanks Chip but it didn't work. I've tried removing my linked db, moving it's path manually and all I get is a blank listbox.
Mar 19 '09 #3
ChipR
1,287 Recognized Expert Top Contributor
Try a
MsgBox strRowSource
right before you set the listbox. Then you can take a look at it to make sure it looks valid in either case. That's the easiest way to reveal a mistake I may have made in concatenating the strings.
Mar 19 '09 #4
ChipR
1,287 Recognized Expert Top Contributor
Forgot to mention that the code would be in the form's open event probably.
Mar 19 '09 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Minor error in line 5 of post 2, Chip - the literal value referring to the name of a table in the tabledefs collection has to be within quotes:

Expand|Select|Wrap|Line Numbers
  1. If Len(CurrentDb.TableDefs("conLINKED_SR").Connect) > 0 Then
I would suggest placing the code in the on-load event of the form, not on open (as the underlying recordset for the form is not available until the form is loaded), then performing a requery of the control after setting its rowsource; the name of the control has not been mentioned, but the line needed is simply

Me![Name of listbox control].Requery

which should be placed after the lines setting the rowsource.

Ah, but what is also missing is the setting of the control's rowsource to the string.

Me![Name of listbox control].RowSource = strRowSource

This must be placed before the requery line in the final code...

Chip's code then becomes

Expand|Select|Wrap|Line Numbers
  1. Dim strRowSource as String 
  2. strRowSource ="SELECT ReportID, ReportName, ReportCaption, ReportCriteriaFlags, ReportRemote FROM [tblReports]" 
  3. If Len(CurrentDb.TableDefs("conLINKED_SR").Connect) > 0 Then 
  4.    strRowSource = strRowSource & " UNION SELECT * FROM [tblReportsState] " 
  5. end if 
  6. strRowSource = strRowSource & " ORDER BY ReportCaption;" 
  7. With Me![Name of listbox control]
  8.   .RowSource = strRowSource
  9.   .Requery
  10. End With
Finally, keep in mind that Union queries will only work if the number of fields and their types are exactly the same in both sets - so your table and the SELECT query must have the same number of fields, of the same type, in the same selection order if the union is to work at all.

-Stewart
Mar 19 '09 #6
tuxalot
200 New Member
here's the generated sql string:
Expand|Select|Wrap|Line Numbers
  1. SELECT ReportID, ReportName, ReportCaption, ReportCriteriaFlags, ReportRemote FROM ([tblReports]UNION SELECT * FROM [tblReportsState])
Which errors with a 'syntax error in join operation' at the word UNION.
Mar 19 '09 #7
tuxalot
200 New Member
Thanks Stewart, I have the code in the tab control like this:
Expand|Select|Wrap|Line Numbers
  1.                 If TabCtl2.Value = 7 Then
  2.  
  3.                     On Error GoTo Err_lstReportName_Click
  4.  
  5.                     Const conLINKED_SR As String = "tblReportsState"        ' linked State Reports table
  6.                     Dim strRowSource As String
  7.  
  8.                     strRowSource = "SELECT ReportID, ReportName, ReportCaption, ReportCriteriaFlags, ReportRemote FROM ([tblReports]"
  9.  
  10.                     If Len(CurrentDb.TableDefs(conLINKED_SR).Connect) > 0 Then
  11.  
  12.                         'OK, we know that conLINKED_SR is a Linked Table, but is the Link valid?
  13.                         'The next line of code will generate Errors 3011 or 3024 if it isn't
  14.                         CurrentDb.TableDefs(conLINKED_SR).RefreshLink
  15.  
  16.                         strRowSource = strRowSource & "UNION SELECT * FROM [tblReportsState]) "
  17.  
  18.                         Me.lstReportName.RowSource = strRowSource
  19.                         Me.lstReportName.Requery
  20.  
  21.                         MsgBox strRowSource
  22.  
  23.                     Else
  24.  
  25.                         strRowSource = strRowSource & ") "
  26.                         strRowSource = strRowSource & "ORDER BY ReportCaption"
  27.  
  28.                     End If
  29.  
  30.  
Mar 19 '09 #8
ChipR
1,287 Recognized Expert Top Contributor
Yep. In post #2, line 3 should have ended with:

FROM ([tblReports] "

I forgot the space at the end, so the resulting string got mashed.
Mar 19 '09 #9
tuxalot
200 New Member
Post #8 is my current code placed in the tab control. I added the space at the end so now it reads:
Expand|Select|Wrap|Line Numbers
  1. strRowSource = "SELECT ReportID, ReportName, ReportCaption, ReportCriteriaFlags, ReportRemote FROM ([tblReports] "
Still no go.
Mar 19 '09 #10

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

Similar topics

0
1339
by: Bill Brinkworth | last post by:
I want the user to type in part of a word, and I want to return all words from an Access word table that contains specified letters. If they were to type "fe??", it should return all words in the dictionary that are 4 letters and begin with "fe" into a listbox. I am having difficulting searching for all words and taking all those words and populating a listbox. Can you help? Here's what I have: CLASS - Public Property word() As String...
3
3554
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID from letdata UNION SELECT as ID FROM MEMODATA; I get an ODBC error. The same query runs when the backend files are MDB files and it runs with MYSQL if I only combine 2 tables.
2
2909
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However, currently the code is such that with each selection there is a postback. We want to avoid it using filter and javascript. I am not using ADO.NET but adodbc and no datagrids or datasets (please don't tell me that i should as my boss clearly doesn't want...
6
2866
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset a denormalized mirror of the database, but I'm not having much luck getting the selection logic down (I haven't found a 'hook' where I can access the listbox object as an object to set the listitem's selected property before it gets rendered).. ...
6
1560
by: P K | last post by:
I have a listbox which I am populating on the client (it contains a list of dates selected from calender). The listbox is a server control. When I get to the server after postback by selecting an item in the list, the list box cannot be accessed. How do I repopulate the listbox on the server after postback and be able to access the elements?
3
2215
by: mikes | last post by:
I have 2 separate queries, which effectively are the same except they draw data from separate tables. Both tables are (design-wise) identical, only the data is different. for each query, there are 2 tables with a standard LEFT JOIN. One field of the query is calculated, looking for a NULL in one table, and then using a field from the second table in that case. One query looks like this: PARAMETERS Text ( 255 ); SELECT...
6
3578
by: halberd4 | last post by:
Hello. I have an Access2003 db that I need to create a 'combined' query for. This query will then become the source for a listbox. The query needs to combine Query1 (example) SELECT Aa, Bb, Cc, Dd FROM Table1 WHERE Cc=3 ORDER BY Aa, Bb, Dd with Query2 (example) SELECT Aa, Bb, Cc, Dd FROM Table1 WHERE Cc<>3 ORDER BY Cc, Aa, Bb, Dd and produce a combined output similar to this (minus header) Aa Bb Cc Dd
5
3832
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
2
5073
by: mcalex | last post by:
Hi, I'm having trouble trying to populate a list box with data from a parameterized query. If I set the rowsource property to the query name, when the form opens I get the parameter values dialog, but I want to pass the params from code. If I create a querydef, I can pass the parameter in, but I don't know how to get the records into the listbox. I have tried lstNames = qdfNames.sql lstNames.recordset = qdfNames.OpenRecordset; ...
0
8002
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
8496
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...
1
8148
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6816
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
6013
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
3962
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...
0
4024
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
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 we have to send another system
0
1329
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.