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

multiple Dlookups for looking on values in multiple tables

21
I know I can look in two tables with d lookup

What I want is depending the case of what procedure I choose in a combo box, to look its time value in a employees table

timevalproc-The variable to assign the dlookup function in vba

tblEmployees
tblProcedure
[Haircut] or [Eyebrows] = [txt ID]
and the time for each of the procedures which is on tblEmployees:

I have this code as an example, this is what I tried, it's close, but I can't find where is the error

Expand|Select|Wrap|Line Numbers
  1. timevalproc = DLookup("[Haircut Time]", "tblEmployee", "[Employee ID] = '" & [txtID] '" & Dlookup("[Procedure Name],"tblProcedure", "[Procedure Name]= '" & [cboProcedure] & "'") & "'")
the second DLookup will take the place of the where condition of the first so unless it returns zero or null is probably going to be treated as true. it should work, but all it says is that there is a syntax error
Jul 15 '16 #1
24 1291
zmbd
5,501 Expert Mod 4TB
Lets start with:
Allen Browne's Extended DLookup() code
Jul 16 '16 #2
ADezii
8,834 Expert 8TB
  1. Looks like a slight Syntax Error, particularly the ' after [txtID]. This should work:
    Expand|Select|Wrap|Line Numbers
    1. timevalproc = DLookup("[Haircut Time]", "tblEmployee", "[Employee ID] = '" & [txtID] & _
    2.               DLookup("[Procedure Name]", "tblProcedure", "[Procedure Name]= '" & [cbrocedure] & "'") & "'")
  2. Another approach may be:
    Expand|Select|Wrap|Line Numbers
    1. timevalproc = DLookup("[Haircut Time]", "tblEmployee", "[Employee ID] = '" & [txtID] & "'")
    2. timevalproc = timevalproc & DLookup("[Procedure Name]", "tblProcedure", "[Procedure Name]= '" & [cbrocedure] & "'")
Jul 16 '16 #3
CVAR
21
Thank you both for your replies for the solution

Zmbd

1. I'm having trouble trying to adapt the ELookup to my scenario, I know It can evaluate 2 criterias but I can't find any examples to be able to do it


ADezii

2. I tried your first approach and it throws me an error

Run-Time error 2465

"My program can't find the field '|1' refferred in your expression."

3.the second approach throws me an error saying

Run-Time error 3464

"Data type mismatch in criteria expression"
Jul 17 '16 #4
zmbd
5,501 Expert Mod 4TB
+ Error 2465: Tells us that you have a field name that is either misspelled or not in the referenced table

+ Error 3464: Tells us that one of the variables (or fields?) you've used doesn't have the correct data type assigned to it for the value you are attempting to give it.

+ [cbrocedure] bothers me here as this is not how one typically refers to a control on a form and I'm not sure you are really returning the value you think you are; however, from what you've posted, I don't know how this fits in with the rest of your code.

+ I really do not like building the criteria strings within the function as it makes it very hard to troubleshoot if the function is at fault or the string is malformed - most of the time I've found it's a malformed string. Therefore, let's build on ADezii's second approach, with my twist on it:

I've typed the following by hand without the VBA-Editor so there may be a typo (or two)... please feel free to correct :)
Expand|Select|Wrap|Line Numbers
  1.    Dim zProcedureName as String
  2.    Dim zEmployeeID as string
  3.    Dim zRsltProcedureName as string
  4.    Dim TimeValProc as Date
  5. '
  6. 'Stop code here so that we can step thru the code [F8] to
  7. 'see where this chokes
  8. STOP
  9. '
  10.    'let's build the string for the [Procedure Name] look 
  11.    '  up and print to the immedate window <ctrl><g> so
  12.    '    that we can see the result.
  13.    zProcedureName = "[Procedure Name]= '" & [cboProcedure]    & "'"
  14.    Debug.Print "zProcedureName = " & zProcedureName
  15.    '
  16.    zRsltProcedureName = Dlookup("[Procedure Name]", "tblProcedure", zProcedureName)
  17.    Debug.Print "zRsltProcedureName = " & zRsltProcedureName
  18.    '
  19.    'build the second criteria string
  20.    zEmployeeID = "[Employee ID] = '" & [txtID] & zRsltProcedureName & "'"
  21.    Debug.Print "zEmployeeID = '" & zEmployeeID & "'"
  22.    '
  23.    'and run the final lookup
  24.    timevalproc = Dlookup("[Haircut Time]", "tblEmployee", zEmployeeID)
  25.    Debug.Print "timevalproc = " & timevalproc
If you haven't already done so, read thru:
How to ask "good" questions -- Post#2 -- SECTION A You'll need to repeat the Debug/Compile step until it completes without error. The compiler stops on the first error found.
Jul 17 '16 #5
ADezii
8,834 Expert 8TB
Change
Expand|Select|Wrap|Line Numbers
  1. [cbrocedure]
to
Expand|Select|Wrap|Line Numbers
  1. [cboProcedure]
P.S. - My mistake, Typo on my part!
Expand|Select|Wrap|Line Numbers
  1. timevalproc = DLookup("[Haircut Time]", "tblEmployee", "[Employee ID] = '" & [txtID] & _
  2.               DLookup("[Procedure Name]", "tblProcedure", "[Procedure Name]= '" & [cboProcedure] & "'") & "'")
Jul 17 '16 #6
CVAR
21
Hi, I read the post you suggested me, also copy pasted your code, in the line
Expand|Select|Wrap|Line Numbers
  1. zEmployeeID = "[Employee ID] = '" [txtID] & zRsltProcedureName & "'"
Says I have a syntax error, which I can't see, maybe a comma, also, I'm a little curious why you declared timevalproc as Date?, I had it declared as string, does it has to do with the type mismatch problem? I also switched cbrocedure to cboProcedure... but it again, in both cases, throws me the error 3464 Type mismatch, hmm, I'll check the timevalproc declaration, maybe that's the problem
Jul 18 '16 #7
ADezii
8,834 Expert 8TB
I'm not sure if zmbd will agree with me or not but at this point I am pretty much out of options. All Paths appear to have been exhausted, and the only option that I see is to Upload a Copy of the Database stripped of any sensitive information so that I can obtain a first hand look. Whether or not this is viable on your end is up to you. Again, I do not speak for zmdb, he may feel differently.
Jul 18 '16 #8
CVAR
21
ok, but where should I upload the copy of the database, it doesn't have any real or sensitive info, since im still building it
Jul 18 '16 #9
zmbd
5,501 Expert Mod 4TB
CVAR,
Before I would say we need to see your database, please try the version I've posted in Post#5. I still suspect that there is a malformed string issue here and my version will help to pinpoint where this occurring.

We're more than willing to "lend a hand" in this process; however, we really need you to take the lead in solving the issue here at hand - in the long run it will serve you better in that you will be able to apply this knowledge to other situations in the future.
Jul 18 '16 #10
CVAR
21
ok, that's the version I have tried, your post #5 sir, it has a syntax error, and it's true, that I should be looking where the error is, I checked and re checked the code and still no success on finding the syntax error on the statement, I suspect it is on the commas before [txtID] it says, end of statement expected, also, this is my first program in access or any language...:

Expand|Select|Wrap|Line Numbers
  1. 'build the second criteria string
  2.    zEmployeeID = "[Employee ID] = '" [txtID] & zRsltProcedureName & "'"
Jul 18 '16 #11
zmbd
5,501 Expert Mod 4TB
Cvar, I was very careful to indicate that I had typed that in by hand without the editor so there might be a typo. You need to be able to read and understand the code. That is a very simple fix, insert an ampersand between the double quote and the [txtID]

Expand|Select|Wrap|Line Numbers
  1. zEmployeeID = "[Employee ID] = '" & [txtID] & zRsltProcedureName & "'"
Because it's my code I have fixed it in the original posting too.

You really should attempt the simple fixes like this yourself.

With that simple fix in place, please execute the code again.


Please check your Bytes.com Inbox (click) I have forwarded a copy of some links, tutorials, and references that I hope you will find valuable. Of note are the two VBA tutorial links and the link to the "hands-on" Access tutorial.
Jul 19 '16 #12
CVAR
21
Okay, I fixed that after replying to your mail, apparently in the last lookup instruction in your code, there's the choke, a "type mysmatch in criteria expression" there:

TimeValProc = DLookup("[Haircut Time]", "tblEmployee", zEmployeeID)
Jul 19 '16 #13
zmbd
5,501 Expert Mod 4TB
We need to see that resolved string here:
zEmployeeID

Open the VBA editor, run your code, <ctrl><g>
You should see something like:
zEmployeeID = [Employee ID] = '23Haircur'
In the Immediate window that opens - that is something we need to see.
IF, and please only if, the information in this string is confidential you can PM the string to me.

The type mismatch has to be with the [Employee ID] field:
+ Is this a numeric or text field in the table?
+ Is the resolved string for zEmployeeID anything like the data in [Employee ID]?
Jul 19 '16 #14
CVAR
21
The employee id Is a number in the table


ok, the immediate window says '1Haircut''

the 1 corresponds to the employee ID number
Haircut is the type of Procedure

so It seems I got a type mismatch between a number and a string, maybe?
Jul 19 '16 #15
zmbd
5,501 Expert Mod 4TB
"1Haircut" is the issue given that [Employee ID] is numeric.

What we can look at is a criteria that has [Employee ID] and [Procedure Name]; however, we are without the description of your tables.

So if you will list the fields in table [tblEmployee] we can go from there...
Jul 19 '16 #16
CVAR
21
ok my form is linked to tblSchedule, I'll list the fields of both tables just in case....

tblSchedule

Employee ID-Autonumber
EmployeeName-short text
Start-date
Break-date
End-date

fields (that have to do with this issue) on my tblEmployee are

Employee ID-autonumber
Procedure name-short text
Jul 20 '16 #17
zmbd
5,501 Expert Mod 4TB
Remove the " ' " from around the [txtID] as it is a numeric value
Add the criteria against [tblEmployee]![Procedure name], as it is a text field we'll need the " ' " around the value.

Once again, this is typed in by hand; however, it should work, if not then post the resolved string like last time :)
Expand|Select|Wrap|Line Numbers
  1. zEmployeeID = "[Employee ID] = " & [txtID] & " AND [Procedure name] = '" & zRsltProcedureName & "'"
