473,408 Members | 2,113 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,408 software developers and data experts.

Better way to test for field being left empty

TD
I use this to test if a user has left a text field empty:

If Me.txtProductID = "" Or IsNull(Me.txtProductID) = True Then
do something here
End If

Is there a better way to determine if a user has left a field empty?

Thanks,
TD

Nov 13 '05 #1
20 2296
On 10 Nov 2005 07:32:46 -0800, "TD" <dl**@adsi-sc.com> wrote:
I use this to test if a user has left a text field empty:

If Me.txtProductID = "" Or IsNull(Me.txtProductID) = True Then
do something here
End If

Is there a better way to determine if a user has left a field empty?

Thanks,
TD


I use -
If Len(Me.txtProductID & vbNullString)=0 Then 'empty field
Nov 13 '05 #2
TD wrote:
I use this to test if a user has left a text field empty:

If Me.txtProductID = "" Or IsNull(Me.txtProductID) = True Then
do something here
End If
What is the procedure for inserting a blank string into a textbox on a
form?
Is there a better way to determine if a user has left a field empty?
You can check all the controls with names beginning with "txt" in one
loop. Maybe put a value in the Tag Property of textboxes that are
required to have data.

Thanks,
TD


James A. Fortune

Nov 13 '05 #3
"TD" <dl**@adsi-sc.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I use this to test if a user has left a text field empty:

If Me.txtProductID = "" Or IsNull(Me.txtProductID) = True Then
do something here
End If

Is there a better way to determine if a user has left a field empty?


What you have there is a belt 'n' braces approach as you're testing for both
nulls and for zero-length strings. The only slight change I'd make is

.... Or IsNull(Me.txt) Then

but that's just personal preference.

Keith.
www.keithwilby.com
Nov 13 '05 #4
What's the matter with

If Nz(me.txt) = "" then
....
End If

Phil
"TD" <dl**@adsi-sc.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I use this to test if a user has left a text field empty:

If Me.txtProductID = "" Or IsNull(Me.txtProductID) = True Then
do something here
End If

Is there a better way to determine if a user has left a field empty?

Thanks,
TD

Nov 13 '05 #5
"TD" <dl**@adsi-sc.com> wrote in
news:11**********************@o13g2000cwo.googlegr oups.com:
I use this to test if a user has left a text field empty:

If Me.txtProductID = "" Or IsNull(Me.txtProductID) = True Then
do something here
End If

Is there a better way to determine if a user has left a field
empty?


Why are you allowing zero-length struings? In my opinion, it's bad
design to define your underlying table to store ZLS's.

Form controls do not store ZLS if unbound or bound to a field that
disallows ZLS. So, in either of those cases, you need only check for
IsNull().

As has been pointed out, though, if you *are* storing ZLS and need
to check for Null or ZLS, then checking the length of the control
value concatenated with a ZLS is the most efficient method.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #6
"Phil Stanton" <ph******@xxstantonfamily.co.uk> wrote in
news:43***********************@ptn-nntp-reader02.plus.net:
What's the matter with

If Nz(me.txt) = "" then
....
End If


Well, for one, you're allocating memory to store your zero-length
string for comparison.

If Nz(me.txt) = vbNullString then

is always going to be more efficient, as vbNullString is a
predefinied Access constant, so the memory for it is already
allocated.

I vaguely remember, also, that MichKa seemed to think that testing
Len() was more efficient than any alternatives, though that
different would matter in only large loops.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #7
David W. Fenton wrote:
vbNullString is a
predefinied Access constant, so the memory for it is already
allocated.


How much memory?

--
Lyle Fairfield
Nov 13 '05 #8
"Lyle Fairfield" wrote
vbNullString is a
predefinied Access constant, so the memory for it is already
allocated.


How much memory?


I'd guess: Not enough that you'd seriously miss it with 1GB of RAM, or more,
as is common in even modestly priced notebook computers these days.

Larry
Nov 13 '05 #9
It may be safer to set the properties of the field to "Required" and
not to allow ZLS, and then to let Access (and the DB engine ...JET?) do
their thing, handle the warnings, requirings etc.
Access is what we pay for, why not use it?
Access has a standard way of doing things. When we have Access do them
throughout our application then the application and its gui are
standardized.
The chance of Access screwing up is about 0.0000000000000001 %.
I've developed in Access since ... it's so far back I can't remember
whether I got on at V1 or V2. The chance of my screwing up is about
(IMO) 5 %.
What is yours?
Of course, maybe my guesses as to why you want to check this are wrong.

Nov 13 '05 #10
To a user one or more blanks in a field looks the same as none at all
and so I always test:

If Trim(Me.txtProductID) = "" Or isnull(Me.txtProductID) then

Nov 13 '05 #11
Per David W. Fenton:
is always going to be more efficient, as vbNullString is a
predefinied Access constant,


Is there any consideration around where the code has to go to get vbNullString -
as opposed to an inline ""?

I guess it's just an address either way... but I don't know anything about what
goes on below VBA.
--
PeteCresswell
Nov 13 '05 #12
Try StrPtr(vbNullString)
and StrPtr("")

Nov 13 '05 #13
"(PeteCresswell)" <x@y.Invalid> wrote in
news:gl********************************@4ax.com:
Per David W. Fenton:
is always going to be more efficient, as vbNullString is a
predefinied Access constant,


Is there any consideration around where the code has to go to get
vbNullString - as opposed to an inline ""?

I guess it's just an address either way... but I don't know
anything about what goes on below VBA.


I don't understand your question.

When your code compares two things, memory has to be allocated for
the comparison. If one side of the comparison is a global constant,
then the memory is already allocated, so no new allocation is
required.

