MS Access Invalid use of Null (Error 94) - ms-access

Can someone please explain why when I remove my last line of code it works but if I add my last line of code I get the ERROR 94
'Variable to set the Event ID too
Dim eventID As Integer
'Variable to set the Event Name too
Dim eventName As String
'Variable to hold the string condition for the Event ID
Dim eventStrCriteria As String
'Variable to hold the Members ID
Dim memberID As Long
memberID = CLng(Me.Text18)
eventStrCriteria = "isNull([check-OutDate]) And (employeeID = " & memberID & ")"
eventID = DLookup("[personelID]", "[tbl_ics238Table]", eventStrCriteria)
MsgBox DLookup("[eventName]", "[tbl_EventInformation]", "[eventID] =" & eventID)

Probably not an answer, more comment but can't yet, but
eventID = DLookup("[personelID]", "[tbl_ics238Table]", strCriteria)
should probably be
eventID = DLookup("[personelID]", "[tbl_ics238Table]", **eventstrCriteria**)
(remove asterix)
That would account for the first dlookup() returning a null (I think), which would then make the second dlookup error out.

Related

Using Null in a DLookup Criteria

I am creating a database to manage the flow of people in and out of an incident system. My idea is to use the DLookup function to allow me to identify a null value in the checkout date. This would allow me to prevent someone from jumping incidents without signing out first.
If it is possible I would like to get the EVENT ID and the COMMAND POST ID so I can create an error message to tell me the incident that is member is still attached to.
'***Section 1 - Ensure member is Checked out and DEMOBed from previous incidents
'******Variables
'*********Variable to hold the Member ID
Dim id As Integer
'*********Variable to hold the checkout status
Dim checkout As String
'*********Variable to hold the Event ID
Dim eventID As Integer
'******Code Block 1 - Check for Null Values
id = Me.Text18
Forms![frm_ics238Table].Refresh
If Not IsNull(DLookup("[eventID]", "[frm_ics238Table]", "[checkoutDate] is Null And employeeID = '" & Me.Text18 & "'")) Then
MsgBox "y"
End If
End Sub
From your original question which right now it doesn't look like you've edited it to indicate the updated code and errors, you had:
id = Me.Text18
Forms![frm_ics238Table].Refresh
If Not IsNull(DLookup("[eventID]", "[frm_ics238Table]", "[checkoutDate] is Null And employeeID = '" & Me.Text18 & "'")) Then
MsgBox "y"
End If
I think these might help:
"[frm_ics238Table]" should be table:"[tbl_ics238Table]"
ID appears to be declared as an integer yet you're using it as a
string. If employeeID is actually a numeric field remove the quotes
around employeeID = '" & Me.Text18 & "'" It should look like this employeeID = " & ID & "
You can also try using IsNull([checkoutDate]) instead of
[checkoutDate] is Null
SO it might look like:
If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", "IsNull([checkoutDate]) And (employeeID = " & ID & ")")) Then
MsgBox "y"
End If
Even better would be to put the criteria into a string variable so you could debug and test it first
Dim strCriteria as String
strCriteria = "IsNull([checkoutDate]) And (employeeID = " & ID & ")"
Debug.Print strCriteria
If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", strCriteria)) Then
MsgBox "y"
End If
EDIT: PROOF OF WORKING CODE:
Not that I don't believe you but I don't believe that it doesn't work if what you've described matches my assumptions.
I tested successfully using assumptions that:
eventID - number field
checkoutDate - date/time field
employeeID - number field
My sample data table
Then I put a command button on a form to test the code:
Private Sub Command8_Click()
Dim strCriteria As String
Dim ID As Integer
ID = 4
strCriteria = "IsNull([checkoutDate]) And (employeeID = " & ID & ")"
Debug.Print strCriteria
If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", strCriteria)) Then
MsgBox "y"
End If
End Sub
Finally - testing the button click resulted in:
In conclusion - if you're still getting an error then you can update
your question with BOTH your code AND your table design.

MS Access 2010 value of MemberID where FinishDate = NULL

