By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

Problem checking textboxes for null

P: 19
Moderator
This thread has been moved. New questions should go into new threads.

Hi Guys,

I am completely lost, trying to get the code found in:
how-check-if-all-textboxes-form-null to work

I am trying the code but I keep getting the error;

'The object doesn't support this property or method'

Runtime error '438'.

Expand|Select|Wrap|Line Numbers
  1.         Dim ctl As control
  2.         Dim check As Boolean
  3.  
  4.             check = True
  5.  
  6.         For Each ctl In Me.Controls
  7.               If ctl.contoltype = acTextBox Then
  8.                     If ctl.Value = "" Then
  9.                      check = False
  10.             End If
  11.  
  12.         End If
  13.  
  14.     Next ctl
  15.  
  16.             If check = False Then
  17.  
  18.             MsgBox "There is no information provided", vbOKOnly
  19.             Me.lstResults.RowSource = ""
  20.             Else
  21.             ListSearchresults
  22.  
  23.             End If
  24.  
The error flags
Expand|Select|Wrap|Line Numbers
  1. If ctl.contoltype = acTextBox Then
and when I hover over actextbox is states actextbox=109

What i want is when the search button is clicked, to make sure at least one text box has a value. if none have a value then to throw the error message and exit sub, other run the Listsearchresults sub.

Any help and guidance would be much appreciated.
Mar 2 '12 #1
Share this Question
Share on Google+
6 Replies


TheSmileyCoder
Expert Mod 100+
P: 2,321
It would appear you have a typo. Its called ControlType, not contoltype.
Mar 2 '12 #2

NeoPa
Expert Mod 15k+
P: 31,307
The clue is in the fact that the property is spelled with all lower-case letters. IntelliSense will fix that for you if it's recognised. Clearly, in this case it doesn't recognise what you've typed ==> Mistyped.

It's why creating items with all lower-case characters (such as [check]) is recommended against. That and Require Variable Declaration can help you to avoid such simple errors in future. Think of these ideas as tools to help you avoid them.
Mar 2 '12 #3

patjones
Expert 100+
P: 931
I forgot all about that thread, and yet it illustrates many good points.

One thing that I will say about IntelliSense is that it usually, but does not always, behave as you might expect it to. In the present case, where we defined "ctl" as a Control typed variable and then refer to "Value" as a method of it...IntelliSense will not capitalize Value for you - presumably because the control type of "ctl" is arbitrary (it may describe a control that cannot have a value). Yet, the code will compile and execute just fine.

I also think that another crucial point, taken from the original thread, is that the If statements must be nested for this to work out properly. The first time that I tried to use code like this, it took me time to realize that the system evaluates both components of an AND even if the first component evaluates to False.

Pat
Mar 2 '12 #4

NeoPa
Expert Mod 15k+
P: 31,307
PatJones:
In the present case, where we defined "ctl" as a Control typed variable and then refer to "Value" as a method of it...IntelliSense will not capitalize Value for you - presumably because the control type of "ctl" is arbitrary (it may describe a control that cannot have a value).
That's not accurate Pat. Intellisense will capitalise it for you unless you have somewhere redefined "Value". This can be done by dimming a variable for instance :
Expand|Select|Wrap|Line Numbers
  1. Dim value As Variant
After this the system will treat all .Value references as .value. One of the reasons that reusing known names for your items is also recommended against.

In fact, it will quite happily match case for any known names, whether or not they are valid properties of the object. Try using the following for instance (where txtName is a TextBox control) :
Expand|Select|Wrap|Line Numbers
  1. Me.txtName.querydefs = 3
Even though TextBoxes have no .QueryDefs property, it will be made to match .QueryDefs for you.
Mar 2 '12 #5

patjones
Expert 100+
P: 931
Correct you are. I got mixed up. When I tried it just now it does indeed capitalize "Value" for you. What it does not do is bring "Value" up in the list of methods for "ctl", an observation that fits in more naturally with what I was trying to say.
Mar 2 '12 #6

NeoPa
Expert Mod 15k+
P: 31,307
PatJones:
What it does not do is bring "Value" up in the list of methods for "ctl", an observation that fits in more naturally with what I was trying to say.
Indeed Pat. That also makes sense of course, as a Control object is almost as non-specific as a Variant (Which is what you seemed to be saying the first time round, to be fair). A reason for using such types only where the code needs to handle various different types of entities.

To say that another way, when dealing with a specific type of control, for instance TextBox, it makes more sense to use a variable of type TextBox. Only when dealing with different types of control does it make sense to use one of type Control. For this case it's valid to use the Control type for the variable, of course.
Mar 2 '12 #7

Post your reply

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