Add New Record Command Button

G

Guest

I have a form which is used for data entry, edit and delete. The forms has
command buttons add new record, edit record, delete record. The add new
record when clicked defaults the date field to current date and enter a new
register number automatically. Noramlly after data entry the users select a
save record button which checks for missing fields. However if I keep
clicking the add new record button it does just incrementing the register
number but the record have no data attached. How can I stop empty records
being added, this is really bugging me as I bet its simple. Many thanks
 
G

Guest

My guess is that part of the coding of your "add record" button is to "save
record". So when you push the "add record" button, it may do like two
things: 1. save (current) record, and 2. Go to NewRecord.

Is it a macro that controls the "on click" event of the button?

CW
 
G

Guest

Another possibility is that your form doesn't require users to "save" the
data. That's the "default" way data entry works in Access anyway. You
enter/edit records and it's automatically "saved". That could, in
conjunction with the below, result in new records even though you haven't
really intentionally altered any of the "new" records.

I think a lot of people attach code to forms such that if the form is
modified, you get a popup asking you if you want to save. You can either say
yes or no. That's the way my forms work most of the time. I've also noticed
that on one of them (forms), even if I don't actually manually enter in data
to a new record it will ask me about saving it when I try to leave or close
the form. I assume that's because one of the fields I have with "default" or
"auto" filled information is causing the form to be "dirty" (considered by
Access to have been modified)

