MS ACCESS formatNumber in Calculated Field - ms-access

I have a uID field linked to an auto number field Num that generates unique, custom numbering for each record in a table.
However the Expression used for the Data type keeps returning an error.
If Field1 = "Cat" then field (uID) must return "C" & [Num]. With the number format "000". ie C001, C010, C121 etc
IIf([Field1]="Cat","C" & formatNumber([Num],3) & [Num],"Unknown")
It seems Access doesn't recognise formatNumber used in this manner.

put this code in your textbox control source
=IIf([Field1]="Cat";"C"+Format([NUM];"000");Null)
this will give you the output what you need.

Related

The value expression for the textbox4 refers directly to the field

I have mult dataset and i used expression and set the dataset as well but getting error
" The value expression for the textbox4 refers directly to the field dataex without specifying a dataset aggregate. when the report contains multiple datasets"
= Mid((Fields!Dateex.Value,3,2) + "-" + Left(Fields!Dateex.Value,2) + "-" + Right(Fields!Dateex.Value, 4),"Gas")
Your dataset "Gas" can contain 1 or more records. If you have a textbox that is not inside a tablix or other bound control then you need to tell SSRS how to deal with more than one record.
So, you need to do one of the following depending on your situation.
If textbox4 is in a table/tablix/matrix etc, you need to set the dataset property of the table/tablix/matrix to Gas, you can then remove the "Gas" part from your expression
If textbox4 is NOT part of a table/tablix/matrix etc, AND "Gas" only ever contains 1 record then you can change all your references from Fields!Dateex.Value to FIRST(Fields!Dateex.Value)
If textbox4 is NOT part of a table/tablix/matrix etc, AND "Gas" contains more than one record then you will have to decide how you can identify the record you need, this might mean using a lookup etc. If you get to this point, edit your question and show some sample data and you report design, without this it's difficult to help you.

Can two different criteria be used with DLOOKUP if they have two different data types?

I have a form that tracks the missed questions on a test. It has a linked child form that tracks the individual questions that the person taking the test missed.
I want to, when I enter a missed question number, [QuestionNumber] to have the form prefill in the required retraining [RetrainAssigned] from a table of question numbers (tblTestQuestionsTitles)
On the After Update property of Question Number field of the child form I have the following code:
Dim RLAAssignedX As String
RLAAssignedX = DLookup("[AssocRLA]", "tblTestQuestionsTitles", ("[QuestionNumber] = " & Me.QuestionNumber And "[AssessmentTitle] = " & Me.Parent!Title))
Me.RLAAssigned = RLAAssignedX
I am getting error 13 - Type Mismatch in data expression, and the debug is highlighting the entire line. QuestionNumber and AssessmentTitle are number fields in all tables. Retraining Assigned is a Text Field - but that is the output of the dlookup not a criteria. All I can think of is that the output and the criteria also have to match but that's not matching what I have tried to lookup here and on google.
Since you're referencing values held by the active form, you should be able to reference the values of such fields directly within the selection criteria of the DLookup expression, without the need for concatenation nor consideration for the data type of the field being filtered, e.g.:
DLookup("[AssocRLA]", "tblTestQuestionsTitles", "[QuestionNumber] = [Forms]![YourParentFormName]![YourSubFormName].Form![QuestionNumber] AND [AssessmentTitle] = [Forms]![YourParentFormName]![Title]")
This approach also avoids the potential for SQL injection.
Yes, WHERE argument can include multiple criteria of different data types but you say yours are both number type.
Concatenation is not correct. RLAAssignedX is declared a String variable. If DLookup does not find a match, it will return Null. String variable cannot hold Null and code will run-time error. If Null is a possibility, need to handle that. Actually, don't really need variable.
Me.RLAAssigned = DLookup("[AssocRLA]", "tblTestQuestionsTitles", _
"[QuestionNumber] = " & Me.QuestionNumber & " And [AssessmentTitle] = " & Me.Parent!Title)

Sum Values not equal to a space from a Control Source in MS Access

As the subject expresses, I'm trying to sum the values of a string field where spaces may exist. It must be done this way, unfortunately.
The database is very old. The original developer chose to make all fields Text fields; to get over the null value problems, a function was written in VB6 to replace any null value with a space. This cannot be changed.
Fast forward to now, I'm trying to create a report that sums the length field without changing spaces to nulls first, and it should be done entirely through the control source property of the report.
I've added some of what I've tried below, but every time the report is run, I receive:
Data Type Mismatch
...and I'm not sure how to get around it.
Ideally, I'd like to keep the users out of the database completely, and just add a combo box that lists the reports created in the database so they can be opened by name without having to run any additional update queries first.
=Sum(IIf([MY_LEN]<>" ",DCount("[MY_LEN]","MY_TABLE"),0))
=Sum(Nz(Iif(Trim([MY_LEN])='',Null,[MY_LEN]),0))
=DSum("[MY_LEN]","[MY_TABLE]","[MY_LEN]<>' '")
=Sum(Iif(Val([MY_LEN])>0,[MY_LEN],0))
=(SELECT Sum([MY_LEN]) AS MyLen FROM MY_TABLE WHERE (((MY_TABLE.[MY_LEN])<>' ')))
Is this possible?
Can't compare anything to Null. Can't say If x = Null Then because Null is undefined. So you can't test if undefined = undefined. Use If IsNull(x) Then in VBA and Is Null in query criteria. Don't really need IIf() for Sum() aggregate, other aggregates such as Count or Avg would.
To handle possible space, empty string, or Null for a text field holding numeric data.
=Sum(Val([MY_LEN] & ""))

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.

MS Access Dlookup #Error or ?Name value

I've got a foreign key lookup in the form of the following two tables -
tblApplications
applicationId
applicationName
tblApplicationsManagement
application (references tblApplications.applicationId)
I have a text field on a form that is setup with tblApplicationsManagement which I want to reference the applicationName through the relationship.
I've tried this -
=DLookup("[applicationName]", "tblApplications","[applicationId] = [tblApplicationsManagement]![application]")
And all I get is #Error on the initial Form entry and everything comes up blank.
edit - the following returns #Name? on all entries
=DLookUp("[applicationName]","tblApplications","application=" & [applicationId])
To make sure I'm capturing all the right info. I'm trying to populate a Plain Text field with the [applicationName] from the foreign key. the Text Field name is fieldApplicationName
NOTE for any viewers - The solution is correct. The issue was on the SQL side having a column called "application" which isn't specified as a keyword but it must be somewhere either on the SQL or MS Access side.
If your form includes a text box named txtApplication which is bound to the tblApplicationsManagement.application field in your form's Record Source, this should work as the Control Source for the problem text box ...
=DLookup("[applicationName]", "tblApplications", "[applicationId] = " & [txtApplication])
Note I presumed tblApplicationsManagement.application and tblApplications.applicationId are both numeric datatypes. If they're text, you'll need to add quotes ...
=DLookup("[applicationName]", "tblApplications", "[applicationId] = '" & [txtApplication] & "'")