473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using DLookUp to prevent duplicates in Form

Hello All,

I've been successfully using the following code:

Expand|Select|Wrap|Line Numbers
  1. DLookUp("[AssetTag1]","[tblEmployeeInfo]","[AssetTag1] = Form.[AssetTag1] ") Is Not Null
In a Macro and basically it looks up the AssetTag field in all records in the database to make sure there are no dupes prior to leaving the field, its set on the Before Update of the respective field.

Now I have been trying to implement the same scenario but now looking at a checkbox, in other words I need to make sure a checkbox's value is True before I perform the above, otherwise ignore it and allow the dupe. Why because I cannot have any dupes on records that are currently set as active (chkActive = True), only on records that are not active (chkActive = False)

I tried adding an If prior to the above stating:

Expand|Select|Wrap|Line Numbers
  1. If [chkActive] = False Then...
But the problem is that it only checks the current active record you are working with and not all records.

If anyone can shed some light on this, would really appreciate it!

Thanks in advance!!!

Alex
Mar 6 '15 #1
19 3088
twinnyfo
3,653 Expert Mod 2GB
Alex,

So, you want it to find out if there is a record with the same AssetTag AND the Checkbox checked? This might work:


Expand|Select|Wrap|Line Numbers
  1. DLookUp("[AssetTag1]", _
  2.     "tblEmployeeInfo", _
  3.     "[AssetTag1] = " & Me.[AssetTag1] & _
  4.         " AND chkActive") Is Not Null
Hope this hepps!
Mar 6 '15 #2
Thanks twinnyfo,

Exactly, but won't the "Me[AssetTag1..." only look at the current record on display and not all records? The form statement is not needed?

Thanks again,

Alex
Mar 6 '15 #3
@twinnyfo
Tried as above and got error:

The object doesn't contain the Automation object 'Me'.
You tried to run a Visual Basic procedure to set a property or method for an object. However, the component doesn't make the property or method available for Automation operations.
Mar 6 '15 #4
twinnyfo
3,653 Expert Mod 2GB
"Me.AssetTag1" is equivalent to "Form.AssetTag1"

But, perhaps the Macro doesn't like the similarity to VBA (which, by the way, it is much easier to manage things in an Access project via VBA than with Macros).

How does this work:

Expand|Select|Wrap|Line Numbers
  1. DLookUp("[AssetTag1]", _
  2.     "tblEmployeeInfo", _
  3.     "[AssetTag1] = " & Form.[AssetTag1] & _
  4.         " AND chkActive") Is Not Null
It is the same as your original code, but adds the chkActive criteria.
Mar 6 '15 #5
@twinnyfo
Fails with macro error 2001, could it be because we are not telling it if chkActive is True or False somewhere?

If I don't do it using a macro, how else can we check as soon as the value is chosen?
Mar 6 '15 #6
twinnyfo
3,653 Expert Mod 2GB
if chkActive is a checkbox (Yes/No Field), then using just the field name evaluates to its value (that is, if the field is true, then it returns true). How this works in a criteria expression is that you are trying to find a record that has a particular AssetTag. When the engine finds that tag, the expression "AssetTag1 = 37" (as an example) evaluates to "True". When you add the statement "AND chkActive", if there is arecord with AssetTag of 37 and the check box checked for chkActive, the statement "chkActive" evaluates to "True". Thus you have "True AND True" which evaluates to "True".

This abbreviated method just saves typing, but is a good habit to get into, because lets say you had a check box and every time you click a button, you want the check box to change its value. Thus, you have two ways of doing this:

Expand|Select|Wrap|Line Numbers
  1. If Me.chkIndicator = True Then
  2.     Me.chkIndicator = False
  3. Else
  4.     Me.chkIndicator = True
  5. End if
OR You could simply say:

Expand|Select|Wrap|Line Numbers
  1. Me.chkInidator = Not Me.chkIndicator
To you, which is easier to write? I know--more than you probably wanted.

Concerning the next question...........

