Help with Data Present for a access program

S

Sheria

Hi, I am working on a program and I wanted to have the record set up to where
each field has to be entered in order to exit the record. Some one told me
about using Data Present, but i have never heard of data present. Could
someone help me out.

Thank you
Sheria
 
D

Douglas J Steele

I've never heard of it either!

What you can do is put logic in the form's BeforeInsert event to check each
field. Something like:

Private Sub Form_BeforeInsert(Cancel As Integer)

Dim strMessage As String

If Len(Me.Field1 & "") = 0 Then
Cancel = True
strMessage = strMessage & "Field1 has no data." & vbCrLf
End If

If Len(Me.Field2 & "") = 0 Then
Cancel = True
strMessage = strMessage & "Field2 has no data." & vbCrLf
End If

If Len(strMessage) > 0 Then
MsgBox strMessage
End If

End Sub

The inclusion of Cancel = True means that the insertion will not take place
if one or more empty fields is found.
 
V

Van T. Dinh

I think you meant "Data Validation" which includes checking for data entry
also.
 
S

Sheria

Thank you, one question will this work for check boxes as well I have a total
of 4 and at least one will need to be checked before exiting the record.
 
S

Sheria

Oh ok, I am not really sure, someone just told me that I need to write a code
for Data Present.
 
D

Douglas J Steele

You'd have to modify the code slightly.

If (Me.Check1 And Me.Check2 And Me.Check3 And Me.Check4) = False Then
Cancel = True
strMessage = strMessage & "You haven't selected any checkboxes." &
vbCrLf
End If

Another way of expressing that is

If (Me.Check1 + Me.Check2 + Me.Check3 + Me.Check4) = 0 Then
Cancel = True
strMessage = strMessage & "You haven't selected any checkboxes." &
vbCrLf
End If
 
S

Sheria

Hi, I tried the code and nothing happend, I tried both of them an error
message keeps coming up with the 2nd & sign, so I did not use the 2nd & sign.
No errors have appeared so far, but the code is just not working. I will
continue trying to see if it will work. Thank you for your help
 
D

Douglas J Steele

Hard to tell, but it looks as though there was word-wrap in what I posted.

The vbCrLf should be on the same line as the strMessage = strMessage & ....
stuff

What do you mean by "the code is just not working"?

Post exactly what code you're using. What is happening, and what should be
happening?
 
S

Sheria

Private Sub Form_BeforeInsert(Cancel As Integer)
Dim strMessage As String

If (Me.Incoming_Call + Me.Outgoing_Call + Me.Incoming_Email +
Me.Return_Call) = 0 Then
Cancel = True
strMessage = strMessage & "You have to select a check box." & vbCrLF
End If

End Sub

When a Rep tries to go to another record without checking a box, the message
should appear and not continue to the next record (What should happen). But
with the code if the check boxes are not check, you are still able to go to
another record (that's what happening at this point).

Thanks Again
 
D

Douglas J. Steele

That was intended to be part of a larger series of checking.

First of all, you need to put code

If Len(strMessage) > 0 Then
MsgBox strMessage
End If

before the End Sub.

Does it actually insert the row though?
 
S

Sheria

Thank you I will try that with the code, yes the rows are inserted, but there
should be a msg box before the insert.

Thank you again
 
H

Harvey Thompson

Sheria said:
Thank you I will try that with the code, yes the rows are inserted,
but there should be a msg box before the insert.

PMFJI
From: Help | Contents | Programming Information | When Events Occur | Find
out when events occur

--------------------------------------------------------

Order of events for records on forms

<snip>

Creating a new record
When you move the focus to a new (blank) record on a form and then create a
new record by typing in a control, the following sequence of events occurs:

Current (form) Þ Enter (control) Þ GotFocus (control) Þ BeforeInsert (form)
Þ AfterInsert (form)

The BeforeUpdate and AfterUpdate events for the controls on the form and for
the new record occur after the BeforeInsert event and before the AfterInsert
event.
 
Top