Use Variable data in Microsoft Access 2010 Data Macro - ms-access

I am new to Access 2010 Data Macros!
In my database I have a function that returns the UserId of the current user. The function returns pubUserId (Public pubUserId as integer)
I have a Data Macro that writes a new record to a table. This works very well. I want to improve the macro by:
* including a call to this function to confirm pubUserId (I then know 100% who the user is!)
* write pubUserId to a field in the table. (This will then keep a record of who did what!)
How do I do the above two steps?

Your macro should RunSql, and your Sql expression should append the result of the pubUserId function at the end the SQL INSERT statement.

I created a query that returns the value of the function (1 row, 1 cell). Then I set the field to something along the lines of DLookup("UserID","ViewName"). Voila!

Related

How to read first line of the flat-file containing date value and compare with a user variable defined in 2008 SSIS package without using sript task

i want to read first line of the flat-file containing date value and compare with a user variable defined in 2008 SSIS package without using sript task.
This solution is a bit long for what the simple task you require but since you don't you want to use script task, you can try below:
Create variable to store the date value from your flat file
Create a data flow task to import the flat file into a SQL table.
Add an Execute SQL task to get the first line from the SQL table and map the result
set to the variable created at step 1.
You can now compare the variable created and the one you already have.
e.g #[User::NewVariableCreated] == #[User::ExistingVariable]
This will return a Boolean data type result of True or False depending on the values supplied.
Hope this helps.
One option is to use a Conditional Split. Within this task, a condition can then be added comparing the date column of each row with the variable. A basic example of a condition for this is below, which checks to see if the date column of each row is equivalent to or more recent than the date variable. From here, the rows be directed based off whichever condition they match.
FlatFileDateColumn >= #[User::DateVariable]

Alter Large Block of Records with Data Macro