Instead of using a macro, create an event procedure in the same BeforeUpdate Event on the form. VBA and Macros have many similarities, but VBA is much more powerful and flexible and (I think) easier to write and troubleshoot....
Mar 6 '15 #7
@twinnyfo
Thanks for your explanation, I don't think it will work in my scenario because I have about 7 fields to check, each of them check each other for dupes...

Now, I tried this on the macro:

Expand|Select|Wrap|Line Numbers
  1. DLookUp("[AssetTag1]","[tblEmployeeInfo]","[AssetTag1] = Form.[AssetTag1] And [chkActive]= -1") Is Not Null
And it worked fine, but it only checks the current record open on the screen, NOT all records, as I need. I also used "-1" because for the life of me I always have trouble placing the single and double quotes on the correct place, hehe... If not I would rather use "True" instead.

I seem to be almost there, but just need that last push, hehe, to get this working...
Mar 6 '15 #8
twinnyfo
3,653 Expert Mod 2GB
I still don't understand:
And it worked fine, but it only checks the current record open on the screen
The very definition of using DLookUp is that it searches all records in the Data source specified, in your case, tblEmployeeInfo. But, it is looking for the value of AssetTag1, which is found on your form. If your Form is bound to the Table, and the current record is shown, and it is the only record that has that particular AssetTag, then it will find only that record. The way you have explained it, you are LOOKING for the record that has that record's AssetTag.... I don't know how else you intend to do it.
Mar 6 '15 #9
@twinnyfo
It is not doing it, because when I set the AssetTag1 field of another record, whose chkActive field is "False", and on another record I select the same asset, I get no warning. But if I check the chkActive box on that same record prior to entering the asset, it does warn me...

I've had all of this working perfectly, without the checkbox option of course, for a long time, just need to add the checkbox option....
Mar 6 '15 #10
twinnyfo
3,653 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. And [chkActive] = Form.chkActive
Although I am still confused as to how your macro (as written) works.

To the DB engine, "[AssetTag1] = Form.[AssetTag1]" should be a meaningless expression, as is "[chkActive] = Form.chkActive", as [AssetTag1] must be evaluated against a numerical value (assuming, here....) and [chkActive] must be checking for True or False, which the expression "Form.chkActive" is neither, but, would be evaluated as "True" because it has a value that does not equal -1.

More properly, your expression should be:

Expand|Select|Wrap|Line Numbers
  1. DLookUp("[AssetTag1]", "tblEmployeeInfo", "[AssetTag1] = " & Form.[AssetTag1] & " AND [chkActive] = " & Form.chkActive) Is Not Null
I removed all the line continuation breaks, in case that was an issue in your macro.

All I can say is that to a VBA programmer, everything about the syntax of your original post tells me that it cannot work. But if it works, then that is all that matters.

However, in lieu of all this, the more complex you get with your db, the more you will want to move toward VBA exclusively.
Mar 6 '15 #11
jforbes
1,107 Expert 1GB
What is the Control Source of the chkActive Control?

I doubt that it is chkActive, probably more like Active. You are providing the DLookup with a Where Clause which means you need to provide the Table's Column Names on one side of the expression and the expected value on the other.

Also the Expected Value for the Form confuses me a little, but that may just be due to my lack of experience with Macro Development. I would expect it to be in this format:
Expand|Select|Wrap|Line Numbers
  1. Form![The Form Name]![Control Name]
This might work:
Expand|Select|Wrap|Line Numbers
  1. DLookUp("[AssetTag1]","[tblEmployeeInfo]","[AssetTag1] = Form![The Form Name]!AssetTag1 And [Active]<>0") Is Not Null
  2.  
Lastly, I typically test a true/false, yes/no expression against 0. It works easier that way across platforms as 0 is nearly always False or No, where as the value for True can vary.
Mar 6 '15 #12
@twinnyfo
I tried your above code and it failed with error 2001...

