473,416 Members | 1,531 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,416 software developers and data experts.

problem with DLookup code. Getting error that I can't figure out

22
This is a little complicated but I'll do my best to explain. In my db I have a table called L_AgeCorrection which has the following fields: Age, Sex, Frequency, AgeValue

This is a table used to assign an Age Correction value to hearing test results - since some degree of hearing loss naturally occurs with aging - OSHA lets us calculate that in before determining if the employee has an actual "significant" hearing loss.

Anyway...I have my main form F_Demographics with a tabbed control. One of the tabs is for audiograms. This has the current audiogram fields where we enter the date of test, type of test, and test results. This tab also has a subform nested in it to show the employee's baseline audiogram results. I need to compare the current and baseline results but AFTER I correct them for age. So here is an example of how I attempted to do this. On the tab for audiogram I used the After Update event to do a lookup and get the age correction values. I have unbound fields to put the Adjusted values and then unbound fields to later calculate the difference.

But for the lookup table here is the code I came up with...

Expand|Select|Wrap|Line Numbers
  1. Private Sub AudioR6000_AfterUpdate()
  2.  
  3. 'Get Values for current test from L_AgeCorrection table
  4. If Me.AudioAgeL > 60 Then
  5.     Me.Adj2000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=2000")
  6.     Me.Adj3000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=3000")
  7.     Me.Adj4000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=4000")
  8.     Else
  9.     Me.Adj2000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = AudioAgeL and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=2000")
  10.     Me.Adj3000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = AudioAgeL and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=3000")
  11.     Me.Adj4000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = AudioAgeL and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=4000")
  12. End If
  13.  
  14. 'Get values for baseline test from L_AgeCorrection table
  15. If Me.BRAge > 60 Then
  16.     Me.Adj2000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=2000")
  17.     Me.Adj3000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=3000")
  18.     Me.Adj4000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=4000")
  19.     Else
  20.     Me.Adj2000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = BRAge and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=2000")
  21.     Me.Adj3000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = BRAge and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=3000")
  22.     Me.Adj4000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = BRAge and [Sex]=Forms![F_Demographics]!empGender and [Frequency]=4000")
  23. End If
The first part is because the lookup table only goes up to age 60 so if they are over 60 the value is the same as if they were 60.

I think I'm so confused because not all the data fields are on the Audio tab - some of them are on the main form and some of them are on the subform.

Anyway...the error I'm getting is:
Run-time error '2447':

There is an invalid use of the .(dot) or ! or invalid parentheses.

The highlighted line is: If Me.BRAge > 60 Then

Can someone help me troubleshoot my code please? FYI...I had it working in my old 2003 Access db but for some reason I'm getting a hiccup here.

Thanks!
Bekah
Nov 16 '09 #1
15 3081
ajalwaysus
266 Expert 100+
Replace you DLookUp with this:
Expand|Select|Wrap|Line Numbers
  1. DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]='" & Forms![F_Demographics]!empGender & "' and [Frequency]=2000")
I didn't read through your entire post, I just saw this jump out at me, let me know if this fixes your DLookUp.

-AJ
Nov 16 '09 #2
rleepac
22
Unfortunately that didn't do it...
Nov 16 '09 #3
ajalwaysus
266 Expert 100+
@rleepac
Ok I noticed you have a few more places where you have issues,
Expand|Select|Wrap|Line Numbers
  1. DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]=Forms![F_Demographics]!empGender...)
Replace with
Expand|Select|Wrap|Line Numbers
  1. DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex]='" & Forms![F_Demographics]!empGender & "' ...)

Expand|Select|Wrap|Line Numbers
  1. DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = AudioAgeL and [Sex]=Forms![F_Demographics]!empGender...")
Replace with
Expand|Select|Wrap|Line Numbers
  1. DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & AudioAgeL & "' and [Sex]='" & Forms![F_Demographics]!empGender & "' ...)

Expand|Select|Wrap|Line Numbers
  1. DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = BRAge and [Sex]=Forms![F_Demographics]!empGender ...
Replace with
Expand|Select|Wrap|Line Numbers
  1. DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & BRAge & "' and [Sex]='" & Forms![F_Demographics]!empGender & "' ...

NOTE: You need to work on using values like BRAge in VBA. I will look for the article for you and post it unless someone else beats me to it.

-AJ
Nov 16 '09 #4
rleepac
22
Yup. I fixed all instances of it and I get the same error.
Nov 16 '09 #5
ajalwaysus
266 Expert 100+
Try my post #4 now, and let me know.

-AJ
Nov 16 '09 #6
rleepac
22
Ok. Fixed all that and still same error. I'd love to see the article if someone would post it that would be great!
Nov 16 '09 #7
ajalwaysus
266 Expert 100+
While I am looking for that, please post your code as it is now.