If this does work correctly, the Debug.Print lines can be removed.

One thing I really want emphasize here, building the strings outside of the functions. I know that all of the examples show building the strings within the functions; however, it makes it 10x harder to troubleshoot and near impossible to properly error trap if something goes wrong in the string. Especially in this particular case where one is nesting the functions within each other.

Honestly, I most likely would have setup a recordset against this instead of the nested-DLookups; however, that would depend on how the information was being used in the remaining code.
Jul 20 '16 #18
CVAR
21
Ok, again, got a syntax error, but this time I added a extra ampersand here:

Expand|Select|Wrap|Line Numbers
  1.  '" *&*  zRsltProcedureName & "'"
  2.  
for the syntax error to go....


now after running it, I got a new error that reads,

Runtime error 2471
"the expression you entered produced an error :
[Procedure Name] "

I already checked that's the name in the actual field on the table, it is
Jul 20 '16 #19
zmbd
5,501 Expert Mod 4TB
++ Post the resolve string please... I can do nothing without that information.

++ Also cut and past the line of code you have, there may be another typo in there...
Jul 20 '16 #20
CVAR
21
ok, sorry for not understanding, but if by "resolve string" you mean what the immediate window says then it says this:

Expand|Select|Wrap|Line Numbers
  1. zProcedureName = [Procedure Name]= 'Haircut'
  2. zRsltProcedureName = Haircut
  3. zEmployeeID = '[Employee ID] = '1Haircut''
  4. zProcedureName = [Procedure Name]= 'Haircut'
  5. zRsltProcedureName = Haircut
  6. zEmployeeID = '[Employee ID] = '1Haircut''
  7. zProcedureName = [Procedure Name]= 'Haircut'
  8. zRsltProcedureName = Haircut
  9. zEmployeeID = '[Employee ID] = 1 AND [Procedure name] = 'Haircut''
