473,513 Members | 2,524 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 24956
Delerna
1,134 Recognized Expert Top Contributor
add
[code]
me.FirstName.selected
[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_LostFocus()
If IsNull(Me.FirstName) Then
MsgBox "First Name cannot be left blank"
Me.FirstName.SetFocus
'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_LostFocus 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
sajitk
77 New Member
Hi Linq,

this is actually an old problem.

i have a table called beneficiary which has a field called cost (interger, default value =0). in the form this is the last field for entry. the user enters the value and presses the save button.The record gets saved.

i have written the following code in the forms_before update ()

[PHP]On Error GoTo Err_frm_GotFocus

If Me.cost = 0 Then
MsgBox "Please enter the cost of Solar Lantern", vbInformation, "Beneficiary Entry"
Cancel = True
Me.cost.SetFocus
GoTo Exit_save_Gotfocus
End If


Exit_frm_Gotfocus:
Exit Sub

Err_frm_GotFocus:
MsgBox Err.Description
Resume Exit_frm_Gotfocus[/PHP]

what happens is that, when the saved is clicked, the record is saved and the ADD button gets enabled and the save button gets disabled. when i click the Add button the focus shifts to the cost control. i think is due to the like
[PHP]Me.cost = 0[/PHP] because each time a new form is loaded, the default value for this field is 0.

The writing the abovecode in OnExit () would be tedious at the moment because there are quite a few controls in the form.

Would be great if you could help me on this.
sajit



Glad we could help!

Linq ;0)>
Apr 11 '08 #11
missinglinq
3,532 Recognized Expert Specialist
If you're saying that when you hit the ADD button the new record is pulled up and the focus is in the Cost field, this is normal behavior for Access. Anytime you move from one record to another, the first control to receive focus in Record #2 will be the same control that last had focus on Record #1.

To solve this you simply need to set focus on the first control you want to have focus, every time you move to a different record:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.    Me.FirstTextbox.SetFocus
  3. End Sub
Linq ;0)>
Apr 11 '08 #12

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

Similar topics

5
24598
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
1738
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
1528
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
14898
by: jmarr02s | last post by:
Hi, How do I replace with when the field is empty? Thanks! jmarr02s
1
1857
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
14444
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
2188
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
8354
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
11196
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
7270
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
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
7565
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...
0
7543
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5704
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
4759
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
3255
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
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
473
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.