Using MS Access 2010
My Table BranchMemberOffice has the following columns:
BranchID
OfficeID
MemberID
FinishDate
Below works but I need the value of MemberID where FinishDate = NULL
strBranchID = Combo221.Column(0)
strOfficeID = 1
strChair = DLookup("MemberID", "BranchMemberOffice", "BranchID = " & strBranchID & " AND OfficeID = " & strOfficeID)
Hope I am clear enough :)
If your table structure is like this (I've not added any Primary Keys):
This code will return MemberID 11:
Sub Test()
Dim MemberID As Variant 'Changed from Long to accept Nulls.
Dim lOfficeID As Long
Dim lBranchID As Long
lOfficeID = 3
lBranchID = 1
MemberID = DLookup("MemberID", "BranchMemberOffice", _
"BranchID=" & lBranchID & " AND OfficeID=" & lOfficeID & _
" AND FinishDate Is Null")
If IsNull(MemberID) Then
MsgBox "No value returned."
Else
MsgBox MemberID & " is the value returned."
End If
End Sub
Edit: Nearly forgot - MemberID in the code above must be defined as a Variant. Keeping it as a Long will return "Invalid use of Null" if the result is Null - a Long can't hold a Null value, while a Variant can.
Edit 2: Have added a message box to show how to deal with Null values.
Try this:
strChair = DLookup("MemberID", "BranchMemberOffice", "FinishDate Is Null")

Getting date from a query which is not showing exact as entered in table

I want to get MAX purchase date from a query.
I have written code which can show date in message box. But problem is that the date is not showing exactly as entered in the table. In the table date is 11-04-17, but in the message box date is shown 30-12-99.
My Code is:
Dim dbforPurdate As Database
Dim rsforPurdate As Recordset
Dim vmedid As String
Dim dtpurtbl As Date
Dim Qryfordate As String
vmedid = Me.MedID
Set dbforPurdate = CurrentDb
'Qryfordate = "SELECT Purchaset.[Purchase Date] AS PurDate, PurchaseDetailt.[Receipt No], PurchaseDetailt.MedID, PurchaseDetailt.BatchNo FROM Purchaset INNER JOIN PurchaseDetailt ON Purchaset.[Receipt No] = PurchaseDetailt.[Receipt No]"
Set rsforPurdate = dbforPurdate.OpenRecordset("Qryfordate")
dtpurtbl = Nz(DMax("[Purchase Date]", "qryfordate", [MedID] = "'" & vmedid & "'"), 0)
MsgBox Format(dtpurtbl, " dd/mm/yy ")
Your DMax() is not finding a value which returns Null and therefore the Nz() returns 0 and the Format() function converts 0 to 30-12-99.
The DMax() shows syntax errors and I am surprised you do not get runtime error. It is missing quote mark in front of [MedID] and has an extra quote mark after = sign.
Is MedID really a text type field?
DMax("[Purchase Date]", "qryfordate", "[MedID] = '" & vmedid & "'")
If you want the message box to not show the funky date value:
MsgBox IIf(dtpurtbl = 0, "No Date", Format(dtpurtbl, " dd/mm/yy "))

Instantiate recordset with ORDER BY Access VBA

I'm trying to create a recordset that is ordered according to a table and the current Orderby property of a certain form. Since the Orderby property will change when the users changes it, I made a function that gets the first field only (It's possible to order it using more than 1 field) in the Orderby property and using it to create an ordered recordset .The field's name given from the getField function is given correctly but I assume that the syntax of "Set rsAll = ..." is incorrect which is what I think is causing the code to stop.
Creating a Recordset in an ordered manner.
Dim rsAll As DAO.Recordset
If Len(Forms("All Patient Sub").OrderBy) > 0 Then
Set rsAll = CurrentDb.OpenRecordset("SELECT * FROM [All Patient Info] ORDER BY " & getField(Forms("All Patient Sub").OrderBy)) 'code stops here
Else
Set rsAll = CurrentDb.OpenRecordset("All Patient Info")
End If
The function that gets the field name from the Orderby property belonging to the form
Public Function getField(ByVal sorter As String) As String
Debug.Print "Started"
'i = 21 hence [All Patient Info].[ is skipped and starts from the field's name
For i = 21 To Len(sorter)
If Mid(sorter, i, 1) = "]" Then
getField = Mid(sorter, 21, i - 21)
Exit For
End If
Next i
Debug.Print getField
End Function
I don't really follow your code; however since you just want the first column in the orderBy, how about this:
This function needs to be put in a module:
'this returns either " ORDER BY [column name]" or an empty string, if there is no column name to order by
public function OrderByClause(propValue as string) as string
dim cols as string()
if propValue = "" then
OrderByClause = ""
else
cols = split(propValue, ",")
dim junk as string: junk = cols(0)
if instr(junk, ".") > 0 then
junk = mid(junk, instr(junk, ".") + 1)
end if
OrderByClause = " ORDER BY " & junk
end if
end function
Then in your form:
Dim rsAll As DAO.Recordset
Dim src as string
src = OrderByClause(Forms("All Patient Sub").OrderBy & "")
src = "SELECT * FROM [All Patient Info]" & src
Set rsAll = CurrentDb.OpenRecordset(src)
Since the OrderByClause function always returns a value, you can just call it in your form regardless of what the property is set to; The invocation uses ... & "" to prevent an error if the property is in fact null.

NZ Invalid Use of Null

I have a form that opens with on OnOpen event. Part of the event contains this code:
nid = Val(DMax("id2", "claims", "yr =" & yr) + 1)
nid = Val(Nz([nid], 1))
But, I get an "Invalid Use of Null" error when it runs.
Any ideas?
EDIT BELOW
Here is the full code:
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Dim lngID As Long
Dim rs As Object
Set rs = Me.Recordset.Clone
lngID = Val(Me.OpenArgs)
rs.FindFirst "[ID] = " & lngID
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End If
Me.LOGID.SetFocus
'Display Suggested ID Numbers
Dim yr As Long
Dim nid As Long
yr = Format(Date, "yy")
nid = Val(DMax("id2", "claims", "yr =" & yr) + 1)
nid = Val(Nz([nid], 1))
Me.SugID = yr & "-" & nid
End Sub
Double-check the value returned by the DMax expression. If no rows match the criteria option ("yr =" & yr), DMax will return Null. Or if id2 is Null in all rows which match the criteria, DMax will return Null.
If the DMax expression returns Null, the Val() expression is equivalent to this:
Val(Null + 1)
But Null + 1 yields Null, so that is the same as asking for Val(Null) which triggers error #94, 'Invalid use of Null'.
I'm unsure what you want instead. If claims.id2 is a numeric data type, maybe this will work:
nid = Nz(DMax("id2", "claims", "yr =" & yr), 0) + 1