And this is my code :
Expand|Select|Wrap|Line Numbers
  1. Dim zProcedureName As String
  2.    Dim zEmployeeID As String
  3.    Dim zRsltProcedureName As String
  4.    Dim TimeValProc As Date
  5. '
  6. 'Stop code here so that we can step thru the code [F8] to
  7. 'see where this chokes
  8. 'Stop
  9. '
  10.    'let's build the string for the [Procedure Name] look
  11.    '  up and print to the immedate window <ctrl><g> so
  12.    '    that we can see the result.
  13.    zProcedureName = "[Procedure Name]= '" & [cboProcedure] & "'"
  14.    Debug.Print "zProcedureName = " & zProcedureName
  15.    '
  16.    zRsltProcedureName = DLookup("[Procedure Name]", "tblProcedure", zProcedureName)
  17.    Debug.Print "zRsltProcedureName = " & zRsltProcedureName
  18.    '
  19.    'build the second criteria string
  20.    zEmployeeID = "[Employee ID] = " & [txtID] & " AND [Procedure name] = '" & zRsltProcedureName & "'"
  21.    Debug.Print "zEmployeeID = '" & zEmployeeID & "'"
  22.    '
  23.    'and run the final lookup
  24.    TimeValProc = DLookup("[Haircut Time]", "tblEmployee", zEmployeeID)
  25.  
  26.    Debug.Print "timevalproc = " & TimeValProc
  27.  
  28. Me.Text101 = TimeValProc
