Combing Text with Ex. Field Name to Create A New Field Name....I Think?

J

J. Trucking

Hello,

I have a query that will return data from a table, and another query.
Here is my question. This is an existing fuel database. It runs on a
keylock system so there are keys numbered A1 to C10. The table returns
a sum of fuel based on a key (for example A1). I have a set of fields
from another existing query labelled DIFFA1, DIFFA2, etc. What I
would like to do is return the value for "DIFFA1" when I am returning
the information from Key A1. I would like to set up an expression
something to the effect of:

Expr1: "DIFF" & [Key]

I did this and it returns the word "DIFFA1" when Key A1 is the key in
question. I would like to return the Value of DIFFA1, not the word.
I wouldn't normally have set the tables up this way and will definitly
look at changing this in the future but is there a way to combine the
text with the existing field name to call on another field. Hopefully
I've explained this clearly. If not let me know

Thanks in advance,

John
 
J

Jeff Boyce

Trying to embed data in your fieldnames sounds like how you would have to do
this ... if you were using a spreadsheet!

Post the underlying structure of your data first -- everything starts with
the data in Access.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
J

J. Trucking

Hey Jeff,

The first Table is titled tblDieselConsumption
The three fields used from this table are "Operator" (text),
"Key" (text), and "Diesel" (number).
I also have a user input a date on a combo box in a form so that (if
they want), they can only show the data from a certain range. The
output will give the operator, which key he used, and the total amount
(sum) of diesel for the given time period.
It looks like this

Operator: John
Key: A1
Diesel (in Litres): 425

The existing query (qryDieselCOmparison) is a query which calculates
the amount of diesel used this month from the recording system inside
the pump. We use this as a comparison to make sure the employees fuel
charges and the actual charges are the same. It has a whole bunch of
expressions that return the calculated value of diesel for the given
time period (based on the same form and user input). It is displayed
as such:

DIFFA1: 430
DIFFA2: 409
etc.

So you can see, John used card A1 and recorded 435 litres for a given
time period. However, the recording device in the machine said John
took 430 Litres. Unfortunetly, I cant just store the data (DIFFA1)
from the pump. It is calculated.

So what I want to do in my summary query from the table, is simply put
the DIFFA1 result beside it. Like I said, I can get the output:

Operator: John
Key: A1
Diesel: 425
DIFFA1: DIFFA1

So it will show the word "DIFFA1" but not the value. BTW, I'm totally
trying to accomplish this as if I were in Excel :).

Thanks,

John
 
B

Bob Quintal

Hello,

I have a query that will return data from a table, and another
query. Here is my question. This is an existing fuel database.
It runs on a keylock system so there are keys numbered A1 to C10.
The table returns a sum of fuel based on a key (for example A1).
I have a set of fields from another existing query labelled
DIFFA1, DIFFA2, etc. What I would like to do is return the value
for "DIFFA1" when I am returning the information from Key A1. I
would like to set up an expression something to the effect of:

Expr1: "DIFF" & [Key]

I did this and it returns the word "DIFFA1" when Key A1 is the key
in question. I would like to return the Value of DIFFA1, not the
word. I wouldn't normally have set the tables up this way and will
definitly look at changing this in the future but is there a way
to combine the text with the existing field name to call on
another field. Hopefully I've explained this clearly. If not let
me know

Thanks in advance,

John
You would need to write some visual basic code to do this, the way
you want. An alternative method would be to write a small query that
contains one of the diff fields and a calculated field containing
the word DIFFn where n is the KeyNumber, plus any othre fields, such
as the yearMonth for the data.

Then you open the query in SQL mode, delete the semicolon (SQL
terminator) copy the SQL to the clipboard.Add the word UNION to the
line below the SQL and paste from the clipboard. Edit the DIFF
number and repeat until you have all the key numbers.

The SQL would look like this.
SELECT YearMonth, "DIFF1" as keyNumber, DIFF1 as KeyValue from
QueryName
UNION
SELECT YearMonth, "DIFF2" as keyNumber, DIFF2 as KeyValue from
QueryName
UNION
SELECT YearMonth, "DIFF3" as keyNumber, DIFF3 as KeyValue from
QueryName

Add a semicolon to terminate the SQL, save and use.

The query would look like this
200706,DIFF1,1234
200706,DIFF2,1410
200706,DIFF3,7654
200707,DIFF1,3333
200707,DIFF2,2292
200707,DIFF3,6674





..

SELECT
 
J

J. Trucking

Hi Bob,

Thanks for the response. I entered in exactly what you said (in a new
query) and I get the message "Invalid SQL statement. Expected
'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. I have
checked everything and cant find what I am missing. Please let me
know what you think.

Thanks for the help,

John
 
J

J. Trucking

Hi Bob,

