473,549 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A particular field cannot be empty

77 New Member
I am new to access and i am trying to build a database for enntering Subscription details of a particular journal. Wat I want is that FirstName field on the form should not be left blank. If the user does not enters the First Name and moves to the field, there should be msgbox prompt, prompting the user that the name cannot be left blank and the cursor should be back on the Firstname text box.

I have tried using the foll code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub FirstName_LostFocus()
  2. If IsNull(Me.FirstName) Then
  3. MsgBox "First Name cannot be left blank"
  4. Me.FirstName.SetFocus
  5. 'Exit Sub
  6. End If
  7. End Sub
  8.  
But the problem is that the cursor does not comes back to FirstName text box. Can anyone help me on this....

Sajit
Feb 15 '08 #1
11 24974
Delerna
1,134 Recognized Expert Top Contributor
add
[code]
me.FirstName.se lected
[code]

immediately after the line
Expand|Select|Wrap|Line Numbers
  1. Me.FirstName.SetFocus
  2.  
I think its selected, intellisense will point you to the correct property


I am new to access and i am trying to build a database for enntering Subscription details of a particular journal. Wat I want is that FirstName field on the form should not be left blank. If the user does not enters the First Name and moves to the field, there should be msgbox prompt, prompting the user that the name cannot be left blank and the cursor should be back on the Firstname text box.

I have tried using the foll code:

Private Sub FirstName_LostF ocus()
If IsNull(Me.First Name) Then
MsgBox "First Name cannot be left blank"
Me.FirstName.Se tFocus
'Exit Sub
End If
End Sub

But the problem is that the cursor does not comes back to FirstName text box. Can anyone help me on this....

Sajit
Feb 15 '08 #2
missinglinq
3,532 Recognized Expert Specialist
Your code is correct, but there's a major flaw in your logic here, Sajit! You want to insure that the first name field has data in it, but you're tying your validation code to the LostFocus event of your first name textbox. What happens if your user never tabs/clicks into the first name textbox and it never has focus ? Your user could then save the record without a first name being entered! When checking to see if a field is empty, you need to place your code at the form level, in the form's BeforeUpdate event. This way, if the user simply skips over the textbox completely, they'll still be forced to fill in the box before the record can be saved!

Remove the code from the LostFocus event and place it here, adding the line Cancel = True, which essentially tells Access that the record is not ready to be saved yet.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If IsNull(Me.FirstName) Then
  3.   MsgBox "First Name cannot be left blank"
  4.   Cancel = True
  5.   Me.FirstName.SetFocus
  6. End If
  7. End Sub
  8.  
Welcome to TheScripts!

Linq ;0)>
Feb 15 '08 #3
missinglinq
3,532 Recognized Expert Specialist
BTW, the Selected property in Access VBA, at least up thru version 2003, is only associated with listboxes, not textboxes. I realized after addressing where the validation code needs to go that I didn't explain why the SetFocus didn't work on the FirstName_LostF ocus event.

One of the peculiarities of Access is that there are some events, such as LostFocus and OnExit, where one would assume that the control has, indeed, lost focus, and thus focus could be reset to the control. In fact, in these events the focus is sort of betwixt and between! The control doesn't have focus (i.e. the cursor doesn't show within the textbox) but you can't set focus to it either! What you have to do, when you run into this situation, is to set focus to another control then set focus back on the desired control. Just another of the mysteries of Access!

Linq ;0)>
Feb 15 '08 #4
sajitk
77 New Member
Thank you,

it works....

Sajit
Feb 15 '08 #5
missinglinq
3,532 Recognized Expert Specialist
Glad we could help!

Linq ;0)>
Feb 15 '08 #6
truthlover
107 New Member
Hi Linq

I'm having the same issue. I understand what you've explained, but I was hoping for a solution that didnt require the person entering the data to go enter the whole form before they found that they are missing required data (after all, setting the properties as required will throw up a message if you try to proceede).

I also tried using the validation property but I cant get that to work for me either.

My database is supposed to go live today, so your help would be greatly appreciated.

Thanks!

