Create a custom function in Microsoft Access - ms-access-2007

I'm new to Microsoft Access and need some help.
I'm creating a database and for each record in the last field of the table I need to perform a calculation based on the fields before.
The Calculation is taking the input of each necessary field (which is a string of one of four options), converting it to a number (0,1,2,3) and then taking the sum.
I know how to write the actual code for the calculation in Macros, I'm just not sure how to set up adding my own custom calculation and have it perform on the addition or update of a record.
If someone could help me set this up that would be great! Thanks!

in a query you create a calculated field. i.e.Table A has fields F1 and F2. Make query qryA with F1 and F2 ... then create F3
F3: F1 + F2
then you use qryA for any forms/reports.....

Related

Visualizing last refresh date in power bi

Is it possible to add a card in power BI that shows the last time the underneath dataset has been updated?
Many thanks!
You can create a blank query and use the following M query to create a single-cell table:
let
Source = #table(type table[Last Refresh=datetime], {{DateTime.LocalNow()}})
in
Source
And then you can use the Card visual and drag in the column to show it. You can rename the field to remove the default aggregation. (It doesn't matter since there is only one row of data anyways)
The value will be recalculated each time the data is refreshed.
you can achieve this by adding a calculated column (Refresh data Column) in the model with the formula =NOW()
Then add measure
Last Refresh Date :=MAX(Table[Last Refresh Column])
Since calculated columns are calculated only on model refresh - it will make sure the calculation is right.
The below code is working for me (for HK).
Add a column with the below formula
Last Refresh = FORMAT(UTCNOW()+TIME(8,0,0),"dd MMMM YYYY ttttt")
Depending on the location it's really confusing with all those functions..
Let me suggest you the workable one:
Go to the:
Query Editor
1.right click on the Queries body on the left side
2.new queries
3.Blank Queries
4.Paste the following formula by putting the right Server Instances and DB Name,
" = Sql.Database("Server\Instance", "Database", [Query="select GETDATE() LastRefreshDate from AnyTableName"])
It'll work out. And since it's pulling directly from the server,, you don't have to worry about choosing the right DateTimeZone functions in PowerBI.

How to put the values of a column of an internal table into a variant?

Can somebody help me figure out if there is a way for the query below:
I have an internal table with one column with 69 records.
I want all these 69 records to be populated into a variant and get saved so that with this variant and the values saved in it I can run a particular program
How can I populate these values ?
Your question is a bit unclear for me.
Do you speak about two programs or one program?
What's the parameter you want to fill in the variant?
I'll just give you some hints - depending on your situation you must pick the correct parts.
I have an internal table with one column with 69 records.
How is the internal table filled?
I want all these 69 records to be populated into a variant and get saved so that with this variant and the values saved in it I can run a particular program
You have a program and you want to save a selection in a variant. So you need some parameters for the selection screen.
You want a table, so you need a SELECT-OPTION.
To define a SELECT-OPTION you need a DDIC-reference (you must say, what kind of field you want.). In the following example I use a material number (MARA-MATNR).
So you program contains something like:
TABLES mara.
SELECT-OPTIONS: s_matnr FOR mara-matnr.
With this you would get:
You can define ranges (from-to) and list of values. As you want only single values, you need something like:
SELECT-OPTIONS: s_matnr FOR mara-matnr NO INTERVALS.
Now you get:
When you push (1) you can enter values.
With (2) you can load from an external file,
with (3) you can load values from clipboard.
So you can fill your values and store the selection in a variant.
When you execute your program, the data is stored in a ranges table:
Now you can loop on this table and copy the S_MATNR-LOW value into your internal table for further processing.
If I misunderstood you question and you want to create a variant dynamically, then take a look on function module RS_VARIANT_ADD (or RS_VARIANT_COPY,RS_VARIANT_CHANGE...)
You could always put the values in TVARVC, either manually or via code. Then specify the TVARVC variable in the variant definition.

Coloring Excel Cell based on a table condition

I am going through the users of a system and reviewing if they have appropriate role names. I then completed an excel table that looks abit like this:
I'm trying to turn the table into a more readable format. I have made a pivot that looks like this:
But I'm not sure how to highlight the cells to reflect the 'Access Appropriate? Yes/No' column. Ideally, it should be colored yellow if the 'Access Appropriate?' = 'No'. I'm thinking of using VBA, but was wondering if there is an easier solution using formulas or pivot table?
Your pivoted data isn't an actual excel pivot table, is it? I know what the x mean, but where do they come from?
Two possibilities come to mind if you want a flexible setup without VBA, aswell as an rather simple VBA-approach that uses an UDF.
Quick'n'dirty (really dirty) would be to
use 1/0 instead of yes/no (you could write that into a helper column with an if-function)
create a new pivot with ROLE_NAME for columns, USER_NAME for rows and SUM or MAX of [Access appropriate] for values
that means: instead of your x you will end up having 1 and 0. Empty cells will still be empty.
conditional format the value-range, e.g. If 1 then green If 0 then yellow if "" then Nothing
Alternatively, you could build your output-table with formulas like INDEX, MATCH and VLOOKUP-formulas.
An additional Key-Column with USERNAME&ROLE_NAME will be needed
conditional format the value-range
VBA: Provided your Rows are distinct a user defined function could do the following
read data into a recordset IF that hasnt been done already (meaning: declared on module-level, the first function call will fill it)
access the data in your recordset with a Recordset.Filter based on your input parameters - USERNAME and ROLE_NAME, in your case
output a certain Field.Value based on your input parameter - Access Appropriate in your case
conditional format the TRUE/FALSE values you get (since this can't easily be done inside an UDF)

create new query based on access form

Here is my problem.
I am working with a database that is VERY simple, it has one table with 21 fields and a series of values. I have created a simple select query that selects 2 fields and generates a total value based on a query calculations field.
What I would like to do is to have an input form with combo boxes that allows users to pass in which fields they would like to select. Basically creating a GUI for someone LESS skilled (if that's possible) than myself to run the query.
Currently I have 21 potential fields. Would I have to create 21 x 21 queries and then try to write some kind of code that would link to the appropriate query if the criteria were selected from the combo boxes? Is there some kind of VBA script that could generate the query on the fly repeatedly?
Any hints on the way forward would be very helpful I've never used VBA but willing to thrash around if someone points in the right direction.
As per Comments, below = SQL for query
SELECT paddling.Paddling, boating.Boating, [boating]![Boating]*[paddling]![Paddling] AS Expr1
FROM paddling INNER JOIN boating ON paddling.useSpace = boating.useSpace;

Calculating the Sum of values in ms-access

I have created a query to calculate the sum of all of the profit values in a table, I tried to output this to a textbox on the main form of my database and I just the error #NAME?.
Has anyone tried this before and are there any major things I am missing?
We would need to see some code/design details to understand why your text box gets that #Name error.
Without those details, I'll just suggest you consider a DSum() expression, instead of a query, to load the text box. And DSum() is kind of like a SELECT query, but returns only a single value instead of a result set.
DSum("YourNumericField", "YourTable")
Examine the DSum online help topic for more details. You might find the optional Criteria parameter useful (like a WHERE clause in a SELECT statement) if you ever want to sum only a subset of rows from your table.
DSum("YourNumericField", "YourTable", "account_status = 'ACTIVE'")
If you have create query to calculate the sum called "querySum". On the form property sheet, make sure selection type you change to FORM, go to Data and select "querySum" as record source.
And then, click text box, go to the property sheet and choice Data > Control Source. So you can choice column from query to the text box.
Otherwise, if you want to use VBA. You can do like this
DSum("NumericField", "YourTable")
Or with condition
DSum("NumericField", "YourTable", "type = 'Payment'")