473,382 Members | 1,357 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,382 software developers and data experts.

Query values without specifying field names?

Odd problem. I have a table in the following format:

DocID Question1 Question2 Question3
------------------------------------------------
298 1, 2, 3 or 0

Each Question field will have 1,2,3 or 0 if not completed. The number
of Question fields will vary.

I need to count all the questions, then figure out how many have 0
answers. Is there any way to query values dynamically when the field
names change. I'm trying to avoid writing multiple queries, each with
hundreds of field names.

Thanks for any help provided...I'm probably out in left field.

Kat

Nov 13 '05 #1
3 1709
I'm not sure if I understand your question or not.
Do you have 1 table, or many tables?
If there's just the one table, you should know how many fields it
has....

Nov 13 '05 #2
Kathy Burke,
This is a lot easier if you have something like:

Doc_tbl
---->Question_tbl

so that question table is the many side of a one-to-many relationship with
the Doc_tbl. I'd be tempted to quietly write some code that iterated
through the fields looking for zeros and building a temporary table listing
the questions with zeros for a given doc_tbl record.

--
Alan Webb
kn*******@SPAMhotmail.com
"It's not IT, it's IS"

<ka**********@comcast.net> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Odd problem. I have a table in the following format:

DocID Question1 Question2 Question3
------------------------------------------------
298 1, 2, 3 or 0

Each Question field will have 1,2,3 or 0 if not completed. The number
of Question fields will vary.

I need to count all the questions, then figure out how many have 0
answers. Is there any way to query values dynamically when the field
names change. I'm trying to avoid writing multiple queries, each with
hundreds of field names.

Thanks for any help provided...I'm probably out in left field.

Kat

Nov 13 '05 #3
Fasten your seat belts, this is gonna get ugly.

As I see it, the only way to get your data to be queryable is to
normalize your table structure. What you really need is your data to
be in a format like this:

DocID, QuestionID, Answer

Without writing zillions of individual queries, this is a pain. So
write some code to do it for you. (If you can't understand code, this
*might* be a problem, but it's either use code or do it manually...)
So, here's what I came up with:

'---BEGIN CODE-----

Option Compare Database
Option Explicit

Public Sub NormalizeTable(ByVal strSrcTable As String)
'--Sample Call: NormalizeTable "MyTable"
'--ASSUMES you have a table called "tblSurvey", with 3 fields:
'--DocID - Long Integer, primary key(1)
'--QuestionNo - Long Integer, primary key (2)
'--Answer - Long Integer, the number you were storing in the Q(n)
field.

Dim dbs As DAO.Database
Dim tdfSrc As DAO.TableDef
Dim intCounter As Integer
Dim strSQL As String
Set dbs = DBEngine(0)(0)

'--point at the table containing your data
Set tdfSrc = dbs.TableDefs(strSrcTable)

'--loop through the fields in the table, appending the SurveyID
(docNo), QuestionID, and Answer to the Survey table

For intCounter = 1 To tdfSrc.Fields.Count - 1
'Create the SQL statement
strSQL = "INSERT INTO tblSurvey (DocID, Answer, QuestionNo)
SELECT [" & tdfSrc.Name & "].[" & tdfSrc.Fields(0).Name & "], [" &
tdfSrc.Fields(intCounter).Name & "], " & intCounter & " AS Expr1 FROM
tblQs;"
'Execute the SQL statement
dbs.Execute strSQL, dbFailOnError
Next intCounter

Set tdfSrc = Nothing
Set dbs = Nothing

End Sub

'---CODE END----

You have to create the tblSurvey table for this to work. Sorry, didn't
feel like messing with making it properly generic - just wanted to get
it working first!

HTH,
Pieter

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
9
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.