Your code is correct, but there's a major flaw in your logic here, Sajit! You want to insure that the first name field has data in it, but you're tying your validation code to the LostFocus event of your first name textbox. What happens if your user never tabs/clicks into the first name textbox and it never has focus ? Your user could then save the record without a first name being entered! When checking to see if a field is empty, you need to place your code at the form level, in the form's BeforeUpdate event. This way, if the user simply skips over the textbox completely, they'll still be forced to fill in the box before the record can be saved!

Remove the code from the LostFocus event and place it here, adding the line Cancel = True, which essentially tells Access that the record is not ready to be saved yet.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If IsNull(Me.FirstName) Then
  3. MsgBox "First Name cannot be left blank"
  4. Cancel = True
  5. Me.FirstName.SetFocus
  6. End If
  7. End Sub
  8.  
Welcome to TheScripts!

Linq ;0)>
Feb 20 '08 #7
missinglinq
3,532 Recognized Expert Specialist
You can do it on a control by control basis, but in order to do so, you have to insure that the user actually goes to the control(s) and the only way to do this is to

  1. Set focus on a specific control anytime you move to a record
  2. Use the OnExit event for that control to insure data is entered and then force the cursor to move to the next control in line
  3. Repeat this process for each and every textbox until all of the required textboxes have been cycled thru. If you have option groups or comboboxes/listboxes where a choice has to be made, they'll have to be included also.
Remember, as shown in the code below, when coming to the final required control, do not set focus anywhere! This is needed so that the user can then use the mouse to click on New Record, Exit, etc.

Doing this for a few controls might not be bad, but it could get quite tedious for a large number of them.

You're probably going to aggravate your end users no end. You'll be effectively keeping them from using the mouse to navigate around the form, forcing them to enter data in the order that you think it should be entered, not neccessarily in the order that they feel it should be entered.

Also, if you need a way for a user to dump a record without saving it, you're going to have to provide a way for them to do so without using the mouse to click on a button.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.   TextBox1.SetFocus
  3. End Sub
  4.  
  5. Private Sub TextBox1_Exit(Cancel As Integer)
  6.  If IsNull(Me.TextBox1) Then
  7.   MsgBox "TextBox1 cannot be left blank"
  8.   Cancel = True
  9.  Else
  10.   TextBox2.SetFocus
  11.  End If
  12. End Sub
  13.  
  14. Private Sub TextBox2_Exit(Cancel As Integer)
  15.  If IsNull(Me.TextBox2) Then
  16.   MsgBox "TextBox2 cannot be left blank"
  17.   Cancel = True
  18.  Else
  19.   ComboBox1.SetFocus
  20.  End If
  21. End Sub
  22.  
  23. Private Sub ComboBox1_Exit(Cancel As Integer)
  24.  TextBox3.SetFocus
  25. End Sub
  26.  
  27. Private Sub TextBox3_Exit(Cancel As Integer)
  28.  If IsNull(Me.TextBox3) Then
  29.   MsgBox "TextBox3 cannot be left blank"
  30.   Cancel = True
  31.  Else
  32.   'If this is the last control, you do not force focus anywhere
  33.  End If
  34. End Sub
Linq ;0)>
Feb 20 '08 #8
truthlover
107 New Member
Actually, the field in question is only the third field on the form so if I force two controls, it shouldnt be a big deal. I would think it would be better than an annoying popup happening *every* time they come to the field or going through the whole form only to have to dump it in the end.

I'll give it a try and see how it goes.

Thanks!


You can do it on a control by control basis, but in order to do so, you have to insure that the user actually goes to the control(s) and the only way to do this is to
  1. Set focus on a specific control anytime you move to a record
  2. Use the OnExit event for that control to insure data is entered and then force the cursor to move to the next control in line
  3. Repeat this process for each and every textbox until all of the required textboxes have been cycled thru. If you have option groups or comboboxes/listboxes where a choice has to be made, they'll have to be included also.
Remember, as shown in the code below, when coming to the final required control, do not set focus anywhere! This is needed so that the user can then use the mouse to click on New Record, Exit, etc.

Doing this for a few controls might not be bad, but it could get quite tedious for a large number of them.

You're probably going to aggravate your end users no end. You'll be effectively keeping them from using the mouse to navigate around the form, forcing them to enter data in the order that you think it should be entered, not neccessarily in the order that they feel it should be entered.

