Dont allow a duplicate using VBA

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I have code that adds a new record to a table.

I need to run an IIf statement and only do this if a record doesn't already exist with the same field value "PID_Number"


Code:
Set rec = CurrentDb().OpenRecordset("tblRFPManager")
    
    rec.AddNew
    rec("PID_Number") = Me.PID_Number
    rec("Last_UserChangeDate") = Now
    rec("Last_UserId") = GetUserLogin()
    rec("PID_Creator") = GetUserID()
    rec("PID_Owner") = rec("PID_Creator")
    rec.Update
    rec.Close


HELP PLEASE!!!!!
 
Last edited:
This is the part of the code that's highlighted when it errors.

Set rec = CurrentDb().OpenRecordset("select * from tblRFPManager where PID_Number = " & Me.PID_Number)

I assume the message is referring to this as too few parameters - no?
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I was able to replicate the error where I had mis-spelled the field name in the SQL. Is PID_Number the name of your field in your table? Is it a standard number field? (it isn't a lookup field)?
 
Upvote 0
PID_Number is the name. It isn't an actual "number" and it isn't a lookup.

But with your help, I just solved the original problem of avoiding duplicates being entered by the user.
Code:
 If DCount("*", "TblRFPManager", "PID_Number" = [Text139]) = 0 Then
    
        Set rec = CurrentDb().OpenRecordset("tblRFPManager")
    
        rec.AddNew
        rec("PID_Number") = Me.PID_Number
        rec("Last_UserChangeDate") = Now
        rec("Last_UserId") = GetUserLogin()
        rec("PID_Creator") = GetUserID()
        rec("PID_Owner") = rec("PID_Creator")
        rec.Update
        rec.Close
    End If

I used the Dcount in the If statement.
 
Upvote 0
This works fine for me:

Code:
Sub abcdefg()
    Dim rec As DAO.Recordset
    Set rec = CurrentDb().OpenRecordset("select * from TestTable where ID = 1")
    If Not rec.EOF Then
        Debug.Print rec.Fields(0).Value
    End If
End Sub

Nevermind....looks like you got it.
THis would have been helpful to know.
PID_Number is the name. It isn't an actual "number".
You should know that you write code for numbers and text differently when writing raw sql queries containing number or text conditions. And that is why I asked you to test your select query. Also a little unsure why you have changed from PID_Number to [Text139]!!
 
Last edited:
Upvote 0
Thanks, XENOU

Sorry, I didn't know the format of PID_Number would come into play. Good to know

I would have never gotten to a solution without all the help. Thanks
 
Upvote 0
Okay but just to clarify, we are not talking about the format of the field - what matters is the datatype of the field. When you write raw sql text and date datatypes are treating differently:

Code:
select * from Table where X = 1
select * from Table where Y = 'a'
select * from Table where Z = #1/1/2017#
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,327
Members
451,697
Latest member
pedroDH

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