MS Access 2010 value of MemberID where FinishDate = NULL - ms-access

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")

Related

Table showing "Wrong" Record, but Query returns "Correct" data?

Alright, so here's what happened :
I had a wide table, that needed to be a long table. I used the code below (CODE 1) to fix that problem:
It seemed to have worked, though I am now finding minor errors in the data, while I will need to resolve those, that isn't what this question is about.
In the end, my table looks correctly, and here is an actual record from the database, in fact it is the record that called my attention to the issues:
tbl_CompletedTrainings:
ID
Employee
Training
CompletedDate
306
Victoria
Clozaril
5/18/2016
306
20
8
5/18/2016
the second row is to show what the database is actually seeing (FK with both Employee and Training tables) Those tables have the following formats:
tbl_employeeInformation:
ID
LastName
FirstName
Address
Line2
City
State
Zip
Site1
Site2
Site3
20
A.
Victoria
6 Street
City
State
00000
3NNNN
4
Eric
A.
15 Street
City
State
00000
3nnnnn
tbl_Trainings:
AutoID
TrainingName
Expiration
6
Bloodborne
Annual
8
Clozaril
Annual
When the query in (CODE 2) is run on this table, the following record is returned
report Query:
LastName
FirstName
Training
CompletedDate
Site1
Site2
Site3
ID
Accccc
Eric
Bloodborne Pathogens
5/18/2016
3NN-NN
N/A
N/A
306
Notice that the ID in the report Query is only there as I was checking records, and is called from the tbl_CompletedTrainings. So here's the question, What is happening?! If the record was just wrong, and not pulled I could understand it, but that's not what's happening. Worse still is the date is the correct date for the training the query returns, but not for the training listed in the table.
Related issue, possibly, I had noticed that when I queried the table with a call on the foreign key, it returns records that are 2 off of the requested training number. Notice that this is the case here as well. The training listed, Clozaril, is exactly two further down the line than the training Bloodborne Pathogens, Key 8 and 6 respectively.
Any help would be very much appreciated in this matter, as I can't seem to catch what is causing the issue. Yet it must be something.
(CODE 1)
Option Compare Database
Option Explicit
Sub unXtab()
On Error GoTo ErrHandler
Dim db As DAO.Database
Dim rsxtab As DAO.Recordset
Dim rsutab As DAO.Recordset
Dim counter As Integer
Dim loopint As Integer
Dim qryGetNameID As DAO.Recordset
Dim qryGetTrainingNameID As DAO.Recordset
Dim expires As Date
Dim namevar As String
Dim lname As String
Dim fname As String
Set db = CurrentDb
Set rsxtab = db.OpenRecordset("SELECT * FROM [Employee Training Log];")
If Not (rsxtab.BOF And rsxtab.EOF) Then
db.Execute "DELETE * FROM tbl_CompletedTrainings;"
Set rsutab = db.OpenRecordset("SELECT * FROM tbl_CompletedTrainings WHERE 1 = 2;")
counter = rsxtab.Fields.Count - 1
Do
For loopint = 2 To counter
namevar = rsxtab.Fields(loopint).Name
lname = rsxtab("[Last Name]")
fname = rsxtab("[First Name]")
Select Case namevar
Case "First Name"
Case "Last Name"
Case "Date of Hire"
Case Else
If rsxtab.Fields(loopint) <> "" Or Not IsNull(rsxtab.Fields(loopint)) Then
Set qryGetTrainingNameID = db.OpenRecordset("SELECT AutoID FROM Trainings WHERE [Training Name] = " & Chr(34) & namevar & Chr(34) & ";")
Set qryGetNameID = db.OpenRecordset("SELECT ID FROM tbl_EmployeeInformation WHERE LastName = " & Chr(34) & lname & Chr(34) & _
" AND " & Chr(34) & fname & Chr(34) & ";")
rsutab.AddNew
Debug.Print lname
Debug.Print fname
Debug.Print namevar
Debug.Print qryGetNameID.Fields(0)
Debug.Print qryGetTrainingNameID.Fields(0)
rsutab.AddNew
rsutab("Employee") = qryGetNameID.Fields(0)
rsutab("Training") = qryGetTrainingNameID.Fields(0)
rsutab("CompletedDate") = rsxtab.Fields(loopint)
rsutab.Update
End If
End Select
Next loopint
rsxtab.MoveNext
Loop Until rsxtab.EOF
End If
exitSub:
On Error Resume Next
rsxtab.Close
rsutab.Close
Set rsxtab = Nothing
Set rsutab = Nothing
Set db = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & " : " & Err.Description & vbCrLf & vbCrLf & " on this field: " & namevar, vbOKOnly + vbCritical
End Sub
(CODE 2)
SELECT EI.LastName, EI.FirstName, T.TrainingName AS Training, CT.CompletedDate, EI.Site1, EI.Site2, EI.Site3, CT.ID
FROM tbl_EmployeeInformation AS EI
INNER JOIN (tbl_CompletedTrainings AS CT
INNER JOIN tbl_Trainings AS T ON CT.Training = T.AutoID) ON EI.ID = CT.Employee;

