Should this work? new record error.

G

Guest

Hello,
Before I start, here are my system specs.
WinXP sp2
MS Office 2003 Pro
Ms Office Access XP

Now, my problem. I created a db in officeXP. The form has 2 fields:
FileNameD_ID
Designation

The On_Open and Current events have the following items:
Me.AllowAdditions = False
Me.AllowEdits = False

On the form I have a Create Record button with the following code:

Private Sub cmdRecordCreate_Click()
On Error goto errHandle:

Me.AllowAdditions = True

DoCmd.GoToRecord, , acNewRec

errHandle:
Call CatchError

End Sub

When I click the button, I get this:
Error number 2105. You can't go to the specified record.

I double checked the tables and I can add records there. In the Open_Form
event I have this:
Private Sub Form_Open(Cancel As Integer)

DoCmd.MoveSize Width:=Me.Width, Height:=Me.FormHeader.Height +
Me.Detail.Height + Me.FormHeader.Height + 800
Me.cmdRecordSave.Enabled = False
Me.lblStatus.Caption = "<< READ-ONLY >>"
Me.lblStatus.ForeColor = 8421504

End Sub

That's all the code I have on the form. I thought this would work. What am I
missing.
Thank you in advance.
-me
 
D

Dirk Goldgar

Me said:
Hello,
Before I start, here are my system specs.
WinXP sp2
MS Office 2003 Pro
Ms Office Access XP

Now, my problem. I created a db in officeXP. The form has 2 fields:
FileNameD_ID
Designation

The On_Open and Current events have the following items:
Me.AllowAdditions = False
Me.AllowEdits = False

On the form I have a Create Record button with the following code:

Private Sub cmdRecordCreate_Click()
On Error goto errHandle:

Me.AllowAdditions = True

DoCmd.GoToRecord, , acNewRec

errHandle:
Call CatchError

End Sub

When I click the button, I get this:
Error number 2105. You can't go to the specified record.

I double checked the tables and I can add records there. In the
Open_Form event I have this:
Private Sub Form_Open(Cancel As Integer)

DoCmd.MoveSize Width:=Me.Width, Height:=Me.FormHeader.Height +
Me.Detail.Height + Me.FormHeader.Height + 800
Me.cmdRecordSave.Enabled = False
Me.lblStatus.Caption = "<< READ-ONLY >>"
Me.lblStatus.ForeColor = 8421504

End Sub

That's all the code I have on the form. I thought this would work.
What am I missing.
Thank you in advance.
-me

I imagine that it's because, when you execute

DoCmd.GoToRecord, , acNewRec

.... you arrive at the new record, the form's Current event fires, the
form's AllowAdditions property is set back to False, and the "new
record" goes away again.

I suggest you do not set AllowAdditions to False in the form's Current
event. Instead, reset that property in the form's AfterUpdate or
AfterInsert event.
 
G

Guest

I just want the form set to AllowEdits=false & AllowAdditions=false if the
user moves to a different record.
Your saying I can do this using the form's AfterUpdate or AfterInsert?
How are these to different?
Again Thank you.


-----------------------------------------------------
 
G

Guest

Sorry, that should read, "How are these two different?
Well, I tried the AfterInsert.
I can now create a new record, but AllowAdditions=true.
I want the form set to AllowAdditions=false "IF" the user moves to an
existing record. How do I do this if not in the Form_Current event?
Thank you.
 
D

Dirk Goldgar

Me said:
I just want the form set to AllowEdits=false & AllowAdditions=false
if the user moves to a different record.
Your saying I can do this using the form's AfterUpdate or AfterInsert?
How are these to different?

AfterUpdate is raised whenever a modified record is saved. AfterInsert
is raised only if that record is a new record.
 
G

Guest

AfterUpdate only fires when an existing record is changed.

AfterInsert is only fired when a new record is added.

I think.... ;^)
 
D

Dirk Goldgar

Me said:
Sorry, that should read, "How are these two different?
Well, I tried the AfterInsert.
I can now create a new record, but AllowAdditions=true.
I want the form set to AllowAdditions=false "IF" the user moves to an
existing record. How do I do this if not in the Form_Current event?

If you set AllowAdditions to False in the form's AfterInsert event, then
once the new record is saved, the form won't allow another addition
until your cmdRecordCreate button is clicked again. However, I can see
a possible problem if the user clicks the button, is positioned at the
new record, and then moves to another record without editing the new one
in any way.

I would probably take a different approach to what you're doing, but you
can make your current approach work by checking, in the Current event,
whether you're on a new record or not. You could write it like this:

'----- start of code -----
Private Sub Form_Current()

Me.AllowEdits = False

If Not Me.NewRecord Then
Me.AllowAdditions = False
End If

End Sub
'----- end of code -----

That should work, and you can forget about the AfterInsert event.
 
D

Dirk Goldgar

Dennis said:
AfterUpdate only fires when an existing record is changed.

AfterInsert is only fired when a new record is added.

You're right about AfterInsert, but AfterUpdate fires when a record is
saved, whether it's a new record or not.
 

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

Similar Threads


Top