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

Mismatch error 13 using DLookup

First post here. I am trying to resolve an issue in MS Access using VBA where I am getting an error 13 type mismatch when using the DLookup function.

I am looking for two values one string type and one integer type. The string is using a form control value and the integer is just the specific value not contained in any form control. My code looks like this.

Expand|Select|Wrap|Line Numbers
  1. If DLookup("[ClientName]", _
  2.    "tblTaskInfo", _
  3.    "[ClientName] = '" _
  4.       & Forms!frmLogEntry!ClientName _
  5.       & "' AND TaskID = 1") 
  6.    Then
  7.  
  8. '[{Moderator's note}:Z{I've left the following unstepped
  9. '... to correctly step this would be to fix the code
  10. '... so this is done in the follow-up post}]
  11.       strSQL = "UPDATE tblTaskInfo SET TaskStart = Forms!frmLogEntry!SignedDate WHERE [ClientName] = Forms!frmLogEntry!ClientName AND [TaskID] = 1"
  12.  
If I open the Immediate window and run the function there the ClientName value is returned to me not a null, but when updating the record in the sheet it displays the error and prompts to open the debugger. I have a version of this working fine using ClientNo in place of all ClientName fields, but the problem is in my data the ClientNo may not always be immediately filled out which then causes some of my later Dlookup functions to update and insert records to fail.

Any help would be appreciated.
Sep 1 '13 #1

✓ answered by zmbd

I started to step your code so that it would be easier to read without haveing to scroll thru things. When I started to this on your string in the if-then clause, it appears at first glance to be... well we'll take a look at that in a minute.

First let's take a look at the Dlookup() - there's potentially more than one thing wrong here:

What are you trying to actually return? Right now you appear to be returning a string value (the ClientName) where a boolean expression is required. The conditional must evaluate to a true or false, or be somthing that explicitly converts to a true/false boolean. A numeric is one thing as almost anything other than 0 will evaluate to "true", a string quite another - strings and booleans don't match for type and the compiler will complain about this, and if you get a null, then all bets are off as these are undefined - you could get an error, or you could get the "else" or false branch.

Pet-peeve-#2 on my list, using the Dlookup() directly in a conditional as you can not easily trouble shoot the returned value. This is closely related to:

My Pet Peeve #1 - It Lives, please don't feed it after midnight nor expose it to water at anytime:
Expand|Select|Wrap|Line Numbers
  1. DLookup("[ClientName]", _
  2.    "tblTaskInfo", _
  3.    "[ClientName] = '" _
  4.       & Forms!frmLogEntry!ClientName _
  5.       & "' AND TaskID = 1")
Now it isn't your fault, just about every example shows to build the criteria within the function; however, how are you going to check that the criteria acutally is resolving to the correct value... you can't! The fact that this is shown/taught this way just frustrates me to no end as all to often the error lies within a malformed string resolution!

The solution is simple, build the criteria string first and then use that in the function:

Expand|Select|Wrap|Line Numbers
  1. (...lots of code... - note we have air code here)
  2. Dim z_str_sql As String
  3. Dim z_var_return as Varient
  4. (...lots of code...)
  5. z_str_sql = "[ClientName] = '" _
  6.       & Forms!frmLogEntry!ClientName _
  7.       & "' AND TaskID = 1")
  8. 'Now if needed, you can place:
  9. 'Debug.Print "Sql= " & z_str_sql
  10. 'run the code, press <ctrl><g> and see what the 
  11. 'string resolves to
  12. z_var_return = DLookup("[ClientName]", "tblTaskInfo", z_str_sql)
Checking how this string value resolves would be my first step.

Second, let's take a look at the update string line 11:
strSQL = "UPDATE tblTaskInfo SET TaskStart = Forms!frmLogEntry!SignedDate WHERE [ClientName] = Forms!frmLogEntry!ClientName AND [TaskID] = 1"
I just want to make sure that your form is open when this executes as this string is going to return the form's control names and not the values. Therefor, if the form is closed you should receive a few prompts asking for various information.
Once again, I would make sure that this string is resolving correctly by placing a debug print on the line following it.

1 1855
zmbd
5,501 Expert Mod 4TB
I started to step your code so that it would be easier to read without haveing to scroll thru things. When I started to this on your string in the if-then clause, it appears at first glance to be... well we'll take a look at that in a minute.

First let's take a look at the Dlookup() - there's potentially more than one thing wrong here:

