Query Expression Error

M

Midland

I have a query drawing on the tables GenoaMachine and Datalink, and this
expression . . .

QSetup:
DLookUp("[DataLink]![RateID]","[DataLink]","[Datalink]![RateID]=[GenoaMachine]![MRateID]")

Gives the response "Access can't find the name 'GenoaMachine!MRateID' you
entered."

The tables GenoaMachine and DataLink are linked to the query and the field
names RateID and MRateID are correct. What might be the the error? Is it a
logical flaw or a typo?
 
J

Jeff Boyce

When Access "can't find the name", it is typically due to a spelling error.
Recheck spelling.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
M

Midland

The answer to my question appears to be "in a query, the criteria for Dlookup
cannot directly refer to any field outside the query. This seems wrong, as it
seems to defeat the purpose of the function, but it is all I can pull out of
the language in the explanations on the web. I see examples that appear to
get past this by using the criteria format . . .

dlookup("[field1]","
","[field2]=" & [table2]![field3])

.. . . but without any explanation as to why this odd combination of
characters would accomplish this. Can someone refer me to a more detailed
explanation of this procedure?

Thanks,
 
G

George Hepworth

Hello, Are you asking for an exegesis of this sample you posted? To be
clearn, this is NOT an "odd combination of characters"; quite to the
contrary it is a very highly structured expression, with elements clearly
prescribed for them in the standard syntax for expressions of this type.

So, if you really want an explanation, here's how I would explain it.

The DLookup Function is one of the Domain Aggregate Functions (DMax and
DSum being two other examples).

The purpose of the domain aggregate functions is, essentially, to retrieve
values from a specified recordset. They do so by selecting value(s)
according to the parameters passed to the function, if any. Note the "if
any" part. Depending on the circumstances, you can omit criteria parameters
when it is appropriate to do so, or you may employ them. You apply criteria,
as you do in any other circumstance, in order to limit the values returned
by the function to those which are relevant to your purposes.

In this case, DLookup simply "Looks" in the recordsource and returns a
value. That source can be a table or a saved query. WHICH value the
expression returns depends on several factors, including the sort order
applied to the recourdsource, if any, and the criteria passed in the
function, if any. In other words, simply using an unqualified Dlookup can
return differnt values at different times, if for no other reason than that
the source table or source query can be sorted differently.

One thing can be predicted: DLookup will bring back the "FIRST" matching
value, whatever that is. For that reason, unqualified DLookups against
unsorted recordsets can yield unpredictble results. It's up to you,
therefore, to construct the expression in such as way as to retrieve the
exact value you need.

Inside the parentheses, you'll find three "arguments" or "parameters". They
are surrounded by quotes and separated by commas. That's the standard syntax
required.

The first parameter is the name of the field containing the values you want.
In your example, for example, the field is aptly named field1.
Note that it is enclosed in brackets. Access puts brackets around field
names to indicate they are units. If you follow one of the standard naming
conventions in which spaces and special characters are not permitted in
names, the brackets MAY be optional. However, if you have mistakenly used
one of the reserved words as a field name, you MUST enclose it in brackets
here. A commonly made mistake, for example, is to name a field in a table
Date. That, of course, is the name Access uses to retrieve date values, so
it must be bracketed anywhere you use it.

So, this DLookup retrieve a value from a field called field1. If there are
multiple records in the table, of course, you'll get only the "FIRST" value,
however that happens to land.

The second argument, or parameter, is the name of the recordset from which
the value is to be retrieved. In your example, this is the generic table.
Note that the same considerations apply to quotes, names, and brackets. Note
that it is separated from the field argument by the comma.

AT this point, then, the DLookup will go to the table specified (i.e.
) and grab the "first" value it encounters in the field specified
(i.e. [field1]).

Okay, now comes the third argument, which you use to restrict the selected
value to those that meet your requirements. Again, note the comma, the use
of quotes and brackets.

This example employs a shortcut, in which you ASSUME the name of the table
from which field2 comes. It would be more complete to state this expression
as

DLookup("[field1]","
","
.[field2]=" & [table2]![field3])

That's what we're doing here. We're telling Access to go to table and
retrieve the value from field1. In order for Access to know WHICH of the
many records in table we want, we append the criterion, "Get the value which
is found in the record(s) where field2 match the values in field3 in the
other table".
That could be one or more matches, depending on what field2 and field3
represent. I will leave exporing that aspect to you to work out.

The final thing to consider is that the syntax here allows you to pass in
the CURRENT value of field3 so that this expression returns different
results in different calls. =" & [table2]![field3])

Compare that to a hard-coded expression in which ONE value is always used as
the criteria:

DLookup("[field1]","
","
.[field2]=747")

see the difference there, where the hard-coded value is inside the quotes,
and in which case, the expression will always match on that value: 747.

It's possible to add more details, but that should be enough to give you a
fair picture of what is happening.

As always, I encourage clarifications and corrections. Thanks.




Midland said:
The answer to my question appears to be "in a query, the criteria for
Dlookup
cannot directly refer to any field outside the query. This seems wrong, as
it
seems to defeat the purpose of the function, but it is all I can pull out
of
the language in the explanations on the web. I see examples that appear to
get past this by using the criteria format . . .

dlookup("[field1]","
","[field2]=" & [table2]![field3])

. . . but without any explanation as to why this odd combination of
characters would accomplish this. Can someone refer me to a more detailed
explanation of this procedure?

Thanks,
--
Midland


Midland said:
I have a query drawing on the tables GenoaMachine and Datalink, and this
expression . . .

QSetup:
DLookUp("[DataLink]![RateID]","[DataLink]","[Datalink]![RateID]=[GenoaMachine]![MRateID]")

Gives the response "Access can't find the name 'GenoaMachine!MRateID' you
entered."

The tables GenoaMachine and DataLink are linked to the query and the
field
names RateID and MRateID are correct. What might be the the error? Is it
a
logical flaw or a typo?
 
M

Midland

Excellent. But, there are details I am unclear about.

When you say “The purpose of the domain aggregate functions is, essentially,
to retrieve values from a specified record set,†Can the record set be any
table or query in the database? Can it be a linked table?

Consider the case where I have these objects in the database:

Tables: GMachine, Datalink
Query: InQry
Form: MachInput

MachInput is for the entry of production data into GMachine. Datalink holds
standard production rate values. InQry is for collating data, summing and
averaging, etc., a source for various reports.

Fundamental question: if I enter data in a bound object in MachInput, is it
immediately populated in GMachine when I hit enter or tab to move from the
object? Or is the update delayed until I leave that record? Is InQry
immediately updated? Is it relevant whether the tables and/or query are open
or closed? My impression is that the objects are linked and immediately
updated when they are closed, but if I have InQry open, for viewing, it is
not updated.

Another detail. Looking at the modified expression you used . . .

DLookup("[field1]","
","
.[field2]=" & [table2]![field3])

.. . . is there significance to the use of a period between the table and
field number rather than the exclamation point? Are the brackets mandatory if
you are using both the table and field names in this circumstance? Is there
any complication if I refer back to a query rather than a table?

--
Midland


George Hepworth said:
Hello, Are you asking for an exegesis of this sample you posted? To be
clearn, this is NOT an "odd combination of characters"; quite to the
contrary it is a very highly structured expression, with elements clearly
prescribed for them in the standard syntax for expressions of this type.

So, if you really want an explanation, here's how I would explain it.

The DLookup Function is one of the Domain Aggregate Functions (DMax and
DSum being two other examples).

The purpose of the domain aggregate functions is, essentially, to retrieve
values from a specified recordset. They do so by selecting value(s)
according to the parameters passed to the function, if any. Note the "if
any" part. Depending on the circumstances, you can omit criteria parameters
when it is appropriate to do so, or you may employ them. You apply criteria,
as you do in any other circumstance, in order to limit the values returned
by the function to those which are relevant to your purposes.

In this case, DLookup simply "Looks" in the recordsource and returns a
value. That source can be a table or a saved query. WHICH value the
expression returns depends on several factors, including the sort order
applied to the recourdsource, if any, and the criteria passed in the
function, if any. In other words, simply using an unqualified Dlookup can
return differnt values at different times, if for no other reason than that
the source table or source query can be sorted differently.

One thing can be predicted: DLookup will bring back the "FIRST" matching
value, whatever that is. For that reason, unqualified DLookups against
unsorted recordsets can yield unpredictble results. It's up to you,
therefore, to construct the expression in such as way as to retrieve the
exact value you need.

Inside the parentheses, you'll find three "arguments" or "parameters". They
are surrounded by quotes and separated by commas. That's the standard syntax
required.

The first parameter is the name of the field containing the values you want.
In your example, for example, the field is aptly named field1.
Note that it is enclosed in brackets. Access puts brackets around field
names to indicate they are units. If you follow one of the standard naming
conventions in which spaces and special characters are not permitted in
names, the brackets MAY be optional. However, if you have mistakenly used
one of the reserved words as a field name, you MUST enclose it in brackets
here. A commonly made mistake, for example, is to name a field in a table
Date. That, of course, is the name Access uses to retrieve date values, so
it must be bracketed anywhere you use it.

So, this DLookup retrieve a value from a field called field1. If there are
multiple records in the table, of course, you'll get only the "FIRST" value,
however that happens to land.

The second argument, or parameter, is the name of the recordset from which
the value is to be retrieved. In your example, this is the generic table.
Note that the same considerations apply to quotes, names, and brackets. Note
that it is separated from the field argument by the comma.

AT this point, then, the DLookup will go to the table specified (i.e.
) and grab the "first" value it encounters in the field specified
(i.e. [field1]).

Okay, now comes the third argument, which you use to restrict the selected
value to those that meet your requirements. Again, note the comma, the use
of quotes and brackets.

This example employs a shortcut, in which you ASSUME the name of the table
from which field2 comes. It would be more complete to state this expression
as

DLookup("[field1]","
","
.[field2]=" & [table2]![field3])

That's what we're doing here. We're telling Access to go to table and
retrieve the value from field1. In order for Access to know WHICH of the
many records in table we want, we append the criterion, "Get the value which
is found in the record(s) where field2 match the values in field3 in the
other table".
That could be one or more matches, depending on what field2 and field3
represent. I will leave exporing that aspect to you to work out.

The final thing to consider is that the syntax here allows you to pass in
the CURRENT value of field3 so that this expression returns different
results in different calls. =" & [table2]![field3])

Compare that to a hard-coded expression in which ONE value is always used as
the criteria:

DLookup("[field1]","
","
.[field2]=747")

see the difference there, where the hard-coded value is inside the quotes,
and in which case, the expression will always match on that value: 747.

It's possible to add more details, but that should be enough to give you a
fair picture of what is happening.

As always, I encourage clarifications and corrections. Thanks.

news:[email protected]...
 
J

John Spencer

The recordset can be a table, or a query (without parameter prompts), or a
linked table.

If you enter data on a form it is not saved to the table until you explicitly
save the data or until you move off the record or close the form or move from
the form to a subform or from a subform to a form. If you are entering in a
query or table directly you must move off the row you are entering.


Brackets are mandatory if your field or table name does not following the
recommendation that the name consist of ONLY letters, numbers, and the
underscore character. No spaces, periods, #, question marks, etc.


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
M

Midland

From what you are saying, since I am using calculated numbers to proof my
data input as it happens, I need to trigger a save before I move from the
current record being input to a new one. Is there a control I can add to do
that?
 
J

John Spencer

No need to when you move off the record to a new record there is an automatic
save of the current record. Re-read my comments.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
M

Midland

Uh, no, thanks, but that's just what I DON'T want to do. I want to be able to
verify the record as it is entered, while I have the source page in front of
me.
 
J

John Spencer

Then if you need a saved value add a button with the following VBA code

If Me.Dirty = True Then Me.Dirty = False

That will attempt to save the current record. I say attempt because you may
run into fields that are required but not filled in or other problems that
would preclude the save.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 

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

Similar Threads


Top