473,480 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create 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 2836
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
ChipR
1,287 Recognized Expert Top Contributor
You never set the rowsource if the table isn't linked.
Mar 19 '09 #11
tuxalot
200 New Member
Thanks Chip. So now we have this IF statement:

Expand|Select|Wrap|Line Numbers
  1.                     If Len(CurrentDb.TableDefs(conLINKED_SR).Connect) > 0 Then
  2.  
  3.                         'OK, we know that conLINKED_SR is a Linked Table, but is the Link valid?
  4.                         'The next line of code will generate Errors 3011 or 3024 if it isn't
  5.                         CurrentDb.TableDefs(conLINKED_SR).RefreshLink
  6.  
  7.                         strRowSource = strRowSource & "UNION SELECT * FROM [tblReportsState]) "
  8.  
  9.                         Me.lstReportName.RowSource = strRowSource
  10.                         Me.lstReportName.Requery
  11.  
  12.                         'MsgBox strRowSource
  13.  
  14.                     Else
  15.  
  16.                         strRowSource = strRowSource & ") "
  17.                         strRowSource = strRowSource & "ORDER BY ReportCaption"
  18.                         Me.lstReportName.RowSource = strRowSource
  19.  
  20.                     End If
  21.  
Table is linked, listbox still empty...
Mar 19 '09 #12
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Tux, you should use the VBE editor to return the rowsource string value (using the messagebox currently commented out in line 12 of the current code version), then copy this into the query editor - switch to SQL view to do so. Check that the SQL actually works when you run the query.

If it does run from the query editor, the query rowsource string itself is OK and something is not right with setting the rowsource for the listbox. If it does not work in the query editor it is not going to fill the listbox either...

-Stewart
Mar 19 '09 #13
tuxalot
200 New Member
works now! I had to remove the parenthesis. Final code:
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 * 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 & "ORDER BY ReportCaption"
  26.                         Me.lstReportName.RowSource = strRowSource
  27.                         Me.lstReportName.Requery
  28.  
  29.                     End If
  30.  
  31. Exit_lstReportName_Click:
  32.                     Exit Sub
  33.  
  34. Err_lstReportName_Click:
  35.                     Select Case Err.Number
  36.  
  37.                         Case 3265        'Linked Table does not exist
  38.                             MsgBox "You are missing a link to your State Reports database. I will open a form to allow you to locate this file", vbCritical, "Missing Database Link"
  39.                             DoCmd.OpenForm "frmChangePathToStateReports"
  40.                             DoCmd.Close acForm, Me.Name
  41.  
  42.                         Case 3011, 3024, 3044        'Table is linked, but Linked Table Path not valid
  43.                             strRowSource = strRowSource & "ORDER BY ReportCaption"
  44.                             Me.lstReportName.RowSource = strRowSource
  45.                             Me.lstReportName.Requery
  46.  
  47.                             'MsgBox "It appears that your State Reports database could not be located.  I will open a form to allow you to update the path to this file", vbCritical, "Link Not Valid"
  48.                             'DoCmd.OpenForm "frmChangePathToStateReports"
  49.                             'DoCmd.Close acForm, Me.Name
  50.  
  51.                         Case Else
  52.  
  53.                             MsgBox Err.Description & Err.Number, vbExclamation, "Error in lstReportName_Click()"
  54.  
  55.                     End Select
  56.  
  57.                     Resume Exit_lstReportName_Click
  58.  
Thanks for the help.

Tux
Mar 19 '09 #14

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

Similar topics

0
1327
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...
3
3540
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 ...
2
2900
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,...
6
2856
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...
6
1548
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...
3
2209
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...
6
3565
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,...
5
3813
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...
2
5060
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,...
0
7055
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
6920
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
7060
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,...
1
6760
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...
1
4799
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...
0
3013
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...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
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.