Jul 20 '16 #21
zmbd
5,501 Expert Mod 4TB
+ Exactly what I am asking for in the immediate window :-)

+ This is almost always either a misspelled field name or a mismatched data type.

++ 1st course of action:
Open the Table in design view, select and copy the field name there to the clipboard.

Go back to the code and paste the field name into the script. I suspect there's an extra space or missing space in the name as given in the code. This is one reason I never ever use spaces in field or table names - difficult to Trouble Shoot.

See if this runs.

++ Second course of action:

Try the following, IF and ONLY IF the first course doesn't solve the issue.
Removeing the quotes from around the value returned in zRsltProcedureName:
Expand|Select|Wrap|Line Numbers
  1. zEmployeeID = "[Employee ID] = " & [txtID] & " AND [Procedure name] = " & zRsltProcedureName
I am expecting an error here.


In either case, please, replace Line 21 with:
Expand|Select|Wrap|Line Numbers
  1. Debug.Print "zEmployeeID = " & zEmployeeID
Jul 20 '16 #22
CVAR
21
ok I tested both course of actions, none worked, same error 2471 happened. Also, it still highlights me the same line in yellow, this line :


TimeValProc = DLookup("[Haircut Time]", "tblEmployee", zEmployeeID)
Jul 20 '16 #23
zmbd
5,501 Expert Mod 4TB
Is [Haircut Time] part of [tblEmployee]?
It is the only other field referenced in the function.
Double check the spelling, in fact, do the same cut and paste with the field name that I suggested for [Procedure name].
Jul 20 '16 #24
CVAR
21
yes, [Haircut Time] is in tblEmployee, in that sentence it is the only field referenced in the function, it can change depending on what procedure I choose on the combo box

well, in fact it was [Procedure Name] in the table, not [Procedure name},so, I already fixed that in the code...

I've double checked the spelling but no errors in the code neither on what the pop up error msg is suggesting
Jul 20 '16 #25

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

Similar topics

4
by: jeff brubaker | last post by:
Hello, Currently we have a database, and it is our desire for it to be able to store millions of records. The data in the table can be divided up by client, and it stores nothing but about 7...
1
by: E Arredondo | last post by:
Hi, I am about migrate from an old program database to MySql (Running under RH LINUX)and I'm wondering which is the best option to do : I currently have one file for each of my modules, (I'm...
2
by: DC Gringo | last post by:
I have two listboxes, the first of which is an autopostback=true that allows multiple row selection. When I select multiple values (by holding down CTL) in the first one, it should query the...
3
by: Maverick | last post by:
Assume i have 2 tables stored in a single dataSet (ds) and the relationship is well added ds.Tables("product") ds.Tables("descriptors") what I want to do is show the data from 2 of the tables,...
19
by: Shwetabh | last post by:
Hi, I have two tables: Code and Color. The create command for them is : create table Color( Partnum varchar(10), Eng_Color char(10), Span_Color char(20), Frch_Color char(20), CONSTRAINT...
2
by: RoomfulExpress | last post by:
I have 3 different tables in one database and need to display the info off each table in a separate html table. I'm looking to see if my concept is correct. In my code below, I'm not writing all...
4
by: dreaken667 | last post by:
I have a MySQL database containing 16 tables of data. Each table has a different number of columns and there are few common field names accross tables. I do have one master table with which I connect...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
0
by: dave32079 | last post by:
I am moving data from Oracle to MS SQL 2000. I have been given the Oracle table structures and about 130 CVS files to populate these structures, 1 file per table. I have converted the table...
7
by: Quizzed | last post by:
Hi, Using MS Access, '97, we have 4 tables in scope, the content of which is provided by a customer. I need to validate the data in all four tables and one validation routine is focused on a field...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.