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

Convert Field Value String to Actual Field in Expression

5
I'm trying to improve performance on a query, and I was wondering if this is possible in Access.

Let's say I have a table [fieldtable] with one text column [field1] and one row.

In that column, I have a field name from another table [TransactionTable]. This fieldtable gets updated from a form, but it never has more than one row in it.

So an example value in my [fieldtable] look like this:

[TransactionTable].[Name]

If I put both tables in my query, without a link, I want to be able to call the field in my [TransactionTable] based on the string shown in my [fieldtable].



So my expression in this example query would be

TransField: [fieldtable].[field1]

which would effectively give me this:

TransField: [TransactionTable].[Name]


This does not work, but I'm looking for some way to make it work, if possible. Any ideas?
Sep 9 '06 #1
10 8568
MMcCarthy
14,534 Expert Mod 8TB
Don't put the fieldtable in your query. You can refer to it by:

Dlookup("[Field1]","[FieldTable]")


I'm trying to improve performance on a query, and I was wondering if this is possible in Access.

Let's say I have a table [fieldtable] with one text column [field1] and one row.

In that column, I have a field name from another table [TransactionTable]. This fieldtable gets updated from a form, but it never has more than one row in it.

So an example value in my [fieldtable] look like this:

[TransactionTable].[Name]

If I put both tables in my query, without a link, I want to be able to call the field in my [TransactionTable] based on the string shown in my [fieldtable].



So my expression in this example query would be

TransField: [fieldtable].[field1]

which would effectively give me this:

TransField: [TransactionTable].[Name]


This does not work, but I'm looking for some way to make it work, if possible. Any ideas?
Sep 9 '06 #2
Zontar
5
Don't put the fieldtable in your query. You can refer to it by:

Dlookup("[Field1]","[FieldTable]")
Thanks, that Interesting, I didn't know about DLookup. I tried it, but I am still getting the "Value" of the field and not the actual field.

When I tried this, all my rows still said

[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]

When I wanted them to say:

Bob Parker
Mary Williams
John Smith
Randy Jones
Lana Maxwell

In other words, the "Name" field from the Transaction Table.
Sep 9 '06 #3
MMcCarthy
14,534 Expert Mod 8TB
Zontar,

Where are you referencing the value [TransactionTable].[Name]

In a query or on the form?



Thanks, that Interesting, I didn't know about DLookup. I tried it, but I am still getting the "Value" of the field and not the actual field.

When I tried this, all my rows still said

[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]

When I wanted them to say:

Bob Parker
Mary Williams
John Smith
Randy Jones
Lana Maxwell

In other words, the "Name" field from the Transaction Table.
Sep 9 '06 #4
Zontar
5
Zontar,

Where are you referencing the value [TransactionTable].[Name]

In a query or on the form?

mmccarthy,

I am using a form, but my question is only regarding a query and a table.

The string "[TransactionTable].[Name]" is what I typed as the value in the first row of my FieldTable Table.

I hope to then use that value in the FieldTable in my query of the TransactionTable.

Basically, I want to Group By whatever field name happens to be in the FieldTable, if that makes sense.

I have a lot of potential field names that I can group on in my query. Instead of housing them all in nested IIF statements (which is too long of an expression anyway, and slows down the query quite a bit) I would like to use this alternative solution, if it's possible.
Sep 9 '06 #5
MMcCarthy
14,534 Expert Mod 8TB
I'm sorry I really don't understand what you're trying to do.

However, if you store "[TransactionTable].[Name]" as a value it will be a string representing the field in the table. But I don't understand how you are referencing it. Or maybe I just don't understand the question you're asking.


mmccarthy,

I am using a form, but my question is only regarding a query and a table.

The string "[TransactionTable].[Name]" is what I typed as the value in the first row of my FieldTable Table.

I hope to then use that value in the FieldTable in my query of the TransactionTable.

Basically, I want to Group By whatever field name happens to be in the FieldTable, if that makes sense.

I have a lot of potential field names that I can group on in my query. Instead of housing them all in nested IIF statements (which is too long of an expression anyway, and slows down the query quite a bit) I would like to use this alternative solution, if it's possible.
Sep 9 '06 #6
Zontar
5
I'm sorry I really don't understand what you're trying to do.

However, if you store "[TransactionTable].[Name]" as a value it will be a string representing the field in the table. But I don't understand how you are referencing it. Or maybe I just don't understand the question you're asking.

Sorry this is so hard to explain. Perhaps I should give a more complete explanation instead of trying to simplify it.

Basically, I would like to design a query that will Group By multiple fields depending on what fields a user selects in a Combo Box.

Since I would like two levels of grouping, it just isn't feasible to have hundreds of queries for every possible combination of groupings.


My FieldTable looks like this:

Field1
[TransactionTable].[Name]

Instead of [TransactionTable].[Name], the [Field1] field could also have the values

[TransactionTable].[Owner]
[TransactionTable].[Company]
[TransactionTable].[Facility]
[TransactionTable].[Location]

- depending on what the user selects in the Combo Box.



