I am looking for a simple duplicate check function. User should not be allowed to add record with same first name, last name and address. They are allowed to add record with same first name and last name and a different address. Thanks.
I assumed the recordset was not currently available somewhere and it had to be created.
Public Function ClientExists(sFName As String, sLName as String, sAddress as String, sConnectionString as String) As Boolean
'Determine if the specified Client already exists in the database
Dim sSQL as String
Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset
'Set ClientExists to False
ClientExists = False
'Open Connection
Set cn = New ADODB.Connection
cn.ConnectionString = sConnectionString
cn.Open
'Create SQL Statement
sSQL = "SELECT * FROM [tblTableName] WHERE [LastName] = '" & sLName & "' AND [FirstName] = '" & sFName & "' AND [Address] = '" & sAddress & "'"
'Open recordset
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = cn
.Source = sSQL
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open
'If a match is found the recordsets recordcount will be greater than 0
If .RecordCount > 0 Then
ClientExists = True
End If
.Close
End With
'Close recordset and connection
Set rs = Nothing
I think that's going to work, but I'm having trouble calling the function from a sub. I keep getting Argument not optional error message. Any thoughts would be appreciated. Thanks
It sounds like you are leaving out one of the variables that are required by the function. Make sure you passing all four(First Name, Last Name, Address and the Connection String for the database) variables to the function.
I figured it out. I'd explain what worked exactly but I've been working on this project non stop for a week and my brain is fried. The dupe check function was exactly what I was looking for. Thanks for all your help!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.