Create a numeric-type calculated field using concatenation, in MS-Access - ms-access

I have a column ProjectYear and a column ProjectNumber; both are numbers. I created a calculated column ([ProjectYear] & "" & [ProjectNumber]) which concatenates the two. For instance, 2015 and 123 gives 2015123.
The issue is that the resulting type of that calculated columns is Short Text, and when I create a query to join to another table which has that column in, but as a numeric type, I get a type mismatch error.
How can I make the calculated column have a numeric type?
I tried CInt([ProjectYear] & "" & [ProjectNumber]), but that it is not allowed.

A calculated field expression can only use a limited set of functions. CInt() is not supported, but Int() is.
I tested this one in Access 2010, with Long Integer for the calculated field's Result Type property, and it does what I think you want ...
Int([ProjectYear] & [ProjectNumber])
Note I believe you are asking about a calculated field in table design, such as this ...
Also note that a calculated field can not be indexed. That has performance implications when you use that field in a join to another table --- although the datatypes can be compatible, it can't take advantage of indexed retrieval.

Related

MS Access Calculating date differences if Dates are Short Text

Is is possible in a Table to Calculate differences between Dates if the Value in the field is considered "short text"?
I am working to convert an Excel macro database into Access one and I have imported the data from the Excel file into an Access Table.
however i realized 2 feilds that count up until closure are now just fixed numbers but need to add up as each day passes until closure
when i imported the Dates became Short Text.
is there an expression that would handle this situation?
Each record has a serialized non repeating ID number seperate from access as well.
Dates I have are
OfficialissuanceDate,
DatePlanSubmitted,
DatePlanCompletedSubmitted,
DateClosed,
I need 2 calculations that increments daily when DateplanSubmitted and DatePlanCompletedSubmitted are null
Both comparing to OfficialIssuanceDate. then stop counting when they are no longer null. (have a date in updated to the record) and retain the value.
I have tried to Google calculating Dates but i get DateDiff function which doesnt appear to work. I've used Access and taken a class on it but never really made a new DB from scratch
Dates in a text field are not actual dates, just strings of characters. A Date/Time field stores value as a double number then displays in a date structure - "dd/mm/yyyy" is Access default structure.
Sometimes Access will do implicit conversion of data but not in this case. Either change field type to Date/Time or do conversion with CDate() function. However, you will find that conversion functions error with Null input.
Arithmetic operation with Date/Time field type is possible. However, arithmetic when any term is null returns Null so have to deal with that. One way uses Nz() function: Nz([DateClosed], Date()) - [DateOpened]. Unfortunately, Nz() is not available in table Calculated field type, so do that calc in query or textbox. Most developers avoid table Calculated field type. If you really want to use, expression would have to be: IIf(IsNull([DateClosed), Date(), [DateClosed]) - [DateOpened].

Comparing multiple fields in two datasets to return a 3rd value

I am in report builder and I have my primary dataset that is from a SQL database, I also then created a second dataset (enter data). I need to compare 2 fields from each dataset to retrieve the correct value from the 2nd dataset and populate a column on my report. I have tried the IIF statements and Lookup statements but I keep getting the error "report item expressions can only refer to fields within the current dataset".
I have a attached a screenshot of what I am trying to do....
The IIF statement I tried to use.. If Acctnum and prodid = each other return IncodeNumber
=IIF((Fields!AcctNum.Value=Fields!AcctNum.Value, "IncodeAccount") AND
(Fields!ProdId.Value =Fields!ProdId.Value, "IncodeAccount")),(Fields!IncodeNumber.Value, "IncodeAccount"),"True")
See code in my problem.
You need to use LOOKUP(). The problem with LOOKUP() is that is can only compare a single value from each dataset. However, we can easily get around this issue by concatenating the two values you need to compare.
Note: This assumes the expression will be in a tablix that is bound to your first dataset and that IncodeAccount is your second dataset - the values you want to lookup. If this is not the case just adjust the expression accordingly
So for you, you probably need to do something like this..
=LOOKUP(
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!IncodeNumber.Value,
"IncodeAccount"
)
I've used two pipe symbols to join the values to avoid incorrect matches being found. e.g. Account 123 and product ID 4567 would incorrectly match to Account 1234 and product ID 567 as they would both be 1234567 when joined. By using the || the match would be 123||4567 and 1234||567 respectively.
You may need to convert the values to string using CStr()
Alternative approach
If you are going to do this 'join' multiple times in the same dataset then you could add a calculated column to the dataset that concatenates the two columns. Then you can use this single field in the lookup which will make things a little simpler.
Or, you could do this concatenation in a database view which would make things even easier.

