473,385 Members | 1,888 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.

How to use DLookup for a String with spaces

I'm having difficulties with using the DLookup function.

I have a form with two textboxes (txtSAP and txtDP) which are bound to a table (tblParts). In these boxes the user can type the partnumber and the corresponding description.

If I press the Create Button the data is saved to tblParts. Now I want to add a functionality: I want to check if a part already exists using the DLookup function.

This is my code for the On-Click event:

Expand|Select|Wrap|Line Numbers
  1. If DLookup("SAPnumber", "tblParts", "SAPnumber = '" & Me.txtSAP & "'") Then
  2.     MsgBox "SAP number already exists!", vbInformation, ""
  3.     Exit Sub
  4. End If
  5.  
  6. If DLookup("PartDescription", "tblParts", "PartDescription = '" & Me.txtDP & "'") Then
  7.     MsgBox "Part description already exists!", vbInformation, ""
  8.     Exit Sub
  9. End If
The first IF works flawlessly. The second IF returns Error 13' : Type Mismatch.

Both Fields "PartDescription" and "SAPnumber" are Text fields. The only difference is that the PartDescription sometimes contains one or multiple spaces. If

It seems that the criteria from the DLookup function ignores the spaces in the String. Is there a solution/workaround for this?
Oct 31 '14 #1

✓ answered by twinnyfo

I have found that I will get errors using DLookup when my search criteria includes an apostrophe (').

If the SAPNumber is a text field but holds numeric characters, it may return a numeric value, which will work in the Boolean world of the If...Then construction. However, if your second DLookup is returning a true text value, then it may be causing the Type Mismatch, as the If...Then is looking for a Boolean result.

Also, It seems odd that you are looking up the same field in your table that you already have. Is this just for demonstration purposes or is there a reason for this? Perhaps a more error free method would be:


Expand|Select|Wrap|Line Numbers
  1. If Nz(DLookup("SAPnumber", "tblParts", _
  2.     "SAPnumber = '" & Me.txtSAP & "'"), "") <> "" Then
  3.     MsgBox "SAP number already exists!", vbInformation, ""
  4. End If
  5.  
  6. If Nz(DLookup("PartDescription", "tblParts", _
  7.     "PartDescription = '" & Me.txtDP & "'"), "") <> "" Then
  8.     MsgBox "Part description already exists!", vbInformation, ""
  9. End If
Also, note that I removed the Exit Sub lines from Lines 3 & 8, as your second block would never run if the part was already found--BUT, I don't know how your tables are set up, so you may want to have those lines in there.

Hope this hepps!

4 14753
jforbes
1,107 Expert 1GB
You can use spaces in a Where Clause for a Dlookup. I would guess that you really are running into a typo. Try adding "Option Explicit" to the top of your code module, then compiling the code.

Also, as a best practice, it's a good idea to use Me.txtDP.Value instead of me.txtDP. You probably wont have trouble with it the way you have, but if you ever get into development with vb.NET vs VBA it will throw an error.
Oct 31 '14 #2
twinnyfo
3,653 Expert Mod 2GB
I have found that I will get errors using DLookup when my search criteria includes an apostrophe (').

If the SAPNumber is a text field but holds numeric characters, it may return a numeric value, which will work in the Boolean world of the If...Then construction. However, if your second DLookup is returning a true text value, then it may be causing the Type Mismatch, as the If...Then is looking for a Boolean result.

Also, It seems odd that you are looking up the same field in your table that you already have. Is this just for demonstration purposes or is there a reason for this? Perhaps a more error free method would be:


Expand|Select|Wrap|Line Numbers
  1. If Nz(DLookup("SAPnumber", "tblParts", _
  2.     "SAPnumber = '" & Me.txtSAP & "'"), "") <> "" Then
  3.     MsgBox "SAP number already exists!", vbInformation, ""
  4. End If
  5.  
  6. If Nz(DLookup("PartDescription", "tblParts", _
  7.     "PartDescription = '" & Me.txtDP & "'"), "") <> "" Then
  8.     MsgBox "Part description already exists!", vbInformation, ""
  9. End If
Also, note that I removed the Exit Sub lines from Lines 3 & 8, as your second block would never run if the part was already found--BUT, I don't know how your tables are set up, so you may want to have those lines in there.

Hope this hepps!
Oct 31 '14 #3
To clearify:

The SAPnumber always consist of two letters and 8 numbers. For instance: ZP12345678. But it never contains a space.

If i already have a part with SAPnumber ZP12345678 in my tblParts and I type the same number (ZP12345678) into my form a message will display: SAP number already exists! If it doesn't exist in the tblParts, the new part will be created.

I also want to check if the description already exists in the tblParts. This description can contain spaces, numbers and letters.

The first part of my code works. If I type an existing SAP number into my form, the message "SAP number already exists!" pops up.

My tblParts is contains:

PartID: Primary Key
SAPnumber: Text
PartDescription: Text
PartPicture: Text (link to the image, nut not relevant in this matter).

Twinny with your code everything works! When I press the Create Button both textfields are checked to see if their values already exist in the tblParts. However it still adds the values to tblParts after prompting the messageboxes. This is why I added the Exit Sub again for each If ... Then clause. This way the user has to retype the SAPnumber and/or description.

Jforbes, Twinnyfo Thanks for the fast responses!
Nov 3 '14 #4
twinnyfo
3,653 Expert Mod 2GB
Jeroen3131,

Glad we could help! Your explanation of why the Exit Sub was helpful, too. Keep plugin' away and let us know if we can help with anything else.
Nov 3 '14 #5

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

Similar topics

3
by: Uttam | last post by:
Hello, Using ADO I have created a table and have also created fields. To create fields, I have used the following: ..Columns.Append "Field_Name", adWChar, 6 I load records into this...
14
by: Thelma Lubkin | last post by:
I am trying to limit the user's options to the choices offered by the combobox text, but I don't want this to extend to how he spaces out the text, e.g. 'abcdefg' and 'ab cd efg' should be...
1
by: Thelma Lubkin | last post by:
I am still struggling with trying to match a user supplied string with strings in a field of a table, where spaces are to be ignored. Arno suggested that I use the Dlookup function, but I...
3
by: Tempy | last post by:
Hi all, i am running into problems with a DLookUp below: =DLookUp("","tblBuyerTotals"," =' " & me!LastName) This is in the Control Source of a text box on the same form. "LastName" is a text...
1
by: Anonieko Ramos | last post by:
> > > How to display multiple spaces in a dropdownlist webform1.aspx <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
0
by: MLH | last post by:
In the following code snippet, notice ("PIN=1234567890&MSSG=Here+is+a+short+message+-+no+spaces+-+just+plusses&Q1=0") I have it that way because I thought PLUSes were required and that spaces...
2
by: Diana | last post by:
I thought I had this process pretty well down pat, but I'm getting a syntax error on the following code: *****Code***** 'Check for users - can't get this to work... Dim strUser As String ...
21
by: Thelma Lubkin | last post by:
I would like my DLookup criteria to say this: Trim(fieldX) = strVar: myVar = _ DLookup("someField", "someTable", "Trim(fieldX) = '" & strVar & '") I don't believe that this will work, and I...
2
by: akoymakoy | last post by:
is there a function to remove leading and trailing spaces on strings? example: word = " THE QUICK BROWN FOX " output: word = "THE QUICK BROWN FOX"
3
by: Elohim | last post by:
Hi to everybody, I'm a learner of C++. I'll really appreciate if you can help me or teach me something. Thank you in advance. #include<iostream> #include<string> int main() { ...
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:
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: 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
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...

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.