You also mentioned that VBA might accomplish waht I am looking for.
Would it be pretty involved or would the code be pretty short.

Thanks

John
 
J

Jeff Boyce

John

see comments in-line below...

J. Trucking said:
Hey Jeff,

The first Table is titled tblDieselConsumption
The three fields used from this table are "Operator" (text),
"Key" (text), and "Diesel" (number).
I also have a user input a date on a combo box in a form so that (if
they want), they can only show the data from a certain range. The
output will give the operator, which key he used, and the total amount
(sum) of diesel for the given time period.

So, the user enters a date on a form... but how does your table know that
date? Again, it all starts with the data. I suspect that you actually have
more than the three fields in this table!
It looks like this

Operator: John
Key: A1
Diesel (in Litres): 425

The existing query (qryDieselCOmparison) is a query which calculates
the amount of diesel used this month from the recording system inside
the pump. We use this as a comparison to make sure the employees fuel
charges and the actual charges are the same. It has a whole bunch of
expressions that return the calculated value of diesel for the given
time period (based on the same form and user input). It is displayed
as such:

DIFFA1: 430
DIFFA2: 409
etc.

So you can see, John used card A1 and recorded 435 litres for a given

DIFFA1 and DIFFA2 doesn't tell me that John used card A1.

Neither of these numbers is 435 -- where did that come from?
time period. However, the recording device in the machine said John
took 430 Litres. Unfortunetly, I cant just store the data (DIFFA1)
from the pump. It is calculated.

So what I want to do in my summary query from the table, is simply put
the DIFFA1 result beside it. Like I said, I can get the output:

Operator: John
Key: A1
Diesel: 425
DIFFA1: DIFFA1

So it will show the word "DIFFA1" but not the value. BTW, I'm totally
trying to accomplish this as if I were in Excel :).

I'm sorry! Access is a relational database, and you will not get the best
use of its features and functions if you feed it 'sheet data. The learning
curve on Access can be a bit steep. Is there a reason you are NOT doing
this in Excel, if that's where your experience is?

One of your options could be to use Access to store data, a query to extract
the underlying raw data for your time period, then export the data to Excel
for further analysis.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
J

J. Trucking

Hi Jeff,

I'll try and explain the whole story. The database is for fuel
records for a fleet of trucks. The fuel system is a "keylock" system
so when an operator goes to the pump, they put a key in a certain hole
(there are 30 keys and theirty holes), turn the key, and fuel up their
machine. The key is a security feature to prevent fuel theft. The
key is linked to a meter inside the pump which will run a total as to
how much fuel that key pumps. After each fueling, the operator will
go into the office and write down how much fuel he took, what key he
has, his name, and the date. For example, John Doe fueled up on
August 1, 2007 with key A1 and took 400 Litres of fuel.

The meters that total the amount used by each key are behind a locked
door on the pump itself. These are aggregate totals menaing they're
alway rising and never reset. For example, for me to know that John
pumped 400 Litres of fuel, I would have to know the meter reading
before and after the fueling that day. It, for example, would read
1200 litres before he fueled up, and 1600 litres after.

So here's how the operation works. The operators write down everytime
they fuel up. At the end of the month, we take this sheet and enter
it in the computer. We also go out (at the end of the month) and
record the meter readings from behind the locked pump door.

We currently do this in excel but we are trying to track an abundance
of other information (maintenance, production, parts, etc.). I have
only recently come to this business and found that this information
was stored (poorly) and was all over the server. I am trying to
consolidate it into a user friendly database rather than have a whole
bunch of spreadsheets. I personally like excel over access, but this
database needs to be user friendly because I wont do the data entry
(for some reason I've found that data entry personel seem to favour
the access interface over excel). As well, I like the benefit of "one
click" reports for the managers.

So back to the fuel part. The reason why we record the data from each
fill up and check it against the pump meters is that we have found
that fuel seems to go "missing". We use this check to ensure that
operators are taking an acceptable amount of fuel and that they are
writing down each time they take the fuel.

I store this information in two tables. The first is called
'tblDieselConsumption" and has the fields: IDDiesel (AutoNumber),
DateofPump (Date), UnitNumber (Text), Operator (text), Litres
(number), and Key (text). This information is stored for each record.

I have another table which stores the 30 meter readings every month.
It is called tblDieselKeylock. The fields are: IDKeylock
(AutoNumber), MonthNo (Text), YearNo (Text), and A1...C30
(Number...stores the numberic reading off of each meter on the pump -
A1-A10, B1-B10, C1-C10).

I only need to see the data at the end of each month. I guess this
could also be true for any previous month. Therefore, I created a
small form and am planning to use it to reference a query. The form
has two combo boxes: MonthToSearch, and YearToSearch. The form is
called frmDieselConsumptionSummary. Any user will say to themselves
"I want to see the fuel useage from last July". They will select
"July" and "2006" and hit OK. A report will be generated, for all the
keys, stating (for example):

