Custom Message Box to identify blank field (1 Viewer)

randolphoralph

Registered User.
Local time
Today, 04:17
Joined
Aug 4, 2008
Messages
101
I have a command button that users click to add a new record. I wanted to add some code that will check the field UniqueID and Date to ensure that the field is not blank.


Here is what I have so far.

Code:
Private Sub All_Load_Click()
If [UniqueID] = "" Then
MsgBox "Please enter Unique ID"
ElseIf [Date] = "" Then
MsgBox "Please enter Date"
Else
Me.AllowAdditions = True
DoCmd.GoToRecord acActiveDataObject, , acNewRec
Me.AllowAdditions = False
End If
End Sub

If I click the command button and either the UniqueID or Date field are blank a new record is added and no message box is displayed
 
Last edited:

dkinley

Access Hack by Choice
Local time
Today, 04:17
Joined
Jul 29, 2008
Messages
2,016
Here ya go ...

Code:
If Len(Nz(Me.txtUniqueIDFieldName, "")) = 0 
   MsgBox "Please enter Unique ID"
   Me.txtUniqueIDFieldName.SetFocus
ElseIf Len(Nz(Me.txtDateFieldName, "")) = 0 
   MsgBox "Please enter Date"
   Me.txtDateFieldName.SetFocus
Else 
   'what you want to do
End if

Note, you are using a field name of Date, this is a reserved word and you would be prudent to rename it to something (like OrderDate, PubDate, etc.).

Now, if you want to validate the field as being unique, you can look at the last couple of days post and I have seen some regarding this.

HTH,
-dK
 

SteveSchapel

Registered User.
Local time
Today, 21:17
Joined
Apr 29, 2010
Messages
242
Randolph,

Try it like this:

Code:
Private Sub All_Load_Click()
   If IsNull(Me.UniqueID) Then
      MsgBox "Please enter Unique ID"
   ElseIf IsNull(Me![Date]) Then
      MsgBox "Please enter Date"
   Else
   Me.AllowAdditions = True
   DoCmd.GoToRecord acActiveDataObject, , acNewRec
   Me.AllowAdditions = False
   End If
End Sub
 

SteveSchapel

Registered User.
Local time
Today, 21:17
Joined
Apr 29, 2010
Messages
242
Ah... I see DK has already mentioned to you about the use of the word 'Date' as a field name.

I am also surprised to see that you can set AllowAdditions back to False before entering any data... does that actually work ok?
 

randolphoralph

Registered User.
Local time
Today, 04:17
Joined
Aug 4, 2008
Messages
101
I have change name of the Date field. Thanks dk and Steve I was not aware that would cause a issue.

Steve so far I have not had any problems with setting AllowAdditions back to False before entering any data. Could this present a problem? I have tested it and it appears ok.
 

SteveSchapel

Registered User.
Local time
Today, 21:17
Joined
Apr 29, 2010
Messages
242
Randolph,

I have change name of the Date field. Thanks dk and Steve I was not aware that would cause a issue.

There is some good related information here, if you are interested:
http://allenbrowne.com/AppIssueBadWord.html

Steve so far I have not had any problems with setting AllowAdditions back to False before entering any data. Could this present a problem? I have tested it and it appears ok.

I don't know what we are doing different from each other. I just tried it, and (as I expected) it doesn't work for me. The new record opens, the focus goes there, and then the new record hides again and the focus goes back to the last record. So, happy that it works for you, but puzzled about what I'm missing here.
 

Users who are viewing this thread

Top Bottom