Also, if you need a way for a user to dump a record without saving it, you're going to have to provide a way for them to do so without using the mouse to click on a button.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. TextBox1.SetFocus
  3. End Sub
  4.  
  5. Private Sub TextBox1_Exit(Cancel As Integer)
  6. If IsNull(Me.TextBox1) Then
  7. MsgBox "TextBox1 cannot be left blank"
  8. Cancel = True
  9. Else
  10. TextBox2.SetFocus
  11. End If
  12. End Sub
  13.  
  14. Private Sub TextBox2_Exit(Cancel As Integer)
  15. If IsNull(Me.TextBox2) Then
  16. MsgBox "TextBox2 cannot be left blank"
  17. Cancel = True
  18. Else
  19. ComboBox1.SetFocus
  20. End If
  21. End Sub
  22.  
  23. Private Sub ComboBox1_Exit(Cancel As Integer)
  24. TextBox3.SetFocus
  25. End Sub
  26.  
  27. Private Sub TextBox3_Exit(Cancel As Integer)
  28. If IsNull(Me.TextBox3) Then
  29. MsgBox "TextBox3 cannot be left blank"
  30. Cancel = True
  31. Else
  32. 'If this is the last control, you do not force focus anywhere
  33. End If
  34. End Sub
Linq ;0)>
Feb 20 '08 #9
truthlover
107 New Member
Well, I implemented the code and it seems to be working pretty well. You're right that it would be annoying to have more than a few controls forced, but it's only effecting 3 fields and it's still much better than my previous solution.

Thanks again for your help!

Actually, the field in question is only the third field on the form so if I force two controls, it shouldnt be a big deal. I would think it would be better than an annoying popup happening *every* time they come to the field or going through the whole form only to have to dump it in the end.

I'll give it a try and see how it goes.

Thanks!
Feb 21 '08 #10

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

Similar topics

5
24611
by: Krechting | last post by:
Hi ALl, I have a code that checks if the documents in a hyperlink field are still where they should be. I use fileexist(). First I want to filter out all the hyperlink fields that are empty. I open a recordset and browse through all the fields to check for the word file. I cannot filter out the empty fields. I have tried: If...
2
1741
by: jtvc | last post by:
I'm trying to insert a record on an sql server database table that has among others a text type field. The insert happens without errors and all the fields show the correct information except for the text field when the number of characters is larger than 900. In this case the text field appears empty. If the text is less or equal to 900...
2
1529
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 Command80_Click() Me.Notes = Me.Notes & vbCrLf & Format(Date, "dd-mm-yyyy") & " ------ " Notes.SetFocus Notes.SelStart = Len(Notes.Value) End Sub
3
14901
by: jmarr02s | last post by:
Hi, How do I replace with when the field is empty? Thanks! jmarr02s
1
1858
by: 1mrlance | last post by:
Hello, In order to clean up a report's appearance, I want to drop the label if the associated field is empty. I'm currently exporting the report to word and then editing out 'blanks', but this is time-consuming. What do I need to do to the label in the subreports to not print if the field is empty? (or do I mean nil?) Thanks
1
14452
by: olivier.wambacq | last post by:
hello, This sounds simple but I can't seem to manage... So I'm in a form, and have got a button to "open" (make visible) a subform. the subform actually contains fields from the same table as the big form. but I want the open button to be disabled if a field in the subform is empty.
4
2194
by: mchlle | last post by:
I have this code that will run as long as all the fields below have data in them. How can I get the code to run if at least one field has data in it. strBCCEmail = "1" & & "@faxmail.com; " & & "; " & "1" & & "@faxmail.com; " & Thanks
8
8355
by: ReadEh | last post by:
I am working on a database in Access 97 that contains over 300 tables. I need to produce a list of tables that contain a particular field (e.g all the tables that contain a field called D.O.B). I'm not interested in the value(s) contained within the field, just that the table contains that field. I'm a bit of a novice at using Access so...
3
11204
by: Coll | last post by:
I inherited a database. On one of the forms, a bunch of fields on the form are to be updated with data from a combo box containing many columns after one particular field is updated. I'm receiving an error message "Field cannot be a zero length string." I do have some records with null values for a few fields - no way around that. How can I...
0
7459
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...
0
7726
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. ...
1
7485
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...
0
6052
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...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
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...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
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...

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.