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

How to access literal value of a field on a query that has a row source on the table

beacon
579 512MB
Hi everybody,

[Access 2003]

I have a combo box field in a table with a number data type that has a row source to another table that has the text value for the item selected. Now, in one of my queries, I would like to access the text value of this field without having to add the second table to my query. Is that possible? I've done it in forms and reports using the .Text or .Column property, but I'm not seeing that has an option in the Expression builder.

Here's an very simplified example of what I'm talking about. The two tables are:
Expand|Select|Wrap|Line Numbers
  1. tblRecords
  2.  
  3. 'Not all fields shown
  4. RecordID: Autonumber
  5. PartIDFK: Number 'Combo box with row source = tblParts. Displays the second of 2 columns in table (PartName)
  6.  
Expand|Select|Wrap|Line Numbers
  1. tblParts
  2.  
  3. PartID: Autonumber
  4. PartName: Text
  5.  
There's a one-to-many relationship between tblRecords and tblParts. In the query I'm trying to create, which includes another table and isn't important to the example, I have PartID as one of the fields. PartID shows up in the query as a combo box and displays the value, but it's bound to the number in the table. I want to grab the text for the field instead of the number.

So if PartIDFK = 2, but the display value is "Engine", I want to be able to access "Engine" in the expression instead of 2.

Just to hopefully address a question you might have, I can't include tblParts in the query because it negatively affects the code in a form where I'm using the query.

Thanks,
beacon
Aug 4 '10 #1

✓ answered by Stewart Ross

Hi Beacon. The column property of a control cannot be referred to within a query as far as I know.

As you have mentioned that you cannot include the table tblParts into the source query for your form, which would be by far the simplest option, you have two choices:

(1) use DLookup to lookup the value corresponding to the part ID, or

(2) use a bespoke VBA function to lookup the value of your combo box text column.

Option 1 is not dependent on the combo, and is the most general solution:

Expand|Select|Wrap|Line Numbers
  1. =Dlookup("PartName", "tblParts", "PartID = " & Forms!YourFormName!YourPartIdControlName)
This will be slower to run than simply looking up the combo would be.

Option 2 is straightforward to do but does tie your query to the combo on the form. Assuming that the combo is on the main form, not a subform, place the following function in a general code module (one that you can access from the Modules tab in Access):

Expand|Select|Wrap|Line Numbers
  1. Public Function fFormControlValue(ByVal FormName As String, ByVal ControlName As String, Optional ByVal ColumnNo As Integer = 0)
  2.     If ColumnNo > 0 Then
  3.         fFormControlValue = Forms(FormName).Controls(ControlName).column(ColumnNo)
  4.     Else
  5.         fFormControlValue = Forms(FormName).Controls(ControlName)
  6.     End If
  7. End Function
In the query editor add a new field calling the function as follows:

Expand|Select|Wrap|Line Numbers
  1. Part Name: fFormControlValue("YourFormName", "YourPartIdControlName", 1)
e.g. if your form is named frmRecords and the control PartIDFK the call would be:

Expand|Select|Wrap|Line Numbers
  1. Part Name: fFormControlValue("frmRecords", "PartIDFK", 1)

The column number is optional in the function listed - if you leave it out the function will just return the value of the control itself (equivalent to the value for Column (0) in a combo).

Access queries can always interpret calls to built-in and bespoke functions, whereas some objects cannot be referred to at all from within a query, and others may be unreliable (as for example when referring directly to the current value of a control as a criterion in a query).

-Stewart

1 2449
Stewart Ross
2,545 Expert Mod 2GB
Hi Beacon. The column property of a control cannot be referred to within a query as far as I know.

As you have mentioned that you cannot include the table tblParts into the source query for your form, which would be by far the simplest option, you have two choices:

(1) use DLookup to lookup the value corresponding to the part ID, or

(2) use a bespoke VBA function to lookup the value of your combo box text column.

Option 1 is not dependent on the combo, and is the most general solution:

Expand|Select|Wrap|Line Numbers
  1. =Dlookup("PartName", "tblParts", "PartID = " & Forms!YourFormName!YourPartIdControlName)
This will be slower to run than simply looking up the combo would be.

Option 2 is straightforward to do but does tie your query to the combo on the form. Assuming that the combo is on the main form, not a subform, place the following function in a general code module (one that you can access from the Modules tab in Access):

Expand|Select|Wrap|Line Numbers
  1. Public Function fFormControlValue(ByVal FormName As String, ByVal ControlName As String, Optional ByVal ColumnNo As Integer = 0)
  2.     If ColumnNo > 0 Then
  3.         fFormControlValue = Forms(FormName).Controls(ControlName).column(ColumnNo)
  4.     Else
  5.         fFormControlValue = Forms(FormName).Controls(ControlName)
  6.     End If
  7. End Function
In the query editor add a new field calling the function as follows:

Expand|Select|Wrap|Line Numbers
  1. Part Name: fFormControlValue("YourFormName", "YourPartIdControlName", 1)
e.g. if your form is named frmRecords and the control PartIDFK the call would be:

Expand|Select|Wrap|Line Numbers
  1. Part Name: fFormControlValue("frmRecords", "PartIDFK", 1)

The column number is optional in the function listed - if you leave it out the function will just return the value of the control itself (equivalent to the value for Column (0) in a combo).

Access queries can always interpret calls to built-in and bespoke functions, whereas some objects cannot be referred to at all from within a query, and others may be unreliable (as for example when referring directly to the current value of a control as a criterion in a query).

-Stewart
Aug 5 '10 #2

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

Similar topics

13
by: MLH | last post by:
I have a form with two controls: !! - combo box !! - text box A button on the form tries to run this SQL when clicked... INSERT INTO BodyMsgsSent (ToWhom, BodyText) SELECT DISTINCTROW !! AS...
2
by: dixie | last post by:
I am trying to write some VBA that can check if a certain value exists in a field in a table. The field is a text field. The table is not a part of the query that forms the recordsource of the...
0
by: leavandor | last post by:
I am trying to design a query that works with a relationship between a Table and a Query. I am comparing a value in the table with a computed value inside the query. The reason for this is that...
1
by: WillieW | last post by:
Hi folks, I have Access 97 and have set up four tables, each with a Primary Key with a file name manually entered. For example, the four tables relate to information stored in a paper file, on...
1
by: somanyusernamesaretakenal | last post by:
Hi everyone, I am working with Access 2003. I know a crosstab query only allows 1 value field, but how to do you around this restriction? I have a table with let’s say customers, products,...
5
by: Rex | last post by:
Hi, I want to change a value in one table depending on the value(s) in another table. I am trying to achieve this in a form. to elaborate I have a many-to-many relationship between tables...
4
by: TinaF | last post by:
I want to set a default value in a table called "Quotes" for a field called "Source." The "Source" field uses a lookup table called "Source" which looks like this: Sourceabbrev Sourcename...
2
by: JC21 | last post by:
Hi All, I would like to know how do I compare one field in one table to another in another table. I have one table which is the Archive table. It keeps a history of everything, I have another...
7
by: nhkam | last post by:
I am using MS Access 2007 I have a transaction table which holds all records with posting date on each of them. I used a make table query to find out the max date hold in the transaction table and...
8
by: alinagoo | last post by:
Hello all. I have the following code but it does not work! Please help me how can i access to a field with a variable keeping Name of that field? PRIVATE SUB ANALYZE(FieldName as string) dim...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...
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...
0
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...
0
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,...

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.