473,406 Members | 2,956 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,406 software developers and data experts.

How to return multiple values from an aggregated dataset to a textbox?

79 64KB
I am trying to build a mechanism that will return value from multiple records in a query to a textbox. I started with placing DLookup function in the textbox’s data control source. Later, I realized DLookup will only return the first matched value. Then I learned that I should use Recordset to accomplish this.

Suppose I have a recipe as below.

Expand|Select|Wrap|Line Numbers
  1. Recipe ID:  1001
  2.  
  3. Ingredient    Percentage    Allergen
  4.  
  5. Milk          10%           Milk
  6. Wheat Flour   50%           Wheat
  7. Water         35%        
  8. Sugar          4%    
  9. Salt           1%
  10.  
  11.  
The value that should be returned to the textbox is allergens from each ingredient in the recipe. In this case, it should return “Milk Wheat” to the allergen text box.

The ingredient name, allergen and percentage exist in an Access query named Q_Recipe_Allergen where several tables are joined. The rows of above recipe are from a SubForm that resides in a MainForm containing recipe header information, such as RecipeID.

I tried the following codes, but it didn’t work. I know something is missing but I don’t know how to fix it.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Txt_Recipe_Allergen_AfterUpdate()
  2.  
  3. Set db = CurrentDb()
  4. Set rs = db.OpenRecordset("Select '[Allergen]' From [Q_Recipe_Allergen] WHERE '[recipeID]=Me![Txt_RecipeID]'")
  5.  
  6. Me.Txt_Recipe_Allergen = rs
  7.  
  8. End Sub
  9.  
Thanks in advance for helps.
Nov 16 '11 #1
6 3218
NeoPa
32,556 Expert Mod 16PB
You may find what you're after by going through Combining Rows-Opposite of Union.
Nov 17 '11 #2
Joe Y
79 64KB
NeoPa,

I think I need more helps and directions.

I assume that I supposed to replace ‘Item’ and ‘GroupBy’ in the thread you referred to with ‘Allergen’ and ‘recipeID’ in my case.

Then, I place the Concat function in a public module and the query to be placed in the textbox’s event?

Do I need to declare or set function of any variable to return the text from the query to the textbox? Sorry, I am still unfamiliar with how VBA works to catch or display data.

Thanks,
Joe
Nov 17 '11 #3
NeoPa
32,556 Expert Mod 16PB
I'm not sure I have all the details required but this example is my best guess at this point. Try it out and let me know how it goes :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Txt_Recipe_Allergen_AfterUpdate()
  2.     Dim strSQL As String
  3.     Dim db As DAO.Database
  4.     Dim rs As DAO.Recordset
  5.  
  6.     strSQL = "SELECT [Recipe_ID]" & _
  7.                   ", Max(Concat([Recipe_ID], Nz([Allergen],''))) AS [Allergens] " & _
  8.              "FROM   [Q_Recipe_Allergen] " & _
  9.              "WHERE  ([RecipeID] = Me![Txt_RecipeID] & ")"
  10.     Set db = CurrentDb()
  11.     Set rs = db.OpenRecordset(strSQL)
  12.     Me.Txt_Recipe_Allergen = rs!Allergens
  13. End Sub
PS. Whatever should trigger this code I very much doubt it should be the Txt_Recipe_Allergen_AfterUpdate() event procedure. I don't know where this was coming from but you are trying to set the value - not respond to it having been set.
Nov 17 '11 #4
Joe Y
79 64KB
Thanks NeoPa,

I am still working on this. I got “Object Required” when I test the codes. Then I changed the code

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
To

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As QueryDef
  2.  
Then the error message “Type Mismatch” appeared and Access highlighted the “&” at the end of line

Expand|Select|Wrap|Line Numbers
  1. "FROM [Q_Recipe_Allergen]" & _
  2.  
I checked the spelling of field and query names. I haven’t found misspelling, so far. Other than misspelling, does this error message “Type Mismatch” mean something else?

On the other hand, I meant to use Default Value of the text box to return query value, but it only takes Expression Builder, not VBA code. I don’t know which event procedure would be the best for setting the returned value from VBA.
Nov 21 '11 #5
NeoPa
32,556 Expert Mod 16PB
You need to determine as a matter of priority when you want this code to run. If you describe that clearly to me I will suggest an event to use for the procedure.

As a separate issue we need to find what, if anything, is wrong with the code. Check out When Posting (VBA or SQL) Code for some necessary tips on posting code (Your VBA code particularly). Point #A2 explains how you need to report errors if you have known ones you cannot resolve yourself. You needn't post the code again if it's exactly as shown in post #4 (and it should be - I have no interest in this stage in going off on any unnecessary tangents).
Nov 21 '11 #6
NeoPa
32,556 Expert Mod 16PB
Joe Y:
On the other hand, I meant to use Default Value of the text box to return query value, but it only takes Expression Builder, not VBA code. I don’t know which event procedure would be the best for setting the returned value from VBA.
I'm afraid I have no idea what you're talking about here. It looks like English but I don't know what you're saying.
Nov 21 '11 #7

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

Similar topics

6
by: Lupe | last post by:
hi, if someone can help me I would be grateful when I do def function kjklj llklç return variableA, variableB
0
by: Jaye Gallagher | last post by:
Hi there, I'm coming from a MS-SQL/MySQL background, and am trying to understand the way Postgres phrases "stored procedure" type stuff. What is mystifying me, in particular, is the fact that...
1
by: turtle | last post by:
I need to write an update query that will return the earliest date to a table based on the data of a different table. I have a labor table that looks like this TableLabor JobCode WorkORder ...
4
by: Aaron | last post by:
can a method return multiple values? pseudo code public string method1() { //db pull select col1, col2, from tb1 s1 = col1;
3
by: smen | last post by:
hiye, can stored procedure return multiple @output's? thanks for replying...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
1
by: deepadaffine | last post by:
I am doing a program that calculate velocity and acceleration. I get all my input in main and then i call a sub function giving the input values as parameter and calculate velocity and...
1
by: v4u2chat | last post by:
Do I need to extend any of classes from AXIS to return multiple values? I'm exposing the following method as web service through AXIS to return multiple values. public ContactAddress...
4
by: ashokbio | last post by:
I want to return values of two arguments through a function via same argument. Example: (a, b) = getvalue(x, y) Can anyone help?
2
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.