There are about 15 other fields in the transaction table that I want to be able to Group By in my query, with 2 levels of groupings. Instead of setting up a query for every possible combination, I would love to be able to do it in just one happy query.



So on my form, there is a combo box where a user will select which field to Group By in the query. I tried doing nested IIF statements in my query, like

SelectedGroup1: IIF([Forms]![Viewer]![Group1]="No Grouping", "No Grouping", IIF([Forms]![Viewer]![Group1]="Name", [TransactionTable].[Name], IIF([Forms]![Viewer]![Group1]="Facility", [TransactionTable].[Facility], IIF([Forms]![Viewer]![Group1]="Location", [TransactionTable].[Location], IIF([Forms]![Viewer]![Group1]="Company", [TransactionTable].[Company], IIF([Forms]![Viewer]![Group1]="Month", [TransactionTable].[Month].......

and so on and so on...

(Selected Group2 would be about the same, only it would reference a different control on the form, and be bound to a different table.)

However, I get an error message saying that my expression is too long. If I shorten it to only have a limited number of fields, the query is just so much slower than if I were grouping by the same field every time.

In order to get around this, I would like to do what I have asked about above. Basically, my query would Group By a field name that the user has selected in the combo box. I tried putting the Expression

SelectedGroup1: [FieldTable].[Field1]

which I hoped would equal [TransactionTable].[Name] and therefore reference that table.

in the query, but my results just looked like this:

[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
....

I tried using your suggestion of the DLookup, but had the same problem. There doesn't seem to be any way to trick Access into recognizing a string value in a field as a field itself, or is there?
Sep 9 '06 #7
MMcCarthy
14,534 Expert Mod 8TB
It sounds like you're trying to do dynamic querying. I've done it myself a few times and I know how complicated it can get.

Firstly you need as you said a table of field names. Normally, I create one overall query. I do this by building up my query. e.g. Build query 1 then build query 2 based on query 1 and other table(s) and so on. This allows me to create calculated fields like Count([field]) and [field]-[field]. In building these queries I establish a column name for each calculated value as well as the table field names. I then end up with a query representing all choices I want to give the user. I build my field table based on the columns in the final query. Any grouping levels are built into the query as I build it.

I can then create (or allow the user to create) any select query involving these fields and the query will select all fields from the query rather than a series of tables.

As I said, I've done this a number of times and the way you're trying to do it isn't really feasible in my experience. Maybe one of the other experts can help you out with it.

If I'm wrong in what you're trying to do, please let me know.

In answer to your final question:

>I tried using your suggestion of the DLookup, but had the same >problem. There doesn't seem to be any way to trick Access into >recognizing a string value in a field as a field itself, or is there?

You would have to represent the TableName and Fieldname as two separate strings. Even then it would depend on how you called them.

If your table had two fields instead of one. One for the tablename and one for the field name. See basic query below as example of how to call them.

"SELECT [" & [Field2] & "] FROM [" & [Field1]

This would be passed to a string and the string then would make up the query.


Sorry this is so hard to explain. Perhaps I should give a more complete explanation instead of trying to simplify it.

Basically, I would like to design a query that will Group By multiple fields depending on what fields a user selects in a Combo Box.

Since I would like two levels of grouping, it just isn't feasible to have hundreds of queries for every possible combination of groupings.


My FieldTable looks like this:

Field1
[TransactionTable].[Name]

Instead of [TransactionTable].[Name], the [Field1] field could also have the values

[TransactionTable].[Owner]
[TransactionTable].[Company]
[TransactionTable].[Facility]
[TransactionTable].[Location]

- depending on what the user selects in the Combo Box.



There are about 15 other fields in the transaction table that I want to be able to Group By in my query, with 2 levels of groupings. Instead of setting up a query for every possible combination, I would love to be able to do it in just one happy query.



So on my form, there is a combo box where a user will select which field to Group By in the query. I tried doing nested IIF statements in my query, like

SelectedGroup1: IIF([Forms]![Viewer]![Group1]="No Grouping", "No Grouping", IIF([Forms]![Viewer]![Group1]="Name", [TransactionTable].[Name], IIF([Forms]![Viewer]![Group1]="Facility", [TransactionTable].[Facility], IIF([Forms]![Viewer]![Group1]="Location", [TransactionTable].[Location], IIF([Forms]![Viewer]![Group1]="Company", [TransactionTable].[Company], IIF([Forms]![Viewer]![Group1]="Month", [TransactionTable].[Month].......

and so on and so on...

(Selected Group2 would be about the same, only it would reference a different control on the form, and be bound to a different table.)

However, I get an error message saying that my expression is too long. If I shorten it to only have a limited number of fields, the query is just so much slower than if I were grouping by the same field every time.

In order to get around this, I would like to do what I have asked about above. Basically, my query would Group By a field name that the user has selected in the combo box. I tried putting the Expression

SelectedGroup1: [FieldTable].[Field1]

which I hoped would equal [TransactionTable].[Name] and therefore reference that table.

in the query, but my results just looked like this:

[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
....

I tried using your suggestion of the DLookup, but had the same problem. There doesn't seem to be any way to trick Access into recognizing a string value in a field as a field itself, or is there?
Sep 9 '06 #8
Zontar
5
mmccarthy,

Thanks for your help.. I am excited about the possibility of doing Dynamic Querying in MS Access, but I don't really know where to go from here. I tried some of the stuff you mentioned, but wasn't able to pass a SELECT statement into the query so it would Execute the SELECT statement, and not just display the SELECT statement.

I'm assuming this has to be done in VB code, which I'm not opposed to, I just usually can only work with an example.

If anyone has any good examples of how I can do Dynamic Querying, I would really appreciate it.
Sep 9 '06 #9
MMcCarthy
14,534 Expert Mod 8TB
In the VBA code you need to create a query definition, probably on a button click. In the following example the list of fields is called List0. The Lists mult select property should be set to simple:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRunQuery_OnClick()
  2. 'This will create the query "My_Query"
  3. Dim qdfNew As DAO.QueryDef
  4. Dim strSQL as String
  5.  
  6.     strSQL = buildQuery ' build the query - see below
  7.     With CurrentDb
  8.         '  createquerydef command line follows
  9.         Set qdfNew = .CreateQueryDef("My_Query", strSQL)
  10.         qdfNew.Close
  11.         Set qdfNew = Nothing
  12.         .Close
  13.     End With
  14. End Sub
  15.  
  16. Function buildQuery() As String
  17. ' Function to build the query
  18. Dim valSelection As Variant
  19. Dim selectSQL As String
  20. Dim strValues as String
  21.  
  22.     strValues = ""
  23.     selectSQL = "SELECT "
  24.     'Go throught each selected 'record' in listbox
  25.     For Each valSelection In Me.List0.ItemsSelected
  26.         'Save value to appropriate text box
  27.         strValues = strValues & "[" & Me.List0.ItemData(valSelection)  & "], "  
  28.     Next valSelection
  29.  
  30.     ' remove the last , and space
  31.     selectSQL = Left(selectSQL, Len(selectSQL) - 2)
  32.     selectSQL = selectSQL & " FROM QueryName;"
  33.  
  34.     buildQuery = selectSQL ' return the statement
  35.  
  36. End Function
  37.  
  38.  


mmccarthy,

Thanks for your help.. I am excited about the possibility of doing Dynamic Querying in MS Access, but I don't really know where to go from here. I tried some of the stuff you mentioned, but wasn't able to pass a SELECT statement into the query so it would Execute the SELECT statement, and not just display the SELECT statement.

I'm assuming this has to be done in VB code, which I'm not opposed to, I just usually can only work with an example.

If anyone has any good examples of how I can do Dynamic Querying, I would really appreciate it.
Sep 9 '06 #10
DLookUp is working nice just use Forms() to extract values from forms and insert it as field name, in example:

Combo name is [SelectName]

so extract from combo selected value with:
Expand|Select|Wrap|Line Numbers
  1. Forms("Same form")("[SelectName]")
  2.  
then insert this value to DLookUp as field name like this:
Expand|Select|Wrap|Line Numbers
  1. "[" & Forms("Form with combo")("[SelectName]") & "]"
  2.  
this is recognized as field name

and insert in to DLookUp:
Expand|Select|Wrap|Line Numbers
  1. =DLookUp("[" & Forms("Form with combo")("[SelectName]") & "]";"[FieldTable]";"[Name] = " & Forms("Form with combo")("[SelectName]"))
  2.  
Last
Expand|Select|Wrap|Line Numbers
  1. "[Name] = " & Forms("Form with combo")("[SelectName]")
  2.  
it is filter and [Name] can also be construction of field name generated selected form.
Nov 30 '06 #11

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

Similar topics

7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
5
by: Andrew V. Romero | last post by:
At work we have an excel file that contains the list of medications and their corresponding strengths. I would like to save the excel file as a text list and paste this list into a javascript...
4
by: Tom Esker | last post by:
I've got Javascript in a form that adds up all of the numbers in a column of form fields and displays a total. It works great if every field has an initial value of 0, but if any of them are null,...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
9
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I am trying to assign a Delegate to an Event using reflection. I use FieldInfo.GetValue to supposedly get the field object, but casting the result to the type of the field and...
2
by: KB | last post by:
Hi guys, In my DataGrid I have a column that displays decimal values as currency ( I set the Data Formatting expression of that column to {0:C}). So the actual string displayed in the grid looks...
7
by: Tizzah | last post by:
What is wrong with that? regex = /^(http|https):\/\/+({1}+)*\.{2,5}(({1,5})?\/.*)?$/ if(field.hpage.value != regex.test(field.hpage.value)){ alert("Bad Homepage") field.hpage.focus()...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
10
by: satishrajana | last post by:
Hi, My SQL returns a NULL in a datefield if there is no date in that field. If there is a NULL in this column, I want to replace it with spaces in my SELECT statement when I am selecting these...
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
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: 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:
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
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.