MS Access Invalid use of Null (Error 94)

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.

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.

Run-time error '13' Type Mismatch MS Access VBA array

Have been trying to insert a Street, Suburb and a postcode into a table but keep getting
Run-time error '13'
Type mismatch
Here is the code:
Sub arrayData()
Dim CustomerNames() As Variant
Dim num As Long, dbs As Database, InsertReocrd As String
Dim CustomerID As Long, num1 As Long
Dim CustomerName As String
Dim Street As String, Suburb As String, Postcode As Integer
Set dbs = CurrentDb()
CustomerID = 0
For num1 = 0 To 50000
CustomerID = CustomerID + 1
CustomerNames = Array(...)
Street = Array("Short", "Lygon", "Flinders", "Elizabeth", "King") //ERROR OCURRS HERE
Suburb = Array("Sandringham", "Brighton", "St Kilda", "Melbourne", "Carlton") //ERROR OCURRS HERE
Postcode = Array("3165", "3298", "3145", "3144", "3000") //ERROR OCURRS HERE
num = Int((250 - 0 + 1) * Rnd + 0)
CustomerName = CustomerNames(num)
InsertRecord = "INSERT INTO CUSTOMER (CustomerID , CustomerName, StreetName, Suburb) VALUES (" & "'" & CustomerID & "'" & "," _
& "'" & CustomerName & "'" & "'" & StreetName & "'" & ")"
dbs.Execute InsertRecord
Debug.Print CustomerID, CustomerName, Street, Suburb
Next
End Sub
Variables that should hold arrays need to be declared as Variant, not as String.
So:
Dim Street As Variant, Suburb As Variant, Postcode As Variant
Note that your INSERT statement is missing the value for Suburb.
Using DAO.Recordset.AddNew etc. would probably be faster and better readable.
As shown yesterday, simplify your concatenation, and you also need and array for the CustomerID and a loop as well:
For n = 1 To 5
InsertRecord = "INSERT INTO CUSTOMER (CustomerID , CustomerName, StreetName, Suburb) VALUES (" & CustomerID(n) & ",'" & CustomerName(n) & "','" & StreetName(n) & "','" & Suburb(n) & "')"
If you really wish to insert 50000 customers this way(?) (you wouldn't), follow the advice from André and use DAO.
Sorry if this doesn't exactly fit the topic. Just in case any other poor fool spends hours trying to figure out why they're getting a 'Type Mismatch' on a VBA recordset.addnew, check the [Default Values] of the fields/columns in the table. I changed a TEXT field to a DATE field, but neglected to remove the "" in [Default Value]. (trying to set any text value to a date field is a no-no).

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