Solved If .NoMatch Then , Doesn't work

MaryamF

New member
Local time
Today, 15:12
Joined
Nov 12, 2007
Messages
14
Appreciate your help in advance,

on this program , I can't get the
If .NoMatch Then
part working...
when there is no match I want to get the message says 'welcome".

-----------------------------------------------------------------------------
Private Sub SSNumber_BeforeUpdate(Cancel As Integer)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT * FROM TblePeople WHERE SSN='" & Me.SSNumber & " ' ")

With rst
If .NoMatch Then

MsgBox ("*Welcome*")

End If

If Not .NoMatch Then

MsgBox "SSN: " & Me.SSNumber & " Already Exists and Belongs to:" & vbCrLf & _
!FullName & " " & !DOB & " " & !PhoneNumber, vbExclamation

Me.TenantName = !FullName
Me.TenantName.Enabled = False
Me.FullName.Enabled = False
Me.TenantDOB = !DOB
Me.TenantDOB.Enabled = False
Me.DOB.Enabled = False
Me.TenantPhone = !PhoneNumber
Me.TenantPhone.Enabled = False
Me.PhoneNumber.Enabled = False
.Bookmark = .LastModified

End If

End With

Set rst = Nothing
Set dbs = Nothing

End Sub
 
NoMatch should be used in conjunction with FindFirst / FindNext.
The easiest and quickest way to test for an empty recordset is to check for EOF, as already mentioned.
 
Are you certain, this table exists? TblePeople

Or maybe this:

Code:
Private Sub SSNumber_BeforeUpdate(Cancel As Integer)
   Dim dbs As DAO.Database
   Dim rst As DAO.Recordset

   Set dbs = CurrentDb
   Set rst = dbs.OpenRecordset("SELECT * FROM TblePeople WHERE SSN='" & Me.SSNumber & " ' ")

   With rst

      If Not .NoMatch Then

         MsgBox "SSN: " & Me.SSNumber & " Already Exists and Belongs to:" & vbCrLf & _
            !FullName & " " & !DOB & " " & !PhoneNumber, vbExclamation

         Me.TenantName = !FullName
         Me.TenantName.Enabled = False
         Me.FullName.Enabled = False
         Me.TenantDOB = !DOB
         Me.TenantDOB.Enabled = False
         Me.DOB.Enabled = False
         Me.TenantPhone = !PhoneNumber
         Me.TenantPhone.Enabled = False
         Me.PhoneNumber.Enabled = False
         .Bookmark = .LastModified

      Else
         MsgBox ("*Welcome*")
      End If

   End With

   Set rst = Nothing
   Set dbs = Nothing

End Sub
 

Users who are viewing this thread

Back
Top Bottom