The original code was suggested a while ago by someone in another forum and they suggested I use a macro because its not only checking the table for possible dupes but also the forms other fields prior to saving the record to flag them as dupe as well... I have 7 asset tag fields (AssetTag1, AssetTag2, etc.) each has the original code referencing each other so that no dupes can be entered anywhere... I really did not question why it had to be a macro, but I guess I can try it as an expression and see what the results are without the checkbox and see if it performs the same purpose and then after that, if it works, try to add the checkbox in the loop...
Mar 6 '15 #13
twinnyfo
3,653 Expert Mod 2GB
Yes, this question is a bit confusing to me, also.
Mar 6 '15 #14
@jforbes
Thanks Much jforbes,

Your code failed with error 2001, though you are absolutely right about chkActive, I ALWAYS catch myself making that mistake chkActive is the name of the checkbox, not the table source name "Active"!

But I tried your code again with that change and it did not work, BUT, I fixed the "Active" issue on my previous code and it worked like a champ!

Here is the final code:

Expand|Select|Wrap|Line Numbers
  1. DLookUp("[AssetTag1]","[tblEmployeeInfo]","[AssetTag1] = Form.[AssetTag1] And [Active]<>0") Is Not Null
For some reason it does not like the name of the form in there, "frmEmployeeInfo". Going to try twynnyfo's suggestion and try to get away from the macros, do you think the same code will work outside of the macro.

Again thanks a million to both of you, could not have done it without your help...!
Mar 6 '15 #15
twinnyfo
3,653 Expert Mod 2GB
VBA should have very similar syntax. Both J and I are standing by to assist as needed.
Mar 6 '15 #16
@twinnyfo
Again, Thank you guys a lot for this... I will get this up and running with the macros to make sure it all works, then I will make a copy and try the VBA...
Mar 6 '15 #17
twinnyfo
3,653 Expert Mod 2GB
There is also a way to convert your existing macros to VBA. I have used that in the past. MS does an OK job, but my VBA always required cleaning up after their conversion, just to make it pretty.
Mar 6 '15 #18
@twinnyfo
Ok, I tried the above in VBA and did not work...

I tried with and without form name and obviously the working macro as well...
Mar 6 '15 #19
Brilstern
208 100+
alex,

Can you post your attempted VBA so we can see where any issues are at?
Mar 6 '15 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Carl | last post by:
When using 'name' in the form, it works, when using 'id' it doesn't. Any comments about this? By the way, is this a good method or is it better to use 'getElementById'? Carl <body> <form...
3
by: kjaggi | last post by:
I am trying to either write a trigger or a check constraint to prevent duplicates in my table. There are two columns I need to look at for the duplicates and only one combo value for both columns...
6
by: Miguelito Bain | last post by:
hi everybody- i have a form with 2 fields on it that i want the user to fill out before he or she can save the record, close the record, or move to the next record, etc... here's the code i...
2
by: Gidi | last post by:
hello, i have a mainForm that has few buttons which open different forms. how can i prevent a form that is already open to be open again ( i don't want to close the mainForm)? thanks....
10
jamjar
by: jamjar | last post by:
How do I change the form view using VB after the form is opened? I am using the Switchboard pretty much as the wizard loads it. I've tweaked the appearance and a couple of lines of code but I...
4
by: Mark1978 | last post by:
Hi All Apologies if someone has asked this question before, but I have hunted high and low for the solution. I am relatively new to using access so am not sure if this is possible. I am using...
0
by: liamacheung | last post by:
Hi, I am designing a GUI application using a VC++ 2005 Form template. I have created a thread function external to the class containing the form. I can successfully create the thread and run...
3
reginaldmerritt
by: reginaldmerritt | last post by:
I'm using Dlookup to search a table for the first record it comes across that has a date >= the date i specifiy. e.g. formateddate = format(Me.SelectedDate,"mmddyy") VarX = DLookup("",...
7
by: Cyd44 | last post by:
I am getting a runtime error 2471 on the expression LoginID using Dlookup and dont know how to correct it. My code is:- Dim LoginID As Integer Dim Priv As Variant LoginID = .lngMyEmpID On...
12
by: irslan rafique | last post by:
Driver's database= table Driver' Name= Text value Main Query= name of form Combo 102: Driver's Names are stored here IC No: Numeric value =DLookUp("","","=" & !!) Please assist me
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.