Several hundred records were incorrectly entered into a table in my Access Database.
A particular ID number includes a two digit leading code that designates the state that record is from; so 01-24586 is a record from the ACT, and 02-55719 is a record from NSW. The incorrect entries have these two switched. I need to replace the first two digits of these records' IDs with the correct code.
To do this, I've tried to write a Named Data Macro that I can call from a regular macro object (so I can double click it in the navigation pane). I've done that, but it doesn't seem to work. My Data Macro (just one of the State fixes) looks like this:
If [State]="NSW" Then
For Each Record In tblCustomer
Where Condition =[State]="NSW"
Alias NSWCust
EditRecord
Alias NSWCust
SetField
Name MyobID
Value = "02-" & Right([MyobID],5)
End EditRecord
End If
When I call it from the other macro, using RunDataMacro it gives me error 3709.
Is this a bad way to go about fixing this? What's wrong with my execution?
Data macros are intended to be used as "triggers" (perform an action when a specific event occurs.
In order to update data, you should use an update query.
Statement would look like this:
UPDATE tblCustomer
SET MyobID = "02-" & Right([MyobID],5)
WHERE [State] = "NSW"

Pad user input with zeros in a multiple value parameter

Is there a way to pad numbers in a multiple value parameter to make them have 8 characters in length? Right now I am using:
=Right("00000000" & Parameters!Accounts.Value,8)
in an invisible parameter then that parameter is passed to the in part of my query. When I have, multiple values unchecked it works properly, but as soon as I turn on multiple values I get an error
The DefaultValue expression for the report parameter ‘ActualAccounts’ contains an error: Operator ‘&’ is not defined for string “00000000” and type ‘Object()’.”
I want the user to be able to paste in a list of accounts like:
93874
93932128
3838
And then it queries the database as
00093874
93932128
00003838
Here is an approach that could work for you.
First, grab the udf_Split user-defined function (UDF) from the this question's answer. With this UDF you to pass the delimited string as a parameter, and it will return a table with a row for each value. This is how SSRS sends the data to SQL Server when it comes to multi value parameters.
Once you have that in place, then all you need to do is use that UDF in the following manner.
DECLARE #ActualAccounts varchar(100) = '93874,93932128,3838'
SELECT RIGHT('00000000' + RTRIM(LTRIM(Value)), 8) AS Accounts
FROM dbo.udf_Split(#ActualAccounts, ',');
Results:
Accounts
--------
00093874
93932128
00003838
Then you could use this in the SQL for the report
SELECT *
FROM Accounts
WHERE AccountNumber IN (SELECT RIGHT('00000000' + RTRIM(LTRIM(Value)), 8)
FROM dbo.udf_Split(#ActualAccounts, ','));
This answer assumes the name of the SSRS parameter is ActualAccounts. You should be able to fit this into a stored procedure, if that is what you are using. It should work in straight SQL if you have that embedded in the RDL, too.
Hope this helps out.

How to use Excel Object Model in Access Query Expression?

I am moderate to advanced user of Microsoft Excel, but very new to Microsoft Access (2010 version). What I am trying to do is use Excel functions in an Access Query Expression. So far, I've researched how to set Access to reference the Microsoft Object Model by going to Create/Module/Tools/References and then selecting the Microsoft Excel 15.0 Object Library.
From there, I went to my Query (Design View) and attempted to add an expression in the Field row to calculate the distance between two points. As a test, I typed:
Distance: Excel.WorksheetFunction.ACOS(50)
I thought this would work, but once I closed, saved the Query, and reran the Query I received the following error:
Undefined function 'Excel.WorksheetFunction.ACOS' in expression
I've done some Googling to determine why this isn't working, but have been unsuccessful. I'm not sure if Access allows you to reference Excel directly from the expression. Or, perhaps my syntax is incorrect.
Write a simple function in VBA (in Access) that calls the Excel function you want to use:
public function myTestFunction(x as double) as double
myTestFunction = Excel.WorksheetFunction.ACOS(50)
end function
Use this function in your query:
If you use the query design grid:
Simply write the function in a column and put the column name of the column that holds the input value as the argument. If you want to put an alias to the column, write the alias before and use :; something like this: columnAlias: myTestFunction([OtherColumn])
If you're writing your query using SQL:
Write your query as usual; use the function like any other function available in SQL:
select [OtherColumn], myTestFunction([OtherColumn]) as function
from [YourTable]

Fields cannot be used in report parameter expression

I have to set the start_date of my report depending of a report parameter. The time stamps are calculated in a database query.
My expression looks like this:
=SWITCH (
Parameters!report_type.Value = 1,First(Fields!daily_start.Value, "Timestamps")
,Parameters!report_type.Value = 2,First(Fields!weekly_start.Value, "Timestamps")
,Parameters!report_type.Value = 3,First(Fields!monthly_start.Value, "Timestamps")
)
Unfortunately I get the error message:
A value expression used for the report parameter 'time_from' refers to a field. Fields cannot be used in report parameter expression
I know, that this is not allowed because SSRS cannot be sure in which order datasets are called. But I think this is not dangerous.
All time stamps are received by query without parameter. The parameter report_type is selected by a user before the report will be generated.
Can someone give me a hint for a workaround?
Here's the workaround - get the value using SQL.
Create a new Dataset called StartDates:
SELECT CASE
WHEN #report_type = 1 THEN daily_start
WHEN #report_type = 2 THEN weekly_start
WHEN #report_type = 3 THEN monthly_start
END AS StartDate
FROM MyTable
You already have the #report_type and #time_from parameters. With the #time_from parameter, set its Default Values to Get values from a query using the StartDates dataset and the Value field StartDate.
Now, you'd think this might be enough to make it work - you're referencing this query as the default value and as you change the #report_type parameter the other parameters refresh, but the first date in the #time_from parameter never changes. That's because the refresh happens on the Available Values query, not on the Default Values query.
So you also need to wire up the Available Values query to the StartDates query. Now your query will fire on the change of #report_type and the default value will be set to the appropriate date for your selection.
I switched from a query to Stored Procedure and was getting this error. Things I tried:
Ensured I had sufficient permission on the database (you need EXEC rights or DBO to run teh sproc)
Delete the existing parameters (and then use refresh fields to refresh/get the correctly named ones back)
Remove the square brackets around the stored procedure if you've specified that
Sometimes, Expressions can get a bit verbose. I have created a Report Code Function and then used that as the Parameter Value.
For example, I created a Code function called "CalculateDateSet" and then set the Report Parameter to this expression:
"=Code.CalculateDateSet(Parameters!Month.Value, Parameters!Year.Value"