Dlookup not evaluating double criteria properly - ms-access

I'm using this code to do a Dlookup with two criteria. But it returns values even if both criteria are not matched in the same record. Note that if I put a value instead of 'heat' that does not exist at all in the table then Dlookup does not return a value. I seams it's looking for the criteria separately and not combined in the same record.
I tried this
DLookup("[Risk ID]", "RA", "[Hazard Category] ='Heat' And [Safety Tag number] = " & SafetyTagNbr)
and This
DLookup("[Risk ID]", "RA", "[Safety tag number] = " & SafetyTagNbr & " AND [Hazard Category] ='Heat'")

For DLookup() you need to insert:
DLookup( "ColumnName", "TableName", "Criteria")
Your syntax is correct, but only if [Safety Tag number] is a Number. If not, then you have to use for your second criteria a String:
DLookup("[Risk ID]", "RA", "[Hazard Category] ='Heat' And [Safety Tag number] = '" & SafetyTagNbr & "'")
If you have more records in your table which statisfie your criteria, then DLookup() returns the first record he finds in the table.

Related

MS access update query issue with date range

I'm using this update query in MS access 2016
Update sampledata set VALUE= '" & Me.value & "' where Day between '" & Me.dayfrom & "' and '" & Me.dayto & "';
The strangest problem m facing is- It is considering the form values for start and end date but however updates records only for the start date. Example. If dayfrom is 01-Nov-2021 and dayto is 30-Nov-2021, the query is updating records of only 01-Nov-2021.
When I pass the day from as 30-Nov-2021, it is updating records for the whole month.
Note: This doesn't happen when I directly pass the values in the query, it happens only when i Pick data from FORM and apply it in query.
UPDATE table3
SET table3.status = "YES"
WHERE (((table3.transactiondate)>=[FORM]![Form3]![startdate] And
(table3.transactiondate)<=[FORM]![Form3]![enddate]));
When you run the query, two pop up input box will open, in the first one, enter the value for start date, e.g 01/01/2022 , in the second one enter the end date e.g 02/28/2022. Do check the date format in use by your system so you can be sure you are entering the date parameter in the right format.
As is, the date values will be casted to strings using your Windows settings.
So, force a format on the string expressions to have true string expressions for the date values:
Sql = "Update sampledata " & _
"Set VALUE = '" & Me!value.Value & "' " & _
"Where Day Between #" & Format(Me!dayfrom.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!dayto.Value, "yyyy\/mm\/dd") & "#;"

What is the DLookup and Nz() format for when field in table is Null?

I have a DLookup formula that fetches the [Model] from MyTable. Everything works until the value for fieldname [Model] is blank.
I get a Runtime Error 94 about how it doesn't understand null information.
I have tried using Nz() function. It is confusing to use these functions when I have so many double and single quotes.
This is what I have.
Dim other as String
other = DLookup("[Model]", "[Part Number & Part Name]", "[Part Number]='" & Forms![Press 2]![Containment - Press 2 subform].Form![Part Number] & "'")
If other <> "" Then
Me.[Model].Value = other
Else
Me.Model.Value = "NA"
End If
Use Nz and reduce the code:
Dim other as String
other = Nz(DLookup("[Model]", "[Part Number & Part Name]", "[Part Number]='" & Forms![Press 2]![Containment - Press 2 subform].Form![Part Number] & "'"), "NA")
Me![Model].Value = other
There are two tricky places for null values in your code:
If Forms![Press 2]![Containment - Press 2 subform].Form![Part Number] is null, the DLookUp will be processed incorrectly, since you're including it as a string.
Fix: use the form value as a parameter, this also avoids SQL injection errors:
other = DLookup("[Model]", "[Part Number & Part Name]", "[Part Number]= Forms![Press 2]![Containment - Press 2 subform].Form![Part Number]")
You're storing the DLookUp result in a string. You can either use Nz here to return a different string, or concatenate a zero-lenght string with the DLookUp result to process a Null value as a zero-length string:
other = VbNullString & DLookup("[Model]", "[Part Number & Part Name]", "[Part Number]= Forms![Press 2]![Containment - Press 2 subform].Form![Part Number]")
That should cover all places where nulls can interfere with this part of code.

Access: filtered form results in to a temp table

I have an Access Form that lists addresses and details related to those addresses. It is filtered manually by staff using the standard menus to come down to a list of relevant addresses.
I'd like to output those filtered results to a table (temp table) so that I can use it to create mailings.
Is this possible and if so what sort of code should I be using for a button.
TIA
MK
When users filtering records, they are changing .Filter property of form. Text of this property is equivalent of WHERE clause of SQL. So, all you need is to create INSERT ... SELECT... SQL query with the same source as record source of form and use filter text for WHERE clause. Something like this:
Dim str_filter As String
Dim str_recSource As String
str_recSource = Me.RecordSource
str_filter = Me.Filter
CurrentDb.Execute "DELETE * FROM MyExportTable"
If str_filter = "" Or Me.FilterOn = False Then
CurrentDb.Execute "INSERT INTO MyExportTable SELECT * FROM (" & Replace(str_recSource, ";", "") & ") as t"
Else
CurrentDb.Execute "INSERT INTO MyExportTable SELECT * FROM (" & Replace(str_recSource, ";", "") & ") as t" & " WHERE " & str_filter
End If

DCount() In VBA Always Returning A Null Value

I am attempting to use the DCount() Function to return a count from my table. My issue is that it always returns a NULL value.
How should I Re-write this VBA statement so that it returns the accurate count?
ReturnedCount = DCount("CountOfItems", "[__TestTable]", "NameOfItem = " & ItemName)
Debug.Print ReturnedCount
NameOfItem implies a string. You need to wrap strings in single quotes when passing them as a parameter to a D-Function; just like passing them as a parameter in a Query.
ReturnedCount = DCount("CountOfItems", "[__TestTable]", "NameOfItem = '" & ItemName & "'")
Using the immediate window to test your D-Functions will simplify debugging.
You should use:
On Error Goto 0
ReturnedCount = DCount("*", "[__TestTable]", "NameOfItem = '" & ItemName & "'")
It will at least return 0 (zero) ... if the table and field names are correct.

Syntax Error Missing Operator VBA

I have a Dlookup in Access 2010 that's supposed to pull a value from a query table that counts the number of checkboxes untoggled. When I run it, it gives me a 'Missing operator in query expression' here.
Countboxes = DLookup("Expr1", "qryCountUntoggled", "[ProjNo =]" & Me.ProjNo & "'")
I can't quite figure out what's wrong.
If ProjNo is numeric, the correct code is:
Countboxes = DLookup("Expr1", "qryCountUntoggled", "[ProjNo] =" & Me.ProjNo )
If ProjNo is text, the code should be:
Countboxes = DLookup("Expr1", "qryCountUntoggled", "[ProjNo] ='" & Me.ProjNo & "'")