Searching for duplicate entries in database via a textfield

cstickman

Board Regular
Joined
Oct 23, 2008
Messages
137
Hello Everyone,

I am probably doing something wrong or have something in the wrong place. I am trying to a DLookup to see if a duplicate number already exists and if it is in the open status. The table name is settlements, the text field is called txtloan1, the columns in the database are loan1 and status. The status field is where it will check for the open status. Below is the code:

<code>
Private Sub txtloan1_AfterUpdate()

If IsNull(DLookup("[loan1]", "settlement", "[loan1] = " & Me.txtloan1.Text & " AND [status] = 'Open'")) = False Then
Cancel = True
MsgBox "Loan Number already exists and is in open status", vbOKOnly, "Warning"
End If

End Sub
</code>

I have it triggered using the after update event within the text field property box. I do not want the user to fill out the form completely just to have it come back as a duplicate error.

Thanks!!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
dont use DLOOKUP.
use the query wizard, choose FIND DUPLICATES.
it will ask you where /when, etc to find
edit the query, put in your own parameters.
 
Upvote 0
Thanks Ranman, but I was able to get the code to work with the following:

<code>
If IsNull(DLookup("[loan1]", _
"settlement", _
"[loan1]=""" & Me.txtloan1.Text & """ AND [status] = 'Open'")) = False Then
Cancel = True
MsgBox "Test Loan1", vbOKOnly, "Warning"
End If
</code>

The problem I am having now is it returns the first value. The loan could have multiple states and with it returning the first one it does not search for the second one. So now I guess I need to use a recordset, but have no idea where to begin to write that into this DLookup. Any thoughts?

Thanks
 
Upvote 0
Umm, if you got it to work, you would not be seeking further help, no? You might have gotten "some" code to do something, but Dlookup will only return ONE value from anywhere, hence Ranman's very appropriate answer.
 
Upvote 0
Or you need a select query to retrieve all occurrences where some field contains some value.
 
Upvote 0

Forum statistics

Threads
1,221,849
Messages
6,162,425
Members
451,765
Latest member
craigvan888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top