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

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)

Related

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.

Expression in Textbox not passing value to the table

Obviously I am not an Access expert or I would not be asking this question. I created a small database with several tables. On a form, there are several combo boxes for the user to choose different combinations of medium, paper, sizes, etc. I have already created an expression that returns the correct value I need, but I cannot figure out how to get this value into the correct field on the table to store with the record the form is creating. Below are screen shots of the form and a couple of the tables. I have also included the expression I am using. I need the value that the expression returns to go into tbl1Artwork and populate the ArtWorkSKU field.
Expression:
=Left([PieceName],4) & [cbxArtist].Column & [cbxMedium].Column & [cbxPaperType].Column & [cbxPrintType].Column & [cbxSize].Column
The ArtWorkSKU text box is unbound as I had to type the expression in there. I am not sure if this is the correct way to accomplish the goal. In the tables below, except for the PK, all fields are Short Text.
All guidance is greatly appreciated.
Saving calculated data is usually not necessary and can be risky. Saved value can become 'out of sync' with raw data. Value can be calculated when needed.
Saving calculated data requires code (macro or VBA).
Can be as simple as Me!fieldname = Me.controlname.
Real trick is figuring out what event(s) to put code into. In your case, most likely form BeforeUpdate.
Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention. Better would be ArtworkSKU or ArtworkSKUnum or Artwork_SKU_Num.
Create the following function in VBA:
Function UpdateSKU()
Me.ArtWorkSKU = Left(Me.[PieceName],4) & Me.[cbxArtist].Column(3) & _
Me.[cbxMedium].Column(2) & Me.[cbxPaperType].Column(2) & _
Me.[cbxPrintType].Column(2) & Me.[cbxSize].Column(2)
End Function
Then, on the form, update the After Update event property (not vba) of each "feeder" control to:
After Update: =UpdateSKU()

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.

MS ACCESS formatNumber in Calculated Field

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.

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.