Key: A1
Operator: John Doe
Total Diesel recorded By Operator for Month: 400 Litres
Total Diesel Recorded By Pump for Month: 410 Litres

This way I would know that there is a 10 Litre difference. Sometimes
there is a zero litre difference and other times there can be upwards
of a 400 litre difference so its important info to know.

I am trying to calculate everything through queries. I have three of
them. The first is qryDieselSummaryPrevious. The query is based on
tblDieselKeylock. I use this query to look at the Keylock Pump Meters
and find the values that the keylock had the month prior to the one
that the user is interested in (because I'm eventually gonna need to
do some subtraction). I accomplish this by using the combo boxes on
frmDieselConsumptionSummary and subtracting one from the month that
the user is interested in (and one from the year in the case of
January). So in this query, the field I use are MonthNo (with the
condition that the only omnth pulled equals that on the user input
form), YearNo (same condition as the month), and A1-C30. I then use
the query to pull that particular month's record and call each one
"PRE" through the use of an expression (ie) A1PRE, A2PRE, etc.

I have a second query called qryDieselSummaryDifference which is based
on tblDieselKeyLock and qryDieselSummaryPrevious. I have the fields
MonthNo and YearNo (with the same input conditions as the last query -
except the "minus one" condition for getting the previous months
data). I then have expressions for each key that find the difference
between the currents months pump reading and the previous months
reading. The expressions look like this: DIFFA1: [A1]-[A1PRE] where
A1 comes from tblDieselKeyLock and [A1PRE[ is from the
qryDieselSummaryPrevious. This all works out perfect.

By doing all of that, I now know the pump meter readings for any month
that I want. What I tried to do know is compare that to a monthly
summary of the individual records located in tblDieselConsumption. I
did this through another query (or so I thought). I made a query
called qryDieselSummaryOverall. I based it on tblDieselConsumption
and qryDieselSummaryDifference. I pulled the fields: DateofPump,
Operator, Key, and Liters from tblDieselConsumption. I worked the
DateofPump so that I only pulled the records based on the user input
from frmDieselConsumptionSummary. I thought I could link it to the
records from qryDieselSummaryDifference to get the corresponding
"DIFF.." record. What I did is listed above but basically I am trying
to call the "DIFF" record based on the Key showing in the Key field.

Like I said, I'm stuck. I understand that this is probably confusing
reading this but I'm not sure how else to store the data and make it
user friendly. I'd like to avoid using excel in this case just
because of the computer experience from the data entry person. I
tried the union query above but it gives me the error message that I
stated above. I thought about VBA and searched this forum on it but I
dont really know what to write in order to call a specific function.
Any help would be greatly appreciated at this point.

Thanks,

John
 
B

Bob Quintal

Hi Bob,

You also mentioned that VBA might accomplish waht I am looking for.
Would it be pretty involved or would the code be pretty short.

Thanks

John
it would be involved.
 
B

Bob Quintal

Hi Bob,

Thanks for the response. I entered in exactly what you said (in a new
query) and I get the message "Invalid SQL statement. Expected
'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. I have
checked everything and cant find what I am missing. Please let me
know what you think.

Thanks for the help,

John
The code does have SELECT statements in it. I set up the names of the
fields based on what you implied were the field names, and I used
queryname, becaue you never gave the real name of your query.

Other than that, I can't tell from here.
 
J

Jeff Boyce

John

A bit more than a simple volunteer can digest...

It sounds like someone, somewhere, needs to enter both the amount written
down by the driver for a given "Key", on a given date, plus the total amount
on the "pump" for each key, once a month.

You can do a query that totals the "key"'s amounts for, say, the month of
August.

You can do a query that "gets" the pump amounts, per key, say, for the month
of August.

Since both will have the same key, you can create a third query that joins
the two, on the "key" field, and calculates the difference.

Does that get you closer?

Regards

Jeff Boyce
Microsoft Office/Access MVP

J. Trucking said:
Hi Jeff,

I'll try and explain the whole story. The database is for fuel
records for a fleet of trucks. The fuel system is a "keylock" system
so when an operator goes to the pump, they put a key in a certain hole
(there are 30 keys and theirty holes), turn the key, and fuel up their
machine. The key is a security feature to prevent fuel theft. The
key is linked to a meter inside the pump which will run a total as to
how much fuel that key pumps. After each fueling, the operator will
go into the office and write down how much fuel he took, what key he
has, his name, and the date. For example, John Doe fueled up on
August 1, 2007 with key A1 and took 400 Litres of fuel.

The meters that total the amount used by each key are behind a locked
door on the pump itself. These are aggregate totals menaing they're
alway rising and never reset. For example, for me to know that John
pumped 400 Litres of fuel, I would have to know the meter reading
before and after the fueling that day. It, for example, would read
1200 litres before he fueled up, and 1600 litres after.

So here's how the operation works. The operators write down everytime
they fuel up. At the end of the month, we take this sheet and enter
it in the computer. We also go out (at the end of the month) and
record the meter readings from behind the locked pump door.

We currently do this in excel but we are trying to track an abundance
of other information (maintenance, production, parts, etc.). I have
only recently come to this business and found that this information
was stored (poorly) and was all over the server. I am trying to
consolidate it into a user friendly database rather than have a whole
bunch of spreadsheets. I personally like excel over access, but this
database needs to be user friendly because I wont do the data entry
(for some reason I've found that data entry personel seem to favour
the access interface over excel). As well, I like the benefit of "one
click" reports for the managers.

So back to the fuel part. The reason why we record the data from each
fill up and check it against the pump meters is that we have found
that fuel seems to go "missing". We use this check to ensure that
operators are taking an acceptable amount of fuel and that they are
writing down each time they take the fuel.

I store this information in two tables. The first is called
'tblDieselConsumption" and has the fields: IDDiesel (AutoNumber),
DateofPump (Date), UnitNumber (Text), Operator (text), Litres
(number), and Key (text). This information is stored for each record.

I have another table which stores the 30 meter readings every month.
It is called tblDieselKeylock. The fields are: IDKeylock
(AutoNumber), MonthNo (Text), YearNo (Text), and A1...C30
(Number...stores the numberic reading off of each meter on the pump -
A1-A10, B1-B10, C1-C10).

I only need to see the data at the end of each month. I guess this
could also be true for any previous month. Therefore, I created a
small form and am planning to use it to reference a query. The form
has two combo boxes: MonthToSearch, and YearToSearch. The form is
called frmDieselConsumptionSummary. Any user will say to themselves
"I want to see the fuel useage from last July". They will select
"July" and "2006" and hit OK. A report will be generated, for all the
keys, stating (for example):

Key: A1
Operator: John Doe
Total Diesel recorded By Operator for Month: 400 Litres
Total Diesel Recorded By Pump for Month: 410 Litres

This way I would know that there is a 10 Litre difference. Sometimes
there is a zero litre difference and other times there can be upwards
of a 400 litre difference so its important info to know.

I am trying to calculate everything through queries. I have three of
them. The first is qryDieselSummaryPrevious. The query is based on
tblDieselKeylock. I use this query to look at the Keylock Pump Meters
and find the values that the keylock had the month prior to the one
that the user is interested in (because I'm eventually gonna need to
do some subtraction). I accomplish this by using the combo boxes on
frmDieselConsumptionSummary and subtracting one from the month that
the user is interested in (and one from the year in the case of
January). So in this query, the field I use are MonthNo (with the
condition that the only omnth pulled equals that on the user input
form), YearNo (same condition as the month), and A1-C30. I then use
the query to pull that particular month's record and call each one
"PRE" through the use of an expression (ie) A1PRE, A2PRE, etc.

I have a second query called qryDieselSummaryDifference which is based
on tblDieselKeyLock and qryDieselSummaryPrevious. I have the fields
MonthNo and YearNo (with the same input conditions as the last query -
except the "minus one" condition for getting the previous months
data). I then have expressions for each key that find the difference
between the currents months pump reading and the previous months
reading. The expressions look like this: DIFFA1: [A1]-[A1PRE] where
A1 comes from tblDieselKeyLock and [A1PRE[ is from the
qryDieselSummaryPrevious. This all works out perfect.

By doing all of that, I now know the pump meter readings for any month
that I want. What I tried to do know is compare that to a monthly
summary of the individual records located in tblDieselConsumption. I
did this through another query (or so I thought). I made a query
called qryDieselSummaryOverall. I based it on tblDieselConsumption
and qryDieselSummaryDifference. I pulled the fields: DateofPump,
Operator, Key, and Liters from tblDieselConsumption. I worked the
DateofPump so that I only pulled the records based on the user input
from frmDieselConsumptionSummary. I thought I could link it to the
records from qryDieselSummaryDifference to get the corresponding
"DIFF.." record. What I did is listed above but basically I am trying
to call the "DIFF" record based on the Key showing in the Key field.

Like I said, I'm stuck. I understand that this is probably confusing
reading this but I'm not sure how else to store the data and make it
user friendly. I'd like to avoid using excel in this case just
because of the computer experience from the data entry person. I
tried the union query above but it gives me the error message that I
stated above. I thought about VBA and searched this forum on it but I
dont really know what to write in order to call a specific function.
Any help would be greatly appreciated at this point.

Thanks,

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top