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

How to test if a match isn't found in searching for a record?

Seth Schrock
2,965 Expert 2GB
I have a textbox on my form that allows me to type in a loan number and it will go to that loan. My problem is that if the number that I type in doesn't exist, nothing happens. My initial thought was that an error would occur and I could trap for the error to perform the steps that I want to happen when a match isn't found. But no error occurs for me to trap. I have tried both DoCmd.SearchForRecord and Me.Recordset.FindFirst. The former does nothing and the later just goes to the first record. What can I use to see if no match was found?
Dec 13 '12 #1

✓ answered by TheSmileyCoder

If we work on the assumption that the loan number is unique, it could look like this:
Expand|Select|Wrap|Line Numbers
  1. Private sub cmdSearch_Click()
  2.   If me.txtSearch & ""<>"" then
  3.     Me.recordset.findfirst "LoanNumber=" & me.txtSearch
  4.     If me.Recordset.NoMatch Then 
  5.       Msgbox "Loan number not found"
  6.     End If
  7.   End If
  8. End Sub
While the Dcount will work, its not necessary the best way to go, since it would require a double query of the table, which isn't really called for.

15 2531
Rabbit
12,516 Expert Mod 8TB
You could use a DCount before doing the search.
Dec 13 '12 #2
Seth Schrock
2,965 Expert 2GB
Do you mean DCount the data source of the form using a criteria of the loan number?
Dec 13 '12 #3
Seth Schrock
2,965 Expert 2GB
Well, it does work that way even if that wasn't what you were meaning, although I think that it was. Thanks Rabbit.
Dec 13 '12 #4
TheSmileyCoder
2,322 Expert Mod 2GB
If we work on the assumption that the loan number is unique, it could look like this:
Expand|Select|Wrap|Line Numbers
  1. Private sub cmdSearch_Click()
  2.   If me.txtSearch & ""<>"" then
  3.     Me.recordset.findfirst "LoanNumber=" & me.txtSearch
  4.     If me.Recordset.NoMatch Then 
  5.       Msgbox "Loan number not found"
  6.     End If
  7.   End If
  8. End Sub
While the Dcount will work, its not necessary the best way to go, since it would require a double query of the table, which isn't really called for.
Dec 13 '12 #5
NeoPa
32,556 Expert Mod 16PB
Why not use an unbound ComboBox instead, and let the operator search from a list of known valid values? The ComboBox can be populated with the valid data as part of its design.
Dec 13 '12 #6
Seth Schrock
2,965 Expert 2GB
That is what I was looking for! Thanks Smiley.

This is a little off subject but deals with the code you gave me, so I'll leave it to you (as moderator) to decide if it should be moved to a different thread.
Recently, I've been given several examples of code (including yours) that doesn't give an = in the If/Then statement. I would really like to understand why you are able to test
Expand|Select|Wrap|Line Numbers
  1. If Me.txtSearch & ""<>""
without giving an Me.txtSearch = ... or Not IsNull(Me.txtSearch). I really have no idea what to Google or anything like that so I can't search it on my own.
Dec 13 '12 #7
NeoPa
32,556 Expert Mod 16PB
There is no requirement for an equals (=), or any other equality operator. What is required is anything that results in a Boolean value.

Rushing now, but will return later for a fuller explanation.
Dec 13 '12 #8
TheSmileyCoder
2,322 Expert Mod 2GB
It probably should be moved to another thread, or better yet I could write a short insight about it, but for now:
I am checking to see if something has been entered into the control.

Now in many cases it should be sufficient to simply check the value for null, using IsNull(Me.txtSearch) which will work for comboboxes as well. One thing to note is that during most normal use of a database a text field will either contain Null or a string of length >0. If you clear out a control (delete all characters) access will not store a zero-length string in the field, it will store a null.

However, I had issues in which input being imported from excel would sometimes result in zero length strings being stored in the database. (It can also be stored through use of code)

In those cases the IsNull was suddenly useless. I therefore started to use the notation I use now:
Expand|Select|Wrap|Line Numbers
  1. If Me.txtSearch & ""<>"" then
In cases where txtSearch is null, the concatenation with the zero length string will yield a zero length string. In cases where it is a zero length string it will also yield a zero length string, and in all other cases it will yield a string different from a zero length string. This also works for comboboxes.
Dec 13 '12 #9
NeoPa
32,556 Expert Mod 16PB
When using the If statement it's important to realise that it is not :
If <SomeValue> <EqualityOperator> <AnotherValue> Then

It is rather :
If <condition> Then

<condition> can be one of a great nuber of expressions or values. All that is necessary is for it to result in a value that can be considered as a Boolean. Specifically, it is not necessary to put comparisons of any kind in here, even if they are, by far, the most common conditions. When you already have a Boolean value, stored in a Boolean variable, there is absolutely not need to convert it into a Boolean value by comparing it either to True or False. So, the following is totally unnecessary (and technically doesn't even do exactly what is expected of it anyway - but we won't go there for now) :
Expand|Select|Wrap|Line Numbers
  1. Dim blnFlag As Boolean
  2.  
  3. ...
  4. If blnFlag = True Then ...
