473,670 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preventing fields from being left blank when form closed

109 New Member
I am struggling with something that is probably quite simple. When I close a form, if certain fields are left empty then I want a message to pop up to tell the user these fields are required and then they can go back and fill them in.

Below is the code (which I found and modified slightly) and it works when used to check ONE field but when I added code for an additional field to be checked, it returns an error (run-time error 2450) - Access can't find the form 'Frm_Case' referred to in a macro expression or Visual Basic code

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnBack_Click()
  2. If IsNull(Forms![Frm_Case]![Frm_Individual_Details].Form![Per_Referral_Date]) Then
  3.   If MsgBox("You must enter a referral date." & Chr(13) & Chr(10) & Chr(13) & Chr(10) & _
  4.   "Press 'OK' to return and enter a date.", _
  5.   vbOKOnly, "Referral Date Required") _
  6.   = vbOKOnly And Forms![Frm_Case]![Frm_Individual_Details].Form![Per_Referral_Date].SetFocus Then
  7.   End If
  8. Else
  9.   DoCmd.Close
  10. End If
  11.  
  12. If IsNull(Forms![Frm_Case]![Frm_Individual_Details].Form![cboReferredFrom]) Then
  13.     If MsgBox("You must specify where the client was referred from." & Chr(13) & Chr(10) & Chr(13) & Chr(10) & _
  14.     "Press 'OK' to return and select a referral service.", _
  15.     vbOKOnly, "Referred From Service Required") _
  16.     = vbOKOnly And Forms![Frm_Case]![Frm_Individual_Details].Form![cboReferredFrom].SetFocus Then
  17.     End If
  18. Else
  19.     DoCmd.Close
  20. End If
  21.  
  22. End Sub
I need to add code for one more field. Any pointers/help greatly appreciated!
Mar 31 '10 #1
15 16639
ADezii
8,834 Recognized Expert Expert
For the 2nd Test, your syntax is incorrect, namely vbOKOnly is not a valid MsgBox Result, and you cannot tack on the And condition:
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Forms![Frm_Case]![Frm_Individual_Details].Form![cboReferredFrom]) Then
  2.   If MsgBox("You must specify where the client was referred from." & vbCrLf & vbCrLf & _
  3.     "Press 'OK' to return and select a referral service.", _
  4.     vbOKOnly, "Referred From Service Required") = vbOK Then
  5.       [Frm_Case]![Frm_Individual_Details].Form![cboReferredFrom].SetFocus
  6.   End If
  7. Else
  8.     DoCmd.Close
  9. End If
P.S. - This type code normally resides in the Form's BeforeUpdate() Event.
Mar 31 '10 #2
hedges98
109 New Member
I'm a little confused - if vbOKOnly is not a valid MsgBox Result and And cannot be tacked onto the condition, why does it work in the first test and not the second?

Would it be wiser to move all of this code to the Form's BeforeUpdate() Event as per your suggestion?
Mar 31 '10 #3
ADezii
8,834 Recognized Expert Expert
I'm a little confused - if vbOKOnly is not a valid MsgBox Result and And cannot be tacked onto the condition, why does it work in the first test and not the second?
vbOKOnly is a MsgBox 'Argument' and evaluates to 0 while vbOK is a MsgBox Result and evaluates to 1. What you are doing is evaluating the Return results of the Msgbox Function and testing to see if it = 0 (vbOKOnly) which will always evaluate to False since 0 is not a valid Msgbox Result. You then tack on an And condition to evaluate the Result of the SetFocus Method which makes no sense to me. I could be looking at this from the wrong angle, see what other Members have to say.

Would it be wiser to move all of this code to the Form's BeforeUpdate() Event as per your suggestion?
I think so, but you may have to set the Cancel Argument to true in order to negate the Update of the Form.
Mar 31 '10 #4
hedges98
109 New Member
Putting the code into the Before Update event of the Form returns an error message (2501) - the Close action was cancelled.

I tried to understand what you've explained in the post above but it went a bit over my head so I'm going to leave this for today and give it a look tomorrow with a clear head to see if I grasp it then.

Cheers!
Mar 31 '10 #5
hedges98
109 New Member
Right, I've had a better look at this and have been looking at the BeforeUpdate of the form and found this code which I have used instead of the code previously posted (I also realised I was overcomplicatin g referencing the fields in the code!)
Expand|Select|Wrap|Line Numbers
  1. If Len(Me!cboReferredFrom & vbNullString) = 0 Then
  2.   MsgBox "Please select where the client was referred from"
  3.   Cancel = True
  4.   Me!cboReferredFrom.SetFocus
  5. End If