I'd bet money it's one of those couple of options. (macro with save command
or "auto/defult" filled field making the form appear 'dirty' coupled with the
macro save command OR coupled with a database that doesn't require "saving"
records.

Hope something in these two posts is of value.

CW
 
G

Guest

The command buttons were created using the wizard. Below is the coding used
for buttons Add New Record and Save Record. What I need is some way to stop
the form adding empty records to the table.
Private Sub AddNewRec_Click()
On Error GoTo Err_AddNewRec_Click

DoCmd.GoToRecord , , acNewRec

'Private Sub RegisterNumber_Click()

If Me.NewRecord Then
Dim strWhere As String
Dim varResult As Variant

strWhere = "RegisterNumber Like """ & Format(Date, "yy") & "*"""
varResult = DMax("RegisterNumber", "HYInReg", strWhere)

If IsNull(varResult) Then
Me.RegisterNumber = Format(Date, "yy") & "-000001"
Else
Me.RegisterNumber = Left(varResult, 3) & _
Format(Val(Right(varResult, 4)) + 1, "000000")
End If
End If

'Disable/Grey Out the "Edit Record" and "Delete Record" Buttons
Me!EditRec.Enabled = False
Me!RegDelRec.Enabled = False

'Disable/Grey Out the Text Boxes Not Applicable To A New Record
Me!ReasonsforEdit.Enabled = False
Me!PersonReqDel.Enabled = False
Me!DeleteDate.Enabled = False
Me!DeptReqDel.Enabled = False
Me!ReasonforDel.Enabled = False
Me!RegNoIDNo.Enabled = False

Exit_AddNewRec_Click:
Exit Sub

Err_AddNewRec_Click:
MsgBox Err.Description
Resume Exit_AddNewRec_Click


End Sub
***************************************
Private Sub Check_SaveButton_Click()
On Error GoTo Err_Check_SaveButton_Click

If AddNewRec.Enabled Then

If IsNull(Me.Designation) Then
MsgBox conMESSAGE, vbExclamation, "SELECT DESIGNATION BEFORE CONTINUING"
Canel = True
Me.Designation.SetFocus
End If
If IsNull(Me.SentTo) Then
MsgBox conMESSAGE, vbExclamation, "ENTER SENT TO DETAILS BEFORE
CONTINUING"
Canel = True
Me.SentTo.SetFocus
End If
If IsNull(Me.CopiedTo) Then
MsgBox conMESSAGE, vbExclamation, "ENTER COPIED TO DETAILS BEFORE
CONTINUING"
Canel = True
Me.CopiedTo.SetFocus
End If
If IsNull(Me.DateSent) Then
MsgBox conMESSAGE, vbExclamation, "ENTER DATE AS DD/MM/YYYY BEFORE
CONTINUING"
Canel = True
Me.DateSent.SetFocus
End If
If IsNull(Me.CompanyNames) Then
MsgBox conMESSAGE, vbExclamation, "ENTER COMPANY NAME DETAILS BEFORE
CONTINUING"
Canel = True
Me.CompanyNames.SetFocus
End If
If IsNull(Me.Subject) Then
MsgBox conMESSAGE, vbExclamation, "ENTER SUBJECT DETAILS BEFORE
CONTINUING"
Canel = True
Me.Subject.SetFocus
End If
If IsNull(Me.Hyperlink1) Then
MsgBox conMESSAGE, vbExclamation, "ENTER HYPERLINK BEFORE CONTINUING"
Canel = True
Me.Hyperlink1.SetFocus
End If
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End If

''''''''''''''''''''
If Me.EditRec.Enabled And IsNull(Me.ReasonsforEdit) Then
MsgBox conMESSAGE, vbExclamation, "REASONS FOR EDIT MUST BE COMPLETED
BEFORE CONTINUING"
Canel = True
Me.ReasonsforEdit.SetFocus
End If
''''''''''''''''''

Exit_Check_SaveButton_Click:
Exit Sub

Err_Check_SaveButton_Click:
MsgBox Err.Description
Resume Exit_Check_SaveButton_Click
End Sub
 
G

Guest

Sue,
The more I talk about access, the more apparent it becomes that, at least
around here, I shouldn't talk very often about it.

I have three points. Hopefully someone will jump in and correct all of them.

1. I think this is happening: you hit "addnewrec" button, and go to a new
record. Your application adds some fields "automatically (programatically).
These additions are enough, to constitute a "record". When you hit
"addnewrecord" again, access saves your "current" record (which consists
solely of those pieces of data automatically added when you arrived), and
then goes to another "new record". Generally speaking, you don't have to do
anything to "save" a record. The mere act of moving to another record or
closing a form will save a record. Adding a "save" button doesn't, in and of
itself, change that fact.

2. It wouldn't happen quite like it is for you EXCEPT for I believe the
code you posted that attempts to force users to add information for all those
fields is ineffective. I can't say for sure how it SHOULD be done, but I
think I'd just make all the fields you want to require "required" fields in
the underlying table.

Regardless, your relevant code says "IF addnewrec.enabled Then" doesn't seem
like it could possibly be right, mainly because "enabled" must be true or
false. I'm not sure that even if you corrercted that it would say what you
want it to say, but I think it's almost surely created a situation where all
that other code below it in the "then" portion is basically meaningless.
Therefore, you aren't requiring people to fill out all those fields after
all. If you WERE requiring all those fields to be completed, then you
wouldn't be able to just hit the "addnewrec" button to create multiple
meaningless documents because the second time you hit it you'd be told "no,
get back over here and fill out these fields on this form FIRST, then you can
go to a new one".

3. I think you could probably fix the problem by getting rid of all your
attempts to automatically add data to any new record created. Specifically,
I have a feeling that that first part of your code that creates an ID tag
based on a couple of possibilities is what's causing the problem. You could
cut the code out and paste it into a word/text document, remove any reference
to it in the properties window of your form, and then see if you are still
having the problem. It may be some other instance of automatically added
data that's causing the problem, so don't dismiss the notion just because you
find out it's not that particular instance.

This is all speculation, but it's what I'd focus on if I were
trouble-shooting the issue with one of my apps.

I hope something I've said spurs you to figure out what the issue is, or
angers someone else into responding with some quality info for you :)

CW
 
G

Guest

Just to be clear about point #3, I'm not exactly sure how that "register"
button works you've set up, so my point about getting rid of that may be
wrong. The overall point, however, was to eliminate the areas where your
form automatically (programatically) adds field entries to see if those
additions are what makes access believe you've created a new record worthy
of saving.

I've got a form on my current project that if you go to "new record" and
then try to leave without manually adding any data, you get the message I've
set up to make users confirm whether or not to save the "modifications". I'm
almost sure it's because of some automatic field entries I've set up, but I
haven't looked at it yet because of other issues...

Anyway, good luck.
CW
 
G

Guest

Many thanks for the information. I decided as suggested to not automatically
enter the register number and this now works better. Many thanks again for
all your help.
 
L

lizw

Hi, I am new to this Discussion Group. I am in need of some instructions and
hope that someone can help solve my problem/s.

1) I would like to add a record into my datasheet between two existing
records?
2) How do you keep the ID #s in sequence?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top