EDIT: here is the link, Quotes (') and Double-Quotes (") - Where and When to use them

Thanks,
-AJ
Nov 16 '09 #8
rleepac
22
Ok. Here is what I have now...

Expand|Select|Wrap|Line Numbers
  1. Private Sub AudioR6000_AfterUpdate()
  2.  
  3. 'Get values for current test from L_AgeCorrection table
  4. If Me.AudioAgeL > 60 Then
  5.     Me.Adj2000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=2000")
  6.     Me.Adj3000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=3000")
  7.     Me.Adj4000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=4000")
  8.     Else
  9.     Me.Adj2000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & AudioAgeL & "' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=2000")
  10.     Me.Adj3000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & AudioAgeL & "' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=3000")
  11.     Me.Adj4000CR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & AudioAgeL & "' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=4000")
  12. End If
  13.  
  14. 'Get values for baseline test from L_AgeCorrection table
  15. If Me.BRAge > 60 Then
  16.     Me.Adj2000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=2000")
  17.     Me.Adj3000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=3000")
  18.     Me.Adj4000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '60' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=4000")
  19.     Else
  20.     Me.Adj2000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & BRAge & "' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=2000")
  21.     Me.Adj3000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & BRAge & "' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=3000")
  22.     Me.Adj4000BR = DLookup("[AgeValue]", "L_AgeCorrection", "[Age] = '" & BRAge & "' and [Sex] = '" & Forms![F_Demographics]!empGender & "' and [Frequency]=4000")
  23. End If
Nov 16 '09 #9
rleepac
22
I read the link and I bookmarked it. It's a little confusing but if I read it a few dozen times I'm sure it will start to sink in... Thank you!
Nov 16 '09 #10
ajalwaysus
266 Expert 100+
Ok, one question, what data type is Age? because if it is a number, you need to drop all the single quotes(') around the age operators, if it is a text, you need to add single quotes(') to
Expand|Select|Wrap|Line Numbers
  1. If Me.AudioAgeL > 60 Then
and
Expand|Select|Wrap|Line Numbers
  1. If Me.BRAge > 60 Then
-AJ
Nov 16 '09 #11
rleepac
22
It's a text field. I tried putting single quotes where you suggested but I got an error that said "expected an expression"
Nov 16 '09 #12
ajalwaysus
266 Expert 100+
Well I have hit a wall, I would ask you to upload the DB, but I do not have access 2007, so I will post this on our expert help form if no one has picked this up here soon, to bring more attention to it. Sorry I couldn't help, I hope it is just something I am over looking but I just don't know without seeing the DB.

-AJ
Nov 16 '09 #13
rleepac
22
Actually the AudioAgeL and BRAge are both calculated fields using the date of the test and a datediff calculation. So I guess it is a number field. I pulled the single quotes from the age operators and now I get a different error.

Now I get:
Run-time error '2450':

Microsoft Access can't find the form 'F_TestsAudioBaseline' referred to in a macro expression or Visual Basic code.

The BRAge data is pulled from the subform F_TestsAudioBaseline. Is that where my new problem is?
Nov 16 '09 #14
rleepac
22
Maybe I should start a new thread? I'm wondering if my table set up is part of the problem.

I have a table for audiograms and a table for baseline audiograms. I can't for the life of me figure out why I did it that way. But I should probably just have one table for audiograms and a field that designates if it is a baseline or annual. I'll try messing with that a little and if I can't figure it out then I'll make a new post.

Thanks for all your help!

Bekah
Nov 16 '09 #15
NeoPa
32,556 Expert Mod 16PB
@rleepac
Huh. My article too complicated to read. So harsh. So harsh (J/K of course :D)

Just in case it helps, you may also find Referring to Items on a Sub-Form helps.

For debugging concepts, try Debugging in VBA.

Welcome to Bytes Bekah!
Nov 19 '09 #16

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

Similar topics

9
by: deko | last post by:
When doing a mail merge with Word, I can't seem to get the below (abbreviated) routine to iterate strBookmark01 -- it just uses the first name in the recordset for all 10 (or whatever) documents......
10
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address,...
0
by: Tony Williams | last post by:
I have posted a number of posts in the Access newsgroups concerning my problem with DLookup. I have had a number of the experts with helpful suggestions but I still can't get it to work! This is...
2
by: g.ormesher | last post by:
Hi, my primary key is a field called "ind" 'Code Dim varUP as Varient Dim varIND as Interger varIND = Me.ind ' this sets varIND to the index value
2
by: Dixie | last post by:
Hi, I am tyring to write some generic code that will send the source SQL for a mailmerge to a Word template. I am trying to use DLookup to insert the query name that is the record source for the...
3
by: mmorgan1240 | last post by:
I have a form that I am trying to update. I would like to have a field populated by another table (State_laws) based on the value of another field on the form. I have a field called "state". When...
2
by: chris.thompson13 | last post by:
I am having a problem setting the criteria part of the DLookup method correctly and am consequently getting an error message. I have a database of staff duties, part of which is a query (qryDaily)...
7
by: Simon | last post by:
Can any one see a problem with this code ( it does not work Private Sub cmdEmailOrderPlaced_Click() 'On Error GoTo Error_Handler Dim strTo As String Dim strBody As String Dim strSubject As...
30
by: DH22 | last post by:
Access 03 I'm currently having an issue using Dlookup when trying to reference a query (criteria syntax) Currently I have 1 table, which is L_Emps (which contains Employee_ID as my key...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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,...
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,...
0
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...

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.