cant be blank...my code wrong

lizzieah

Registered User.
Local time
Today, 21:33
Joined
Mar 27, 2003
Messages
11
When a field in form (in this case txtArtist) is blank i want to be able to make it so they cant move on or aware that the field is blank. I wrote the following code but it doesnt seem to work properly, it works after i type a name in the field and then delete it and move on, it comes up with the message. But doesnt show any message if i tab right pass it.

After Update
Dim strMsg As String, strTitle As String
Dim intStyle As Integer
If IsNull(txtArtist) Or txtArtist = "" Then
strMsg = "You must enter Artists name."
strTitle = "Cant be blank"
intStyle = vbOKOnly
MsgBox strMsg, intStyle, strTitle
Cancel = True
End If

HeLP
cheers
 
Use the on exit event and if isnull use same msgbox and then set focus to that textbox.
 
I would take a different approach if you want to ensure that the "Artist" field in the table is NEVER empty (Null). In the design view of the table where the Artist field is...select the Artist field and type this in the Validation Rule: Is Not Null and type this in the Validation Text: The Artist field can not be empty!. Also, the "Allow Zero Length" field should be set to NO.

In regards to you original question, you would want to use the OnLostFocus event of the txtArtist field to test if the txtArtist field is Null and then call the message box if they selected the txtArtist field and left it empty. Also, I would simplify your code like this...
Code:
Private Sub txtArtist_LostFocus()
    
    If IsNull(txtArtist) Or txtArtist = "" Then
        MsgBox "You must enter an Artist's name.", vbCritical, "Invalid Artist Name"
        Me.txtArtist.SetFocus
    End If
    
End Sub
HTH
 
Of course if the user never tabs into the artist field, your code will never execute. So, if you don't set the required property at the table level, you'll never raise an error message at all unless you put your code in the BeforeUpdate event of the FORM.
 
Problem though

Great help guys. Well i thought it was all going well until i reopened access later and it decided it wasnt going to work now, even after i saved it.
This was my new code.

Dim strMsg As String, strTitle As String
Dim intStyle As Integer
If IsNull(txtArtist) Or txtArtist = "" Then
strMsg = "You must enter Artists name."
strTitle = "Cant be blank"
intStyle = vbOKOnly
MsgBox strMsg, intStyle, strTitle
Cancel = True
txtArtist.SetFocus
End If

Whats wrong?
It did work, then no longer.
 
lizzieah,

You didn't post your whole module.

What event did you use?

Pat's suggestion that this be enforced at the table design
level or at the form BeforeUpdate event is valid.

Wayne
 
Im up to here with Access....getting really frustrated....
Ok the above example is just one of the things i had to do. But i was able to do it to the field i called AcquisitionNo.
This code below was actually working...


Private Sub txtAcquisitionNo_BeforeUpdate(Cancel As Integer)

If Len(txtAcquisitionNo.Text) > 6 Then
MsgBox "Incorrect Length"
Cancel = vbCancel
End If
If IsNull(txtAcquisitionNo) Or (txtAcquisitionNo) = "" Then
MsgBox "Cant be Blank"
Cancel = vbCancel
End If
End Sub

Private Sub txtAcquisitionNo_Change()

If Len(txtAcquisitionNo.Text) > 6 Then
MsgBox "Incorrect Length"
End If

End Sub

however after i left and came back, it decides its not going to work....Whats with that?????????
HELP
 
As I mentioned above...
I would take a different approach if you want to ensure that the "Artist" field in the table is NEVER empty (Null). In the design view of the table where the Artist field is...select the Artist field and type this in the Validation Rule: Is Not Null and type this in the Validation Text: The Artist field can not be empty!. Also, the "Allow Zero Length" field should be set to NO.
You would not have to worry about any "events" if you used a Validation Rule for each field in the design of the table. A user will not be able to save a record that does not pass your validation test at the table level if you correctly set up a Validation Rule and prompt the user with a custom message with a Validation Text.

HTH
 

Users who are viewing this thread

Back
Top Bottom