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

How to check if all the textboxes in a form are null

doma23
107 100+
I have a form with multiple textboxes and 2 command buttons (Save, Cancel).
If user clicks the CANCEL button, I would like to check if all the textboxes on that form are null and in the case they are, I wan't form to close immediately.
If all the textboxes are not null, I want to ask user if he really wants to cancel and warn him that all entered data will be lost.

When I run the code, I receive the runtime error 438.
My code is something like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdWrite_Click()
  2.  
  3. Dim stDocName As String
  4. Dim stLinkCriteria As String
  5. Dim c As Control
  6. Dim check As Boolean
  7.  
  8. stDocName = "START_M"
  9. check = True
  10.  
  11. For Each c In Me.Controls
  12. If TypeOf c Is TextBox And Not IsNull(c.value) Then check = False
  13. Next
  14.  
  15. If check = False And funz_are_you_sure = "NO" Then Exit Sub 
  16. Else
  17. DoCmd.Close
  18. DoCmd.OpenForm stDocName, , , stLinkCriteria
Here comes the code of the function:

Expand|Select|Wrap|Line Numbers
  1. Public Function funz_are_you_sure() As String
  2. Dim Msg, Style, Title, Help, Ctxt, Response, MyString
  3. Msg = "Are you sure? You will lose all inserted data..."
  4. Style = vbYesNo + vbExclamation + vbDefaultButton2
  5. Title = "Cancelling confirm"
  6. Response = MsgBox(Msg, Style, Title)
  7. If Response = vbYes Then
  8.     funz_are_you_sure = "YES"
  9. Else
  10.     funz_are_you_sure = "NO"
  11. End If
  12. End Function
May 28 '10 #1

✓ answered by patjones

It's because I forgot to nest the If tests...

Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim booFilledBox As Boolean
  3.  
  4. For Each ctl In Me.Controls
  5.       If ctl.ControlType = acTextBox Then
  6.            If Not (IsNull(ctl.Value) Or ctl.Value ="") Then
  7.                 booFilledBox = True
  8.            End If
  9.       End If
  10. Next ctl

The problem with the way I wrote it out the first time is that Access evaluates the WHOLE line for each control. There are some controls that cannot have a value, and so ctl.Value has no meaning. As a result, we need to first check that the control is a text box (or some other control that can have a value, like a combo box) - then make the check to see if it's empty or not.

Pat

5 17343
patjones
931 Expert 512MB
What line are you getting the error on? You can check this by setting a breakpoint at the first executable line in the code and then stepping through it using F8.

Usually, when I do checks like this in my Access forms, I do something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim booFilledBox As Boolean
  3.  
  4. For Each ctl In Me.Controls
  5.      If ctl.ControlType = acTextBox And Not (IsNull(ctl.Value) Or ctl.Value ="") Then
  6.           booFilledBox = True
  7.      End If
  8. Next ctl 
  9.  
  10. 'Handle case for finding a filled text box...

Pat
May 28 '10 #2
doma23
107 100+
Hi, tnx for answering.
I totally forgot to check the error with debugging.
I tried your code also and I get the same error number, it's on this line:

Expand|Select|Wrap|Line Numbers
  1. If ctl.ControlType = acTextBox And Not (IsNull(ctl.Value) Or ctl.Value ="") Then 
acTextBox value is "109" - I guess it's standard numeric value for text box
ctl is null

And that's it, I still don't understand.
May 28 '10 #3
patjones
931 Expert 512MB
It's because I forgot to nest the If tests...

Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim booFilledBox As Boolean
  3.  
  4. For Each ctl In Me.Controls
  5.       If ctl.ControlType = acTextBox Then
  6.            If Not (IsNull(ctl.Value) Or ctl.Value ="") Then
  7.                 booFilledBox = True
  8.            End If
  9.       End If
  10. Next ctl

The problem with the way I wrote it out the first time is that Access evaluates the WHOLE line for each control. There are some controls that cannot have a value, and so ctl.Value has no meaning. As a result, we need to first check that the control is a text box (or some other control that can have a value, like a combo box) - then make the check to see if it's empty or not.

Pat
May 28 '10 #4
doma23
107 100+
Wow. It works! :)
Interesting, the thing is that, as you can see, it also crossed my mind that not all controls have value property, so I added the code to check if the control is textbox, just as you did in your first code.
But I still don't understand completely, even if Access evaluates the whole line, our code included AND, so it would set boolean variable only in case both conditions are truth. So if the Access knows that one condition is not truth for sure (if it's not textbox), it shouldn't matter if it can't determine or process the second condition, becuase the boolean is in any case false.
Obviously, it does matter, but why?

UPDATE: Ooops, I think I kind of a answered myself. It's exactly beacuse of the AND that the machine looks at it as a WHOLE like you already pointed.

Well, thank you very much for your answer!
May 28 '10 #5
patjones
931 Expert 512MB
Well, the problem is that an expression which errors (such as ctl.Value for a ctl which cannot have a value) cannot be assigned a boolean status. So actually, the part after the AND has no boolean status and the entire line errors out.

Pat
May 28 '10 #6

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

Similar topics

9
by: Shyguy | last post by:
I have two forms that both open the same (3rd) form. The third form does the same thing, but a little differently depending on which form it is opened from. Is there a way I can check which form...
4
by: Rich | last post by:
I have a access form that is connected to linked sql table via odbc. I have some fields that I dont want to allow nulls when data is entered via the form. I can set the null checking on the SQL...
0
by: JIK | last post by:
Hi. I have a web app written in ASP.NET/C# with a lot of textboxes, dropdowns etc. Is there any possibilities to check for changes in texboxes etc in a web form on the client side easily? And...
4
by: Gene | last post by:
As my purpose is to load one form at a time, so I want to unload another form before loading the form. But how to check which form is being load, so I can unload it first.
3
by: Eagle | last post by:
Hi, I've got an input form (frmInput) that can be accessed in two ways. A combobox on this form either gets filled in by a selection on an already opened form (frmBasic), or one can input a...
9
by: KelsMckin | last post by:
Hello, I was wondering if there was a way to check there was already a form open before opening a new one, sorry that doesnt seem clear, here is an example: The following button opens a new form...
8
by: News Microsoft | last post by:
Hi there. I would like to know how can I test if a Form exists. This is the situation: I have a single Form in my application. My objective is, when I minimize the form, it will disapear and a...
9
by: pamela fluente | last post by:
I want a simple method or function to check if at least one of the field in a composite object is null (nothing). For instance, if Field5 is null I would like that a function such as ...
5
by: wassimdaccache | last post by:
Hello everybody I'm using access 2003 Sometimes I use to open more than form in my project what I need is to check another form if still opened For example I have : form name X,Y,Z
8
by: cssExp | last post by:
Hello, It should be working but for some reasons it's not. function Validator(frmname) { this.formobj=document.forms; if(!this.formobj) { alert("BUG: couldnot get Form object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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.