473,385 Members | 1,673 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.

MS Access accepting column name as parameter

Hello,

I'm pretty new to this, but I'm working on an MS Access DB and I'm trying to create a query that will accept a column name as a parameter. The table is set up to show an employees base schedule and there is a seperate column for each hour of work. If the column has a value of 1, they're scheduled to work... if the value is 0 they aren't. We would like one query that can be used to return who is working for the given hour, but the problem is that the column names are named like h1, h2, h3, etc. and I can't figure out how to accept the column name as a parameter.

Generally I can do something like [@parametername] but that doesn't seem to work for column names. I also tried tablename.[@hour] but no luck there either.

Thanks!

P.S. Not sure if it matters but it's MS Access 2000
Feb 24 '07 #1
4 6756
ADezii
8,834 Expert 8TB
Hello,

I'm pretty new to this, but I'm working on an MS Access DB and I'm trying to create a query that will accept a column name as a parameter. The table is set up to show an employees base schedule and there is a seperate column for each hour of work. If the column has a value of 1, they're scheduled to work... if the value is 0 they aren't. We would like one query that can be used to return who is working for the given hour, but the problem is that the column names are named like h1, h2, h3, etc. and I can't figure out how to accept the column name as a parameter.

Generally I can do something like [@parametername] but that doesn't seem to work for column names. I also tried tablename.[@hour] but no luck there either.

Thanks!

P.S. Not sure if it matters but it's MS Access 2000
__1 Create a Query called qryBaseSchedule which contains all your Fields (Name, h1 to h24, etc.) with no criteria.
__2 Name your Table tblBaseSchedule or adjust code.
__3 Create an Unbound Combo Box on a Form called cboHour and simply place the Hours in its single column (h1, h2, h3, h4, h5, h6, h7...).
__4 In the AfterUpdate Event of the Combo Box, place the following code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboHour_AfterUpdate()
  2.    CurrentDb.QueryDefs("qryBaseSchedule").SQL = "Select * FROM " _
  3.              & "tblBaseSchedule Where [" & Me![cboHour] & "] =" & 1
  4.    DoCmd.OpenQuery "qryBaseSchedule"
  5. End Sub
__5 Now, whenever you select an Hour in the Combo Box, all Records that contain a 1 in that specific Hour Field will be returned.
__6 There may be an easier Method, but it doesn't exactly come to mind right now. Any questions, feel free to ask.
Feb 24 '07 #2
Thanks for the reply. I'm still having some trouble and perhaps it will help if I give more background information.

Basically we have an ASP.Net/C# application that connects to an Access DB. There are about 100 queries currently stored in the Access DB and there are no problems calling and using them. However, there is a table that was poorly structured and the only way I can create a query that will do what I want is to pass a column name as one of the parameters (h1, h2, etc.). Is there any way to do that in Access? I keep reading through the Access help guides and nothing has worked.

I know that I could make 24 seperate queries and call those seperately, but that seems tedius and inefficient. I'm also thinking that I could just imbed the SQL in the application but I guess that's a last resort. Again, I'm pretty new to this and my main goal is to not break anything that's already working.

Thanks!
Feb 25 '07 #3
ADezii
8,834 Expert 8TB
Thanks for the reply. I'm still having some trouble and perhaps it will help if I give more background information.

Basically we have an ASP.Net/C# application that connects to an Access DB. There are about 100 queries currently stored in the Access DB and there are no problems calling and using them. However, there is a table that was poorly structured and the only way I can create a query that will do what I want is to pass a column name as one of the parameters (h1, h2, etc.). Is there any way to do that in Access? I keep reading through the Access help guides and nothing has worked.

I know that I could make 24 seperate queries and call those seperately, but that seems tedius and inefficient. I'm also thinking that I could just imbed the SQL in the application but I guess that's a last resort. Again, I'm pretty new to this and my main goal is to not break anything that's already working.

Thanks!
To the best of my knowledge, there is no way to pass a Column Name as a Parameter in the Query Grid but I could be wrong. The only Method I know is what I previously had Posted (a combination of an SQL Statement and VBA code). Does it necessarily have to be a Stored Query?
Feb 25 '07 #4
NeoPa
32,556 Expert Mod 16PB
If I can't come up with a better solution (which I certainly can't atm) then you can use a technique 'similar' to ADezii's, in that it uses the SQL of an already designed QueryDef but simply modifies it for specific use.
You don't say what you want the query for so I can simply show how to set up the SQL.
Your qryBaseSchedule query should be set up to assume we always want to use [H1].
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. strSQL = CurrentDb.QueryDefs!qryBaseSchedule.SQL
  3. strSQL = Replace(strSQL, "[H1]", "[H" & Me!cboHour & "]")
Feb 26 '07 #5

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

Similar topics

14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
3
by: CSDunn | last post by:
Hello, I have a situation with MS Access 2000 in which I need to display report data in spreadsheet orientation (much like a datasheet view for a form). If you think of the report in terms of what...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
4
by: bhbgroup | last post by:
I have a query on one large table. I only add one condition, i.e. a date (the SQL reads like 'where date > parameterdate'. This query is rather quick if 'parameterdate' is either explicitly...
1
by: Thomas Zimmermann | last post by:
I have a form with a subform in datasheet view. Now, I want to trigger a procedure (P1) each time the user selects an entire column (by clicking in the heading) in the subform. The procedure (P1) I...
1
by: archana | last post by:
Hi all I ma having problem in sqldataadapter. In my sql database i have one table containing column name as 'Name and address'. Here 'and' is reserved word which i am using in column name. In...
2
by: Roger | last post by:
I have a stored procedure running on DB2 V7 Z/os calling a COBOL program to do some inserts. The stored procedure have 3 input columns and one column is of varchar(32648) The stored procedure is...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.