473,497 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Part2: Using DLookUp to prevent duplicates in Form

50 New Member
{{ Split from: Using DLookUp to prevent duplicates in Form }}

@alexrubio
Hi all,

I was just tasked at tweaking the above macro and again I cannot find the right combination... I need to put an exception to a particular location. In other words the macro should alert to duplicate locations unless the location is equal to "17LR". The above works perfectly in another part of the database, here is what I tried:

Expand|Select|Wrap|Line Numbers
  1. DLookUp("[OfficeLocation]","[tblEmployeeInfo]","[OfficeLocation] = Form.[OfficeLocation] And [OfficeLocation]<>17LR") Is Not Null
And it just does not prompt for ANY duplicates anymore, I thought maybe it was because of the "LR" in the location, so I tested with "1752" and the same results, I also tried "Or" instead of "And", that will prompt for the dupes, but when I select "1752" it does not allow me to save the record and/or move to next record, I have to cancel the changes...

Thanks again for your help!!!
May 4 '15 #1
7 2122
zmbd
5,501 Recognized Expert Moderator Expert
I really dislike that all of the examples show to use the function with the where clause built in place as it make the programmer's life more difficult.

Why you might ask, because you cannot check how the string is actually resolving!

Instead, I suggest a slight modification to make troubleshooting the code so much easier as most of the time the issue is with something missing or not resolving properly/as expected within your string.

So to use your code:
Expand|Select|Wrap|Line Numbers
  1. DIM strSQL as string
  2. strSQL = "[AssetTag1] = Form.[AssetTag1] And [Active]<>0"
  3. '
  4. 'now you can insert a debug print here for troubleshooting
  5. ' - press <ctrl><g> to open the immediate window
  6. ' - you can now cut and paste this information for review!
  7. '
  8. debug.print "Your criteria = " & strSQL
  9. '
  10. 'now use the string in your code:
  11. (DLookUp("[AssetTag1]", _
  12.    "[tblEmployeeInfo]", _
  13.    strSQL )) _
  14. is not null
  15.  
Your where string also looks a bit off...
Expand|Select|Wrap|Line Numbers
  1. "[AssetTag1] = Form.[AssetTag1] And [Active]<>0"
Did you intend (I've stepped this here just to put emphases on the data being pulled from the form and added grouping to help with the logic flow):
Expand|Select|Wrap|Line Numbers
  1. "([AssetTag1] = " & _
  2.    Form.[AssetTag1] & _
  3.    ") And (" & _
  4.    [Active] & "<>0)"
so that the string would return values from the form?

If you will make these little changes and post back the resolved string we can help you tweak the code.

The other thing of note, it looks as if you are using the DLookup() directly in the if..then conditional which is fine in and of itself; however, once again it can make trouble shooting difficult. I would personally assign you function to another variable and test against that in your conditional, I usually use the NZ() around DLookup() functions and return something logical rather than a potential zero-length string (aka null) or zero when I need to be sure that there is no useable result being returned.

as for why your code is hanging... I suspect it has to do with your function not returning anything useable...

Place a STOP command at the start of your code and step thru it using [F8]... you're most likely in a loop; however, without that code, we won't be able to explicitly solve that problemwhich
May 4 '15 #2
alexrubio
50 New Member
Hi zmbd,

Thanks for your response, this is a macro I'm working with... The original post had both the working code and the above which was a tweaked version of it. I would also rather leave the post unsplit so that you can get the background of the story thus making more sense...?

Thanks Again...
May 4 '15 #3
zmbd
5,501 Recognized Expert Moderator Expert
alexrubio:
+ The link to the prior thread should provide continuity.
++ the last post in the original thread was asking for the code you were using at that point for that question as your prior post said that code was not working
++ You're asking a second question, albeit closely related to the original question, yet it doesn't follow the discussion in the original thread at that point.

+ The threads can be merged again if needed.

Just a point if interest:
In Access "Macro" is not the same as "VBA."
I understand that in Excel and Word, these are one and the same; however, MS, in their infinite wisdom, when it comes to Access there is a great difference between Macro programing and VBA programing. One major difference, is that when programing for a SharePoint site, VBA isn't supported for the Forms posted to the site (and I have no kind words for that) nor is the normal method of establishing table relationships. However, that is a horse of a different colour.
May 4 '15 #4
alexrubio
50 New Member
Ok, problem fixed, I was missing single quotes around "17LR" since the field is a text field, grrr, they always get me...

So the working macro code is:

Expand|Select|Wrap|Line Numbers
  1. 1.DLookUp("[OfficeLocation]","[tblEmployeeInfo]","[OfficeLocation] = Form.[OfficeLocation] And [OfficeLocation]<>'17LR'") Is Not Null
Thanks for your help zmbd, appreciate the suggestions.
May 4 '15 #5
zmbd
5,501 Recognized Expert Moderator Expert
ahh.. the dirty little text fields...
This is precisely why I build those strings in the variable first so that you can see what is being returned.

You might find this tool to be helpful...
{Allen Browne's ElookUp}
There's a link there that leads to subqueries... also a very useful tool to have in the toolbox

BOL...

-z
May 4 '15 #6
alexrubio
50 New Member
@zmbd
Thanks again zmbd, will look into that in future projects...!
May 4 '15 #7
NeoPa
32,556 Recognized Expert Moderator MVP
I've changed the selection of Best Answer as the original doesn't fit the criteria. If anything was most helpful it was the first reply which pointed the user where to look to find their problem.
May 6 '15 #8

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

Similar topics

4
13619
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
3046
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
6570
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
1522
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
39341
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
2531
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...
3
2424
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
3399
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
2744
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
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6878
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
7373
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...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.