What might be the formula or code to find the max value of 3-6 fields PER RECORD in a query. I have individual vendors offering prices for a number of products. Each product is a new record. I would like a calculated expression in my query design to be BEST COST. BEST COST would be the max value of the 3-6 vendors. I thought of using iif() but I felt there should be better way. Any help?
13 9803 NeoPa 32,556
Expert Mod 16PB
2 options I know of :
1. V V complicated IIF().
2. Call Public Function in a module designed to find max of parameters entered.
Is there no MAX IN (list of fields) command?
NeoPa 32,556
Expert Mod 16PB
I'm pretty sure there's not :(.
We had a thread in here last week about getting the max of three fields and that was handled by IIF() in the end.
What might be the formula or code to find the max value of 3-6 fields PER RECORD in a query. I have individual vendors offering prices for a number of products. Each product is a new record. I would like a calculated expression in my query design to be BEST COST. BEST COST would be the max value of the 3-6 vendors. I thought of using iif() but I felt there should be better way. Any help?
It's possible that this is suitable for a crosstab query using your query as a base.
Can you post the full sql statement of your query and I'll have a look.
It's possible that this is suitable for a crosstab query using your query as a base.
Can you post the full sql statement of your query and I'll have a look.
Sorry one other thing can you confirm it is the max of the values you are looking for and not the min (lowest value).
PEB 1,418
Expert 1GB
I can offer you 2 functions...
The first one after adaption can calculate max or min of all fields in a query: -
Function min_in_columns(SQL, Start_col, End_col) As Double
-
Dim mydb As Database
-
Dim myr As Recordset
-
Dim i
-
Dim min_value
-
-
Set mydb = CurrentDb()
-
Set myr = mydb.OpenRecordset(SQL)
-
-
min_value = 0
-
-
myr.MoveFirst
-
For i = Start_col To End_col
-
If min_value > myr(i) Then
-
min_value = myr(i)
-
End If
-
Next i
-
myr.Close
-
mydb.Close
-
-
min_in_columns = min_value
-
End Function
-
If you want to use it in a query based on table Data with PK ID so you have to type as column in your query:
Min:min_in_columns("SELECT * FROM DATA WHERE ID="+str(ID)+";", 5, 10)
This will give you the minimum from the columns between 6 and 11
And this function is a bit more simple for avoiding the multiple IIF() only: -
Function MinNumb(A, b)
-
If A < b Then
-
MinNumb = A
-
Else
-
MinNumb = b
-
End If
-
End Function
-
-
So for 3 fields it should be:
Min: MinNumb(C, MinNumb(A, b))
To confirm...it is the MAX value I am looking for. My sql is:
SELECT Table1.RequestDate, Table1.Symbol, Table1.Cusip, Table1.MLRate, Table1.TotalQty, Total1.TotalNV, Total1.JefRate, Total1.JefQty, Total1.QuadRate, Total1.QuadQty, IIf([QuadRate]>[JefRate],[QuadRate],[JefRate]) AS [Best Rate], [MLRate]*[TotalNV] AS [Current Cost], [Best Rate]*[TotalNV] AS [Best Cost], [Best Cost]-[Current Cost] AS Difference, IIf([Best Rate]=[JefRate],"Jefferies",IIf([Best Cost]=[QuadRate],"Quadriserv","No Change")) AS [Who to Call]
FROM Table1;
As you can see I have begun an embedded IIF() statement. What I would like to do is in my BEST RATE column of my query, I would put some 'equation' that would return the MAX value from JefRate,ML rate and QuadRate. As for the suggestion of the "simple" function", your example showed complete code for 2 variables (a) and (b). If I have 3,4 or 5 variables, would this not just be the same as my embedded IIF() statement?
Thank you again for any help here!!
I also like your function idea. I have never, but have always wanted to create a function. Would I create it in the modules area?
NeoPa 32,556
Expert Mod 16PB
In the VBA window (Alt-F11 from the database), Right-click anywhere in your project and insert a module.
In here type in the Public function you want.
In the VBA window (Alt-F11 from the database), Right-click anywhere in your project and insert a module.
In here type in the Public function you want.
In the module just create the function, referencing Vladi's brilliant! idea. - Function MaxNumb(A, b)
-
If A > b Then
-
MaxNumb = A
-
Else
-
MaxNumb = b
-
End If
-
End Function
Then in query
SELECT MaxNumb(C,MaxNumb(A, B)) As MaxNo ....
Get the idea?
Yes, I did it and it worked! I actually changed the function given to work for more than 2 items! Thanks for all the support!
Yes, I did it and it worked! I actually changed the function given to work for more than 2 items! Thanks for all the support!
Using your initiative, that's what I like to see. If you want you can post the function you're currently using for the benefit of future searches on this thread.
Mary
NeoPa 32,556
Expert Mod 16PB
I was just writing a recursive function to do it for any number of parameters when I realised VBA doesn't support passing on all the extra parameters in the 'ParamArray' to another function call :(.
(Killer - please contradict - I hope there really is a way).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Shufen |
last post by:
Hi,
I'm a newbie that just started to learn python, html and etc. I have
some questions to ask and hope that someone can help me on.
I'm trying to code a python script (with HTML) to get...
|
by: |
last post by:
If I need to check if a certain value does exist in a field, and return
either "yes" or "not" which query would be the most effestive?
|
by: rickcheney |
last post by:
I just changed my Access 2002 database to a SQL Server ADP project. I
had a form where the user entered a value into a text box and when a
command button on the form was clicked a Report was...
|
by: Stuart E. Wugalter |
last post by:
Hello.
No one in the queries newgroup answered this question. I hope someone here
can. Thank you. Stuart
The following Update Query allows my users to substitute a numerical value
for the...
|
by: Ken |
last post by:
I wrote a function to use in queries that takes a date and adds or
subtracts a certain length time and then returns the new value. There
are times when my function needs to return Null values.
...
|
by: thomas goodwin |
last post by:
I have a query which asks for a parameter value to execute it.
To see the results I have to:
a) click on the query -- the "Enter Parameter Value" window pops up.
b) enter the parameter value
c)...
|
by: jjturon |
last post by:
Can anyone help me??
I am trying to pass a Select Query variable to a table using Dlookup
and return the value to same select query but to another field.
Ex.
SalesManID ...
|
by: |
last post by:
Hi everyone,
I have a form with a combo box on it. When you select a value (a PO#) from
the combo box, the bound field is the indexID of the selected PO. On the
same form, I have a text box...
|
by: tania |
last post by:
i have this table in my database:
CREATE TABLE FILM(
F_ID INT(5) NOT NULL AUTO_INCREMENT,
F_TITLE VARCHAR(40) NOT NULL,
DIRECTOR_FNAME VARCHAR(20) NOT NULL,
DIRECTOR_LNAME VARCHAR(20) NOT NULL,...
|
by: Brad Baker |
last post by:
I have an asp.net/csharp application that requires a particular variable to
work properly. This variable is usually passed via a query string in the URL
when the application is first run but under...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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: 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...
| |