What are you trying to actually return? Right now you appear to be returning a string value (the ClientName) where a boolean expression is required. The conditional must evaluate to a true or false, or be somthing that explicitly converts to a true/false boolean. A numeric is one thing as almost anything other than 0 will evaluate to "true", a string quite another - strings and booleans don't match for type and the compiler will complain about this, and if you get a null, then all bets are off as these are undefined - you could get an error, or you could get the "else" or false branch.

Pet-peeve-#2 on my list, using the Dlookup() directly in a conditional as you can not easily trouble shoot the returned value. This is closely related to:

My Pet Peeve #1 - It Lives, please don't feed it after midnight nor expose it to water at anytime:
Expand|Select|Wrap|Line Numbers
  1. DLookup("[ClientName]", _
  2.    "tblTaskInfo", _
  3.    "[ClientName] = '" _
  4.       & Forms!frmLogEntry!ClientName _
  5.       & "' AND TaskID = 1")
Now it isn't your fault, just about every example shows to build the criteria within the function; however, how are you going to check that the criteria acutally is resolving to the correct value... you can't! The fact that this is shown/taught this way just frustrates me to no end as all to often the error lies within a malformed string resolution!

The solution is simple, build the criteria string first and then use that in the function:

Expand|Select|Wrap|Line Numbers
  1. (...lots of code... - note we have air code here)
  2. Dim z_str_sql As String
  3. Dim z_var_return as Varient
  4. (...lots of code...)
  5. z_str_sql = "[ClientName] = '" _
  6.       & Forms!frmLogEntry!ClientName _
  7.       & "' AND TaskID = 1")
  8. 'Now if needed, you can place:
  9. 'Debug.Print "Sql= " & z_str_sql
  10. 'run the code, press <ctrl><g> and see what the 
  11. 'string resolves to
  12. z_var_return = DLookup("[ClientName]", "tblTaskInfo", z_str_sql)
Checking how this string value resolves would be my first step.

Second, let's take a look at the update string line 11:
strSQL = "UPDATE tblTaskInfo SET TaskStart = Forms!frmLogEntry!SignedDate WHERE [ClientName] = Forms!frmLogEntry!ClientName AND [TaskID] = 1"
I just want to make sure that your form is open when this executes as this string is going to return the form's control names and not the values. Therefor, if the form is closed you should receive a few prompts asking for various information.
Once again, I would make sure that this string is resolving correctly by placing a debug print on the line following it.
Sep 1 '13 #2

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

Similar topics

2
by: Stephen Briley | last post by:
For some reason, my posts are scrubbed as attachments. Lets hope that sending from the yahoo account works. I'm new to Python and I'm trying to do some database work with MS Access, but I can't...
2
by: Steve Briley | last post by:
I'm new to Python and I'm trying to do some database work with MS Access, but I can't seem to get around a "datatype mismatch error".&nbsp; Here's an example table that I'm working with... ...
4
by: Mike | last post by:
I am getting a type mismatch error when I do a bulk insert. ---Begin Error Msg--- Server: Msg 4864, Level 16, State 1, Line 1 Bulk insert data conversion error (type mismatch) for row 1, column...
3
by: Martin Lacoste | last post by:
Is there some issue with using too many left/right/mid/len functions in queries? Depending on the usage, they work fine, but... then there's here: SELECT Master_CAO.Incipit,...
3
by: martlaco1 | last post by:
Trying to fix a query that (I thought) had worked once upon a time, and I keep getting a Data Type Mismatch error whenever I enter any criteria for an expression using a Mid function. Without the...
6
by: shan | last post by:
What is the meaning for the error expression syntax and type mismatch error.I am using turbo c++.can anybody correct the errors in the folowing program. Following program is to find matrix...
1
by: jodyblau | last post by:
I am getting a type mismatch message under strange circumstances. Here's whats going on: 1. I have split the database into a front end and a back end. 2. I have compiled the project. 3. ...
5
by: kjworm | last post by:
Hello Everyone, I have been fighting with a type mismatch error for many hours today and I can't seem to find what the problem is. Hopefully it is more than a missing apostrophe! I have isolated...
4
by: puzzlecracker | last post by:
I am going to post the most relavent code that causes this behavior: Here I try to invoke a static method of TestProgram Class...
5
by: Knowlton | last post by:
I am trying to set the value of a varible using DLookup and keep getting a type mismatch error. Here is what I have: Dim lngPickupTrailer As Long 'get mileage where trailer was picked up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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,...

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.