The only problem is, the form still closes if the field is blank. I thought the 'Cancel = True' part would prevent that?

Does it have something to do with me having a button that is used to close the form (OnClick even of button) also?

Another point, the fields are all in a subform on a form. I've tried putting the BeforeUpdate code in the subform (which works but still closes the form) and the form (which does nothing).
Apr 1 '10 #6
Belimisimus
18 New Member
@hedges98
Check this post, I think you can find answer there...
http://bytes.com/topic/access/answer...ons-can-posted
Apr 1 '10 #7
hedges98
109 New Member
Thank you!

Okay so, Cancel = True doesn't relate to the closing of the form. I've tried a few things from that link but the form is still closing after the MsgBox pops up asking for the field to be filled in!
Apr 1 '10 #8
hedges98
109 New Member
Still struggling with this if anyone has any pointers!
Apr 6 '10 #9
ADezii
8,834 Recognized Expert Expert
Can you Upload the DB?
Apr 6 '10 #10

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

Similar topics

5
7903
by: Batezz | last post by:
I have created a form (below) How do I stop it redirecting to another page (productsearchresults.php) when form is submitted if both the fields are blank? Any help appreciated. Batezz
3
2566
by: Itai | last post by:
I have an aspx file named index.aspx which contains two ‘form' sections, one that has the runat=server attribute (e.g From1) and one which is a regular HTML form (e.g SignInForm). I am trying to copy values from two INPUT controls that are located within the Form1 section to hidden INPUT controls located in the SignInForm and post the SignInForm form hidden controls values to a different page (e.g login.aspx) Problem is that after...
10
3102
by: Mark McLellan | last post by:
Dear all Following the oft-repeated advice here and ciwas I have made my site nearly 4.01 strict (working on it). There are some items on which I would appreciate your advice: 1. Hidden fields http://www.zoo.co.uk/~mmenterprises/contact.htm I am using FormMail from Matt's Script Archive. The W3C validator objected to the hidden fields unless I put, say, P round them. That gave
0
2151
by: visionstate | last post by:
Hi there, I have a form which has 2 text boxes, a combo box and a sub form in it (which reads from a query. The query reads from the table). On load, I would like the fields in the text boxes and combo box to be become blank. If I use the "" or 'null' instruction then this in turn makes the corresponding field in the sub form blank which in turn makes the query field blank and the table field blank! If I then change the combo box option,...
4
2209
by: sparks | last post by:
I am trying to fix a database that someone did about 4 yrs ago in access97. The main table just contains demographics and is on the main form of the database. It has a subform on a tab that contains the other information. it is set 1 to 1 in the relationships. simplify if I can
12
2140
by: Mark Rae | last post by:
Hi, See the previous thread Request.Form abuse in this newsgroup... I'm looking for a simple and efficient way to prevent people hijacking the <formtags on my websites and using them to send spam. I would imagine they're using the HttpWebRequest method for this. Essentially, it would require a property on a WebForm that indicates whether it is *only* for PostBack (true by default, but configurable), which would
12
2666
by: Jan | last post by:
Hi: I've got the Error 3197 problem ("The Microsoft Jet database engine stopped the process because you and another user are attempting to change the same data at the same time.") in a client database. It started appearing about a month ago and has gotten worse and worse. The responses I saw when I searched on this topic talk about corruption in a memo or OLE field, but there are none of those in this database. I took a copy of the...
69
8053
by: kabradley | last post by:
Alrighty Guys and Gals, I have another question that I hope you all can help me with. I have a report that uses a cross-tab query as its record source. This cross-tab query is getting all of its information from another query. That query, qryRpt, is pulling information from several different tables. One of these table is tblDistributions, a table that holds monthly distributions for a particular investment, and the second is tblDeduction, a...
6
1839
by: briggal1 | last post by:
Hi I wonder if anyone can help. I have a report in Access 2000, which works perfectly and displays all the information until I select a period where one, or more, of the fields is blank. Example 1: I run the report and select data from 05/04/06 to 04/04/08 and get the report no problem.
12
2112
by: MikeB | last post by:
I created a small website for a friend. On this website he has a contact page where people can send him email. When I wrote this page I checked some tutorial pages and they warned about certain precautions to take to avoid spammers using the mail form to spam multiple people. I believe I did most of that, such as making sure that the header fields does not include multiple addresses, etc. Now it does seem some spammer has discovered...
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8896
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8810
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8590
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7410
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2035
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.