The most sensible way to write this is simply :
Expand|Select|Wrap|Line Numbers
  1. If blnFlag Then ...
In the specific case of checking form controls to see if they have any contents, it is only necessary to compare the control to an empty string. As Smiley says, it is possible for empty controls (depending on the design of your tables and fields - I always try to ensure empty strings are never allowed anywhere) to have Null or an empty string representing their .Value. Code to handle that is simply :
Expand|Select|Wrap|Line Numbers
  1. If Me.MyControl > "" Then ...
This will result in a True condition only when neither Null nor "" is contained therein. Essentially when it has an actual value of some sort (any sort). Many people prefer to use <>, as this is generally recognised as the inequality operator and that is a better fit for how they think of it, but technically > works just as well. Please understand that for those that think that way, <> is a more sensible option than >. How you understand what you write is important.
Dec 13 '12 #10
Seth Schrock
2,965 Expert 2GB
Okay, I understand how
Expand|Select|Wrap|Line Numbers
  1. If blnFlag Then...
  2.  
  3. 'same as
  4.  
  5. If blnFlag = True Then...
I also understand the last code block that NeoPa posted as that has the equality operator that I would expect. However I still don't quite Smiley's. Me.txtSearch is not a boolean value, and I'm not sure what & does (is it the same as AND?) and why the double set of double quotation marks around the <>? I understand the difference between empty strings and nulls and the reason for not testing simply for a null value (something I'm guilty of far too often). I just don't understand how that line equates testing for that. One of the other instances where I have recently found an If/Then used without a equality operator is in my code to test if the BE file exists where it should be. The code is:
Expand|Select|Wrap|Line Numbers
  1. If .Attributes And dbAttachedTable Then
  2. ...
I have absolutely no idea what values it is testing for. When running the code, the values for .Attributes and dbAttachedTable that result in a True are
.Attributes = 1073741824
dbAttachedTable = 107371824
Where in that code does it specify that those are the values it is looking for?
Dec 14 '12 #11
NeoPa
32,556 Expert Mod 16PB
Seth:
However I still don't quite Smiley's.
The ampersand (&) is a string concatenator character (See Using "&" and "+" in WHERE Clause for some interesting and related points). "ABC" & "DEF" resolves to "ABCDEF".

Form controls can be unpredictable. One way to make a control behave as if it is not a Null but an empty string, is to create an expression Me.MyControl & "". As the string is a string, and the control reference will return something, whatever, the expression will always resolve to a string. Even if the control is a Null.

If Me.MyControl & "" <> "" Then
This is forcing the left side of the expression to evaluate to a String expression, no matter whether it contains a value or not (Particularly Null). It isn't necessary, but it can be helpful to understand when you are dealing with variables (of any sort) that may contain Null.

AND
You really need to look this up and think about it. It isn't too straightforward for people who don't think excessively logically. I'll explain what I can though (Not a reason to avoid checking it out properly, but hopefully may get you started).

AND is a bitwise logic operator. Bitwise operators are ones that work on bits (Binary Digits) independently of each other. I should say at this point that it is much easier to understand and appreciate if you work and think either in Binary or, more probably, Hexadecimal, rather than Decimal (Bases 2, 16 and 10).

If you consider a Bytes variable has 8 bits and is generally treated as numeric. A decimal number 10 is written in Hex as 0A and Binary as 0000 1010. And is an operator that puts two values up against each other and returns a 1 value for each bit where both matching bits in the original values are 1. If either or both is a 0 then 0 is returned. Here are some binary examples for you :
Expand|Select|Wrap|Line Numbers
  1. 0000 1010     0000 1111     0000 1010
  2. 0000 1010     1111 0000     0000 0000
  3. 0000 1010     1111 1111     0000 1010
  4. 1010 0000     0000 1111     0000 0000
  5. 1010 0000     1111 0000     1010 0000
  6. 1010 1010     0000 1111     0000 1010
  7. 1010 1010     1111 0000     1010 0000
  8. 1010 1010     0000 1010     0000 1010
  9. 1010 1010     0000 0101     0000 0000
If you print out .Attributes and dbAttachedTable in Hex (Use ?Hex(.Attributes),Hex(dbAttachedTable)) you'll see that only one bit is set.
Expand|Select|Wrap|Line Numbers
  1. 40000000   40000000
This is because you can store a flag value in each of the available bits when using bitwise operations. .Attributes is such a field. When checking for this one flag, we're only interested in this flag. If we use AND, as illustrated above, we can ignore (turn off) all other bits simply by ANDing with a long integer which has binary 0s in each position we have no interest in. dbAttachedTable is exactly that. A value which turns off every binary flag except the one to indicate attached tables.

If you checked the If command earlier, as suggested, you will see that it treats any value that is not zero as True. If you have a complex flag field with various flags set, then any result of the AND that doesn't turn all off is considered to be True. Another reason why one should never use the code :
Expand|Select|Wrap|Line Numbers
  1. If blnFlags = True Then ...
