473,770 Members | 5,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a query, where the table could be selected by a condition. Need assistance.

176 New Member
Hi all.

I'm writing a report that utilizes a query called qryRecords
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblRecordsORG1;
however, there is tblRecordsORG1, tblRecordsORG2, etc. qryRecords should be based on tblRecordsORG?. My attempt is:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT IIf(Form!MyForm!MyControl=1,[tblRecordsORG1].[Column1],[tblRecordsORG2].[Column1]) AS Column1, IIf(Form!MyForm!MyControl=1,[tblRecordsORG1].[Column2],[tblRecordsORG2].[Column2]) AS Column2, IIf(Form!MyForm!MyControl=1,[tblRecordsORG1].[Column3],[tblRecordsORG2].[Column3]) AS Column3....
  2. FROM tblRecordsORG1, tblRecordsORG2;
this is not enough, because number of columns could be changed in a tblRecordsORG? type of table, and the number of tblRecordsORG? type tables could be changed also. That's why I need to write a query that is something like:

SELECT * FROM -Name of the control value-

but it should be a query, not an SQL string:
Expand|Select|Wrap|Line Numbers
  1. sqlStr = "SELECT * FROM " & Forms!MyForm!MyControl
because It should be used in several different places, and to function as a base to other, more specific quieries.

Appreciate any help.
Regards.
Jan 14 '08 #1
3 1504
patjones
931 Recognized Expert Contributor
Hi all.

I'm writing a report that utilizes a query called qryRecords
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblRecordsORG1;
however, there is tblRecordsORG1, tblRecordsORG2, etc. qryRecords should be based on tblRecordsORG?. My attempt is:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT IIf(Form!MyForm!MyControl=1,[tblRecordsORG1].[Column1],[tblRecordsORG2].[Column1]) AS Column1, IIf(Form!MyForm!MyControl=1,[tblRecordsORG1].[Column2],[tblRecordsORG2].[Column2]) AS Column2, IIf(Form!MyForm!MyControl=1,[tblRecordsORG1].[Column3],[tblRecordsORG2].[Column3]) AS Column3....
  2. FROM tblRecordsORG1, tblRecordsORG2;
this is not enough, because number of columns could be changed in a tblRecordsORG? type of table, and the number of tblRecordsORG? type tables could be changed also. That's why I need to write a query that is something like:

SELECT * FROM -Name of the control value-

but it should be a query, not an SQL string:
Expand|Select|Wrap|Line Numbers
  1. sqlStr = "SELECT * FROM " & Forms!MyForm!MyControl
because It should be used in several different places, and to function as a base to other, more specific quieries.

Appreciate any help.
Regards.

Why don't you just write a SQL query for each case you have to deal wtih, and then pick out the proper one using a Case structure?

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL1, strSQL2, .... , strSQLn As String
  2.  
  3. Dim rst As Recordset
  4.  
  5. strSQL1 = "SQL query for condition 1"
  6. strSQL2 = "SQL query for condition 2"
  7. . . .
  8. strSQLn = "SQL query for condition n"
  9.  
  10. Select Case MyControl
  11.  
  12.      Case 1
  13.  
  14.          Set rst = Currentdb.OpenRecordset(strSQL1)
  15.  
  16.      Case 2
  17.  
  18.          Set rst = Currentdb.OpenRecordset(strSQL2)
  19.  
  20.      . . .
  21.  
  22.      Case n
  23.  
  24.          Set rst = Currentdb.OpenRecordset(strSQLn)
  25.  
  26. End Select
  27.  
This approach might seem a little bit laborious, but it's a whole lot clearer than writing one huge SQL query that's all mucked up with "IIf's", and it'll be easier to debug than the huge SQL query. Likewise, if the number of conditions and/or columns changes, you just have to add or delete branches from the Case structure accordingly...

Pat
Jan 14 '08 #2
Michael R
176 New Member
Thanks Pat.

The thing is I need the query as a base for other queries, and I can't do it using VBA recordset.

Also, there could be many tblRecordsORG? Type tables and their columns number could change. I wanted one solution only so that I won't have to fix it each time more tables or more columns have been added.
Jan 14 '08 #3
patjones
931 Recognized Expert Contributor
Thanks Pat.

The thing is I need the query as a base for other queries, and I can't do it using VBA recordset.

Also, there could be many tblRecordsORG? Type tables and their columns number could change. I wanted one solution only so that I won't have to fix it each time more tables or more columns have been added.
What exactly do you mean by "base for other queries"?

It seems like even if you go the route of writing one big SQL query with a bunch of IIf statements, you'll still need to modify that query every time the number of tables/columns changes...

Pat
Jan 16 '08 #4

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

Similar topics

4
2976
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and attempting to perform a "left join" query to build a cross-reference table. The left join query is currently taking nearly 2 hours for MySQL to process, using Navicat as a front-end. My system specs are 1.4Mhz Pentium Processor with 1GB of RAM...
5
1553
by: Bill | last post by:
Good Day; I would appreciate assistance developing a query that I haven't been able to develop without using a second table. I wish to count the number of records that are still open on the first of each month. Each record has an open date and a close date or the close date is null i.e., the record is not yet closed. I've previously beaten this by building a table, simply a list of the dates for the first of each month for the next...
2
5283
by: phpuser32423 | last post by:
Hi everyone Is it by any chance possible to use mysql and php to auto create the content for drop-down lists on forms by retrieving the values from a database? The reason i ask is that on a site i am making i am asking users to select from list (e.g. nationality) however i would like to avoid typing out every possible value of which there are about 40 (which change). I would much prefer to keep a seperate table in my database for the...
2
18700
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the first query returns a number of data items, consisting, among other fields, of a Company_ID and a Rating_Date. For most companies, the latest value of Rating_Date is equal to a value in a seperate table (tblVariables) which logs the last time a...
3
2097
by: Serious_Practitioner | last post by:
Good day, and thank you in advance for any assistance you can provide. I have a table in an Access 2000 .mdb file, and I've run into something odd and insolvable, at least for me. The database is for membership information. This particular table, called tblMembershipInfo, has fields/columns as follows - fldMemNum - Member Number fldActionDate - The date when the entry was made in that row - Data type is Date/Time, no format specified
6
6306
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate Supervisor table, and join the Supervisor table to the Employee table and a copy of the Supervisor table to create the self-join. I can't figure out how to do this from reading Viescas or from researching it here. I can start a query and create 2 copies...
5
3616
by: kumar_rangan1976 | last post by:
I need the below sybase code to be migrated in UDB : select distinct c.partnumber as I_PART, case when d.IntegratorID = 'DCX05' then 'U' when d.IntegratorID = 'DCX04' then 'M' when d.IntegratorID = 'DCX03' then 'E' else ' ' end as 'C_LU_SRCE_PCHSNG', o.SupplierCode as I_SUPLR_LOC,
4
4017
by: jason.teen | last post by:
Hi, I am trying to create a table of Query "Metrics" ie. I have many tables in a database which most of the time just counts the number of rows in a certain table, or the number of distinct rows in a table with some condition. I'm trying to use the MS Access built in report feature to create one master "metrics report" where it will just have sample output such as:
45
2989
by: ZaphodBBB | last post by:
Windows XP Pro , Access2003. Hi I have a form called frmCheckSheet and a table called tblCheckSheet. On the form there is a combobox cboSerialID which is multicolumn and when a serial number is selected the afterupdate event loads various other text boxes with customer name, department, type of equipment etc. One of the Text boxes on the form is txtAssessment, and it holds an assessment of the item checked...it is linked to a memo field in...
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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
10225
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
10001
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
8880
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...
0
6676
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
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
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
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

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.