Obviously, this would have to be in a loop of 1000s of iterations
for it to make any real difference, but it's not that uncommon a
scenario for loops of that nature.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #14
Lyle Fairfield <ly***********@aim.com> wrote in
news:8j********************@read1.cgocable.net:
David W. Fenton wrote:
vbNullString is a
predefinied Access constant, so the memory for it is already
allocated.


How much memory?


That's irrelevant. What matters is the time it takes to allocate the
memory for the empty string in each iteration of the loop.

Now, if you did this:

Dim strMyEmptyString As Strig
strMyEmptyString = ""

If Nz(me.txt) = strMyEmptyString then
. . .
End If

then you've removed the problem, as you've allocated memory for it
by creating a variable and storing the value for it.

In a loop, you wouldn't see any different between strMyEmptyString
and vbNullString, since the CPUs necessary to allocate the memory
for your variable would be very small and happen only once.

But why do that when there's a global constant with that value
stored in it already defined? THe memory is already allocated every
time you start up Access, so why allocate more, even when there is
no realistic perfromance penalty?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #15
"Jim Devenish" <in***************@foobox.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
To a user one or more blanks in a field looks the same as none at
all and so I always test:

If Trim(Me.txtProductID) = "" Or isnull(Me.txtProductID) then


Why would you allow the storage of spaces in a field?

The default behavior of Access text controls trims all trailing
spaces, so the only way I've ever ended up with spaces in a field
was when I was appending data in code or SQL and was sloppy about
it.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #16
Per lylefair:
Try StrPtr(vbNullString)
and StrPtr("")


"" looks tb faster.

For 100 million iterations, I got 5.203 seconds for vbNullString and 3.828
seconds for "".

viz:
-------------------------
Const mLim = 100000000
Const mWhoopie = "xxx"

Public Sub aa()
Dim i As Long
Dim L As Long

DoCmd.Hourglass True
LogTime True, "Begin vbNullString"
For i = 1 To mLim
L = StrPtr(vbNullString)
Next i
LogTime False, "End vbNullString"
DoCmd.Hourglass False
End Sub
--------------------------

Then I did the real thing, getting 29.250 seconds for vbNullString and 27.922
seconds for "".
viz:
-------------------------
Const mLim = 100000000
Const mWhoopie = "xxx"

Public Sub aa()
Dim i As Long
Dim L As Long

DoCmd.Hourglass True
LogTime True, "Begin vbNullString"
For i = 1 To mLim
If Len(mWhoopie & vbNullString) > 0 Then
'Do nothing...
End If
Next i
LogTime False, "End vbNullString"
DoCmd.Hourglass False
End Sub
-------------------------
Bottom line: it looks like "" is faster; but only in the most technical,
legalistic sense. As a practical matter, the execution speeds of the two
appear tb identical.
--
PeteCresswell
Nov 13 '05 #17
hmmmmmmmm
Well, creative minds do creative things, even unexpected things. I
thought you would investigate the StrPtr of "" and of vbNullString.
What you would find out if you were here in Carlisle ON Canada is that
"" is a long pointer (VarPtr eg 1308288 ) to a long String Pointer
(StrPtr eg at 1308288 we find four bytes which represent 134606676). At
the memory location of the StrPointer 134606676 we find the string in
unicode (Thus "Pete" would take up 8 bytes. The length of the string is
found in the four bytes before the StrPtr memory location, in ""'s
case, zero, [in Pete's case, 8] and of course there would be no
relevant bytes at the location, as the length of "" is zero.
BUT, vbNullString is a long pointer to nowhere; that is its string
pointer is zero (0). I am guessing therefore that there is no memory
allocated for vbNullString, not when Access opens and not ever.

Nov 13 '05 #18
Per David W. Fenton:
I don't understand your question.


I was thinking in terms of references - that maybe one way there was only a
direct reference and another way it might have to go through a handle or
something.

All seemingly moot in light of my little experiment...
--
PeteCresswell
Nov 13 '05 #19
David W. Fenton wrote:
But why do that when there's a global constant with that value
stored in it already defined? THe memory is already allocated every
time you start up Access, so why allocate more, even when there is
no realistic perfromance penalty?


How much memory?

Nov 13 '05 #20
"(PeteCresswell)" <x@y.Invalid> wrote in
news:3u********************************@4ax.com:
Bottom line: it looks like "" is faster; but only in the most
technical, legalistic sense. As a practical matter, the
execution speeds of the two appear tb identical.


You're not testing the situation I said was faster.

I said that:

If [expression] = vbNullString Then

is going to be faster in a loop than

If [expression] = "" Then

Lyle's StPter() code is not looking at the same task, and your test
code is concatenating, which is introducing irrelevant tests.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DiggidyMack69 | last post by:
Hello folks I am working on a project using JSP's and Servlets. I have a servlet that handles requests from several dynamic HTML forms. I want to know if there is a way to check wether an HTML form...
4
by: chirs | last post by:
I try to test a field to see if it's empty. But it prints "It is not null". Why and how can I fix it? <html> <head> </head> <body> <input type="text" name="box1"> <script>
6
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really...
4
by: Brian | last post by:
I'm pulling what's left of my hair out. I'm trying to check a memo field on opening a report. If the field has "value", I'd like to change the fields back color. If it's "Empty", I'd like to...
3
by: AA Arens | last post by:
When I want the first character of a field to be Uppercased, I need to make an input mask, like >L< followed by ??????? for example. But this mask creates ____ in an unfilled field, which I don't...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool including SourceOffSite, SourceAnyWhere and VSS Remoting This article makes a detailed...
2
by: AA Arens | last post by:
I have button and added the command that places a date and some stripes in the Notes-field. That works fine. If there is already some text, it places the date at a next line: Private Sub...
1
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box....
1
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i...
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?
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
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.