The result of the bitwise AND may well be considered to be True by If, but it wouldn't be equal to the defined value True as that is explicitly -1. -1 because that is the number which is the result of all bits being 1 in a long integer. So, if this format is used you would get an unexpected failure instead of a success.
Dec 14 '12 #12
Seth Schrock
2,965 Expert 2GB
Okay, I understand Smiley's code now. I was thinking of the double quotes as if they were trying to have quotation marks added to a string. That part makes perfect sense now. In this particular case, there was an equality operator in the condition.

For the .Attributes and dbAttachedTable part, I think I understand, but let me make sure. The And compares the two values and returns a true when the values are equal (all the binary number match up). Is that correct?
Dec 14 '12 #13
NeoPa
32,556 Expert Mod 16PB
No. A True is a confusing concept here. Each bit position returns a 1 when the 1 bits match up in the two values being ANDed together. The result is a pattern of 1s and/or 0s. Only if all bits are 1 can it be said to be equal to True (-1), but any value other than all 0s (False) will take the True route through the If command. This is why it can be confusing (and logically incorrect) to compare a value with True to test it.
Dec 14 '12 #14
Seth Schrock
2,965 Expert 2GB
Ah, I see.
Expand|Select|Wrap|Line Numbers
  1. If .Attributes = True AND dbAttachedTable = True
  2.  
  3. If (.Attributes AND dbAttachedTable) = True
are all false statements as there isn't any true/falseness about them. But because of the match (in the real or original code), the If/Then statement will follow the true path. That makes sense. Now would I be correct in understanding that the reason that .Attributes and dbAttachedTable have the bitwise comparison done between them is that there is no equality operator? So while the outcome is the same, the process is totally different between the following:
Expand|Select|Wrap|Line Numbers
  1. If .Attributes = 1073741824 AND dbAttachedTable = 1073741824 Then...
  2. vs.
  3. If .Attributes AND dbAttachedTable Then...
The first option would perform no comparison between the two other than matching that both equations are true or both are false. In the second option, because there is no equality operator, the condition becomes a comparison between the two and the If/Then statement will follow True if they match and False if they don't.

Can this be done between regular variables (strings, integers, boolean, etc.) or does it have to be something that returns a number (so string and boolean wouldn't work)? I can't think of a situation where it would be needed but it would be nice to know if I ever come up with a situation that I might be tempted to use it if it is something I can or can't do.
Dec 14 '12 #15
NeoPa
32,556 Expert Mod 16PB
That seems to indicate good progress Seth, but I mentioned it wasn't too straightforward, no?

First important tip is to use Hex when dealing with bits. There is no overlap between binary and decimal, so it's very complicated to see where the bits are.

Bitwise logic is not a simple as you seem to appreciate as in 8 bits you can store 8 separate flags or pieces of binary information. If bit-0 (numbered from the right, or least significant digit) reflects A and bit-1 reflects B then I can check for either of them in my flag variable by saying :
Expand|Select|Wrap|Line Numbers
  1. If blnFlags And &H03 Then ...
Certainly, one could check one flag of the 8, but the facility is there to check more than one.

As such, I don't believe your understanding as expressed is quite accurate.
Dec 14 '12 #16

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

Similar topics

3
by: William Payne | last post by:
Hello, I'm working on my own variant of grep and I have an annoying astethic (spelling) problem. Say the user is searching for the string "foo" in all files of type .bar in a given directory...
19
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. ...
8
by: David | last post by:
Hi, I have a form as follows: Main Form (To Select Customer) Sub Form 1 (To Select PO Number, by clicking on field) - Continuous form Sub Form 2 (To view order details)- Continuous form...
1
by: Daniel Martini | last post by:
Hi all, I'm currently coding some functions in plpgsql for generating reports out of records in a table. Problem is: NULL values in records make the complete function fail. Here is a simple...
2
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2003, .net 2005 and Crystal Report 11 Rel 2. My C# application runs fine on my Development machine but when I create the setup .msi file and install it on another PC the Crystal...
11
by: elrondrules | last post by:
Hi Am pretty new to python and hence this question.. I have file with an output of a process. I need to search this file one line at a time and my pattern is that I am looking for the lines...
3
by: | last post by:
I'm analyzing large strings and finding matches using the Regex class. I want to find the context those matches are found in and to display excerpts of that context, just as a search engine might....
1
by: morangi | last post by:
<?php session_start(); error_reporting(E_ERROR && ~E_NOTICE && ~E_WARNING); ob_start(); $link = mysql_connect('localhost', 'root', 'test'); if (!$link) { die('Could not connect: ' ....
4
by: Deano | last post by:
I have a little search routine going whereby the user types in some text and clicks a button on the main form. I then see if that text exists in a record on the subform. I get a match, grab the...
15
by: DavidW | last post by:
Hello, Is the function below the simplest way to produce an iterator to the next non-space in a string? (Or the upper-bound iterator if none is found). Searching for a sequence is overkill and...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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,...

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.