Continuous form / new record

P

Peter

Hi all, I have the following issue..

In a continuous form, for each new record, I have a command button that
“creates†a related record...this command button is visible in the “next
record†even if a record is not created in the current form. How do I
restrict the command button from creating a new related record if the current
record is not created..?

I hope I managed to explain my problem well...

Thanks!
 
T

Tom van Stiphout

On Wed, 20 Jan 2010 23:19:02 -0800, Peter

If you setup your db design correctly, the related (child) record
CANNOT be created until the parent record is created. Enforcing this
"referential integrity" is one of the cornerstones of a relational
database. Open the Relationships form, put both tables on it, draw a
line from the PK to the FK, and check the box to enforce the relation.

With that out of the way, you can test for Me.NewRecord to know if
you're on a new (not yet saved) record:
if Me.NewRecord then
Msgbox "Yo! Don't click me until a parent record exists"
else
'Code to create child record goes here.
end if

-Tom.
Microsoft Access MVP
 
J

Jack Leach

I'm not sure I completely understand, but maybe one of these two things will
help...

If you are trying to show a button visible only on a new record, scratch
that idea. Continuous forms don't allow for any "differences" (other than
data obviously) for each record.

If you want the button only to show when there is a previous record to copy
from (in the same form), then you can check the RecordCount property of the
Form's Recordset...

If Me.Recordset.RecordCount = 0 Then Me.MyButton.Visible = False
(or, if you like...)
Me.MyButton.Visible = Me.Recordset.Recordcount


If, by chance, you are creating a record in a continuous form that is a
subform and you mean "related" record by a record on the Main form, then
there may be a way to handle this as well. In your continous form, save your
form with the button visible property set to false... then, in the Current
and AfterUpdate events of the Main form, use some code to toggle the
visibility of the button...

If Me.NewRecord Then
Me.SubformControl.Form.MyButton.Visible = False
Else
Me.SubformControl.Form.MyButton.Visible = True
End If

I should note though, that copying "related" information to a subform (if
this happens to be the case) is probably a bad practice. It sounds like
maybe you're entering the same data twice (main and sub). There are some
cases (say an item master, to which you create an order that may have a few
differences) where this is ok, but mostly you want to avoid it.

Anyway, not sure exactly what the situation is, but maybe something I've
said will give you an idea or two...

hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
J

Jack Leach

I'm not sure I completely understand, but maybe one of these two things will
help...

If you are trying to show a button visible only on a new record, scratch
that idea. Continuous forms don't allow for any "differences" (other than
data obviously) for each record.

If you want the button only to show when there is a previous record to copy
from (in the same form), then you can check the RecordCount property of the
Form's Recordset...

If Me.Recordset.RecordCount = 0 Then Me.MyButton.Visible = False
(or, if you like...)
Me.MyButton.Visible = Me.Recordset.Recordcount


If, by chance, you are creating a record in a continuous form that is a
subform and you mean "related" record by a record on the Main form, then
there may be a way to handle this as well. In your continous form, save your
form with the button visible property set to false... then, in the Current
and AfterUpdate events of the Main form, use some code to toggle the
visibility of the button...

If Me.NewRecord Then
Me.SubformControl.Form.MyButton.Visible = False
Else
Me.SubformControl.Form.MyButton.Visible = True
End If

I should note though, that copying "related" information to a subform (if
this happens to be the case) is probably a bad practice. It sounds like
maybe you're entering the same data twice (main and sub). There are some
cases (say an item master, to which you create an order that may have a few
differences) where this is ok, but mostly you want to avoid it.

Anyway, not sure exactly what the situation is, but maybe something I've
said will give you an idea or two...

hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
D

Dorian

In button click event, insert a check:
If Me!NewRecord Then
Exit Sub
End If
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 

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