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
11 24956 Delerna 1,134
Recognized Expert Top Contributor
add
[code]
me.FirstName.selected
[code]
immediately after the line
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
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. - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If IsNull(Me.FirstName) Then
-
MsgBox "First Name cannot be left blank"
-
Cancel = True
-
Me.FirstName.SetFocus
-
End If
-
End Sub
-
Welcome to TheScripts!
Linq ;0)>
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)>
Thank you,
it works....
Sajit
Glad we could help!
Linq ;0)>
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. - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If IsNull(Me.FirstName) Then
-
MsgBox "First Name cannot be left blank"
-
Cancel = True
-
Me.FirstName.SetFocus
-
End If
-
End Sub
-
Welcome to TheScripts!
Linq ;0)>
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 - Set focus on a specific control anytime you move to a record
- 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
- 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. - Private Sub Form_Current()
-
TextBox1.SetFocus
-
End Sub
-
-
Private Sub TextBox1_Exit(Cancel As Integer)
-
If IsNull(Me.TextBox1) Then
-
MsgBox "TextBox1 cannot be left blank"
-
Cancel = True
-
Else
-
TextBox2.SetFocus
-
End If
-
End Sub
-
-
Private Sub TextBox2_Exit(Cancel As Integer)
-
If IsNull(Me.TextBox2) Then
-
MsgBox "TextBox2 cannot be left blank"
-
Cancel = True
-
Else
-
ComboBox1.SetFocus
-
End If
-
End Sub
-
-
Private Sub ComboBox1_Exit(Cancel As Integer)
-
TextBox3.SetFocus
-
End Sub
-
-
Private Sub TextBox3_Exit(Cancel As Integer)
-
If IsNull(Me.TextBox3) Then
-
MsgBox "TextBox3 cannot be left blank"
-
Cancel = True
-
Else
-
'If this is the last control, you do not force focus anywhere
-
End If
-
End Sub
Linq ;0)>
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 - Set focus on a specific control anytime you move to a record
- 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
- 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. - Private Sub Form_Current()
-
TextBox1.SetFocus
-
End Sub
-
-
Private Sub TextBox1_Exit(Cancel As Integer)
-
If IsNull(Me.TextBox1) Then
-
MsgBox "TextBox1 cannot be left blank"
-
Cancel = True
-
Else
-
TextBox2.SetFocus
-
End If
-
End Sub
-
-
Private Sub TextBox2_Exit(Cancel As Integer)
-
If IsNull(Me.TextBox2) Then
-
MsgBox "TextBox2 cannot be left blank"
-
Cancel = True
-
Else
-
ComboBox1.SetFocus
-
End If
-
End Sub
-
-
Private Sub ComboBox1_Exit(Cancel As Integer)
-
TextBox3.SetFocus
-
End Sub
-
-
Private Sub TextBox3_Exit(Cancel As Integer)
-
If IsNull(Me.TextBox3) Then
-
MsgBox "TextBox3 cannot be left blank"
-
Cancel = True
-
Else
-
'If this is the last control, you do not force focus anywhere
-
End If
-
End Sub
Linq ;0)>
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!
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)>
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: - Private Sub Form_Current()
-
Me.FirstTextbox.SetFocus
-
End Sub
Linq ;0)> Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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
|
by: jmarr02s |
last post by:
Hi,
How do I replace with when the field is empty?
Thanks!
jmarr02s
|
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
| |
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.
|
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
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
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...
| |