Access 2016 Table Calculated Field

I have a table containing the following: Five Y/N fields and a calculated field [Priority Results] that totals the number of 'Yeses' from those five y/n fields. I'm trying to create another calculated field that will return a value of Low, Medium or High dependent on the number of boxes that have been checked. [Priority Results] currently returns the values 0 through -5. Low = 0 & -1, Medium = -2, High = -3 or lower. I've tried SEVERAL different versions of If/Then, If/Else, Iif statements and always receive a syntax error. I've read a lot of different sites and the following expression seems to be the most commonly used, but I'm still getting the error. Anyone have any ideas? I've even tried this statement on a non-calculated field and can't get it to work.
IIf([Priority results]<="-1","Low",IIf([Priority results]="-2","Medium",IIf([Priority results]>="-3","High")))
Here are the calculated field [Priority results] properties.
Expression:
[Class Non-Attendance]+[Instructor Referral]+[Late Registration]+[Low Starting GPA]+[Talon Log-in]
Result Type: Long Integer
enter image description here
The part of the table this question relates to has the following fields:
Class Non-Attendance: Yes/No
Instructor Referral: Yes/No
Late Registration: Yes/No
Low Starting GPA: Yes/No
Talon Log-In: Yes/No
Priority Results: Calculated field counting the Yes/No fields above
Priority Outcome: Calculated field (that isn't working) prioritizing based on Priority Results
Don't put parameters for number fields in quotes.
Consider:
IIf(Abs([Priority Results])<=1, "Low", IIf(Abs([Priority Results])=2, "Medium", "High"))
In a query or textbox, expression could be:
Switch(Abs([Priority Results])<=1, "Low", Abs([Priority Results])=2, "Medium", True, "High")
Parts of the question still confuse me, which is why this answer will be brief. You have a calculated field PriorityOutcome based on another calculated field PriorityResults and that is the problem. Access doesn't calculate PriorityResults before calculating PriorityOutcome. Instead Access says PriorityResults doesn't exist yet and passes null to PriorityOutcome resulting in either an error or a silent fail.
There are several fixes you can mix and match. You can repeat the calculation for PriorityResults inside PriorityOutcome: wasteful but often the fastest solution. You can also add a code module with public functions to do part or all of the calculations. Then refer to those public functions in your calculated fields Access intellisense can find public functions.

How to use DateDiff in calculated field in access

I am trying to get "No of days late" in a particular table in MS access. I am trying to use calculated data type.
I have [ActualReturnDate] and [ReturnDate] in the same table (both are Date/Time) and I want to save difference between two columns in a calculated field.
I am using following expression:
DateDiff("d", [ActualReturnDate] , [ReturnDate] )
But no matter what i do I get error saying "The expression X cannot be used in a calculated column."
So does that mean I cannot use DateDiff in Calculated field? If not how should I do it?
You indeed can't do this in a calculated field.
Use a query instead, add a column and do the calculation in that column.
You can just add and substract dates.
Just use [ActualReturnDate] - [ReturnDate] as the expression to calculate the difference. If both fields are defined as date/time, the result should be the same, only include the time part as decimal.
If you want only whole days, you can wrap the result in Int()

MS Access, Use Expression Builder to Compare Field in One Table to DLookup in Another Table

I'm trying to make a MS Access report, where I use a text box to display a field value, then have another text box indicating if the first value is higher or lower than an entry in a separate table.
The report has a record source of "Table 1", and a textbox named "txt_Value1" which displays the number in Field: "Value1". I have a second table, "Customer_Criteria" which has a field "PassValue" that I want to compare against. My expression builder statement is:
IIf([txt_Value1]<(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'")),"TRUE","FALSE")
This statement always returns false, regardless of what the correct logical result is.
I've tested it, writing:
IIf(1<(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'")),"TRUE","FALSE")
And I get the correct results. Also, if I write:
IIf([txt_Value1]< 1,"TRUE","FALSE")
I get the correct results. What am I missing to compare the textbox value vs. the Dlookup?
As I understand, both fields are numeric. Access may consider those fields as text, so for correct comparing use type conversion.
Try this:
IIf(CLng(Nz([txt_Value1],0))< _
CLng(Nz(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'"),0)), _
"TRUE","FALSE")
Nz required if fields may contain NULL values, in this case type conversion function will return error.