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

button to check if fields on form do not equal blank

Hi,

I have a form with around 10 fields..

When the user clicks the "add record button"
I would like a message box to appear to say which fields need responses (field = "") and it will cancel the event.

Hope this is clear
Sep 3 '15 #1
2 1229
jimatqsi
1,271 Expert 1GB
There are several ways you could approach this. One would be the verbose one, like this (each method requires adding code to the top of the On_Click event for the button:

Expand|Select|Wrap|Line Numbers
  1. dim bErr as boolean
  2. bErr=false
  3. If len(nz(me.textbox1))=0 then bErr=true
  4. If len(nz(me.textbox2))=0 then bErr=true
  5. If len(nz(me.textbox3))=0 then bErr=true
  6. ...
  7. if bErr then
  8.     msgbox "At least one field has been left blank"
  9.     exit sub
  10. end if
  11.  
Or, a much more elegant solution would be something like this. Decide on a convention to use that will identify each of these problem boxes (It is assumed some text boxes might be okay to be blank). Your convention might be to name these text boxes in a similar manner. I prefer to use the Tag property. Set the Tag property for each required field to some value, like "required." Then your code could look like this
Expand|Select|Wrap|Line Numbers
  1. dim ctl as control
  2. dim bErr as boolean
  3. for each ctl in me.Controls
  4.     if ctl.tag = "required" and len(nz(ctl.value))=0 then bErr=true
  5. next
  6. if bErr then 
  7.     msgbox "At least one field has been left blank"
  8. end if
  9.  
  10.  
Or this would add the names of the fields that are blank
Expand|Select|Wrap|Line Numbers
  1. dim ctl as control
  2. dim bErr as boolean
  3. dim msg as string
  4.  
  5. for each ctl in me.Controls
  6.     if ctl.tag = "required" and len(nz(ctl.value))=0 then 
  7.         bErr=true
  8.         msg = msg & ctl.name & vbnewline
  9.     end if
  10. next
  11. if bErr then 
  12.     msgbox "These fields have been left blank." & vbnewline & msg
  13.     exit sub
  14. end if
  15.  
  16.  
And there are numerous other ways to approach this.

Jim
Sep 3 '15 #2
Many thanks for this...

What is me.controls? Is this the form name?
Also,ctl.name.. ? is the name of the text
Sep 3 '15 #3

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

Similar topics

1
by: Jerry Sievers | last post by:
Fellow coders; I am curious about a difference in behavior between Mozilla and MSIE. The scenario; 1. a php form page is loaded on first hit with field values set to various things including...
5
by: Codeman II | last post by:
Hi there, I am building a form where the user must upload a picture and fill in his details. Now I have a problem as all of this is on the same form. How will I be able to have the Browse...
8
by: helpful sql | last post by:
Hi, I have 2 radio buttons on my Windows form control. The radio button's CheckedChanged event disables or enables other controls on the form based on the value of the Checked property. When the...
4
by: fanis | last post by:
Please help me on this one. I use the docmd.openform "frmName",,,stLinkCriteria expresion to open a form. For example: stlinkcriteria = " = " & 44 Although RecID = 44 exists the form opens with...
2
by: Del | last post by:
I have a popup form that consist of a single field called EnteredBy and a Subform that has three fields. The popup form also has a button in the Form Footer called close. In the On Click event I...
5
by: Rinaldo | last post by:
Hi, How to regonize when the user clicks the minimize button in a form with events? Rinaldo
3
by: extrym | last post by:
I have 2 forms, "Staff" and "Security" (without the ""'s). The Staff form has the following fields: staff_ID staff_name staff_number staff_DOB priority The Security form contains the...
3
by: hannoudw | last post by:
Hi i searched here but i didn't find out how to add a button and when i click on this button to open a new blank record? I could really need some help. many thanks :)
1
robin a
by: robin a | last post by:
I have a tabbed that contains a form NavigationSubform (this was auto-named so I don't know if it goes in the syntax for what I'm trying to do) I am concerned with a form named...
10
by: tajuddin | last post by:
I wanted to have a Command Button on a form to save a record & then ask to open a new record. I am new to access. I have MDFID on a form with primary key. Thanks Seth, To be frank, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.