Here is the code that I am using...
My problem is the last line before the end of the subroutine.
Private Sub Month_Year_LostFocus()
Dim sDate As String
Dim dDate As Date
Dim ddate1 As Date
Dim sUnit As String
Dim myBoxDate As Access.TextBox
Dim myBoxUnit As Access.ComboBox
Dim rst As ADODB.Recordset
Set myBoxDate = Me.Controls!Month_Year
Set myBoxUnit = Me.Controls!Unit
'format the date value to mmmm-yyyy
sDate = myBoxDate
dDate = CDate(sDate)
ddate1 = Format(dDate, "mmmm-yyyy"
'Set the value of the combo box to a variable
sUnit = myBoxUnit.Value
'create the recordset to be searched for duplicates
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenKeyset
rst.LockType = adLockReadOnly
rst.Source = "NGSCommunityCommitmentTons"
rst.Open Options:=adCmdTable
'search for the unit # that was entered and the date that was entered.
'If either one exist, clear them out so a new record is not added
'and then close the form. If one of them does not exist, assume that
'this record will be a new one.
With rst
.Find "Unit = '" & sUnit & "'"
If Not .EOF Then
.Find ("Month_Year = " & ddate1)
If Not .EOF Then
msgbox "There is already data entered for this month. Use the " & _
"NGS Community Commitment Update Form.", vbOKOnly
myBoxDate.Value = Null
myBoxUnit.Value = Null
End If
End If
End With
Set rst = Nothing
'Move the focus to the next control so the form can be closed.
Me.Controls!Unit.SetFocus
DoCmd.Close acForm, "NGS Community Commitment Entry Form", acSaveNo
End Sub