PC Review


Reply
Thread Tools Rate Thread

Access2000: Default values and new record

 
 
Arvi Laanemets
Guest
Posts: n/a
 
      18th Feb 2006
Hi

Without any default values, when user is on new record, no other new records
can be created. When the user leaves the new record without entering
anything, the record is not created.

Now I have a form, where several cells have default values - one of them on
table-level, others on form level. And when the user doesn't enter anything,
the new record is saved when he leaves it. Even worse - he can create
unlimited number of new records with only default values entered
automatically.

Is there some easy solution for this problem, short of using the design with
all controls on form unbound (read data - edit data - save edited data)?


Thanks in advance!
Arvi Laanemets


 
Reply With Quote
 
 
 
 
Brendan Reynolds
Guest
Posts: n/a
 
      18th Feb 2006
There are a number of possible solutions.

You could ensure that you have at least one required field with no default
value.

You could add code to the Form_BeforeUpdate event procedure to compare the
values of the controls against their default values, and if they match set
Cancel = True. Something like so ...

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Trim$(Me.Text0 & vbNullString) = Me.Text0.DefaultValue Then
If Trim$(Me.Text2 & vbNullString) = Me.Text2.DefaultValue Then
Cancel = True
End If
End If

End Sub

You could use a table-level validation rule. Something like ...

Field1 <> 0 Or Field2 <> 'x'

.... where 0 and 'x' are the default values.

--
Brendan Reynolds
Access MVP

"Arvi Laanemets" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> Without any default values, when user is on new record, no other new
> records
> can be created. When the user leaves the new record without entering
> anything, the record is not created.
>
> Now I have a form, where several cells have default values - one of them
> on
> table-level, others on form level. And when the user doesn't enter
> anything,
> the new record is saved when he leaves it. Even worse - he can create
> unlimited number of new records with only default values entered
> automatically.
>
> Is there some easy solution for this problem, short of using the design
> with
> all controls on form unbound (read data - edit data - save edited data)?
>
>
> Thanks in advance!
> Arvi Laanemets
>
>



 
Reply With Quote
 
Arvi Laanemets
Guest
Posts: n/a
 
      18th Feb 2006
Hi Brendan

Thanks, it almost works now (I used AfterUpdate event - because some default
values are inserted by code in form, I probably can't use table level
validation here). But when I am on new record, then whenever I try to
navigate to some other record, I'm returned to new record. I have to press
Esc at first, and only then I can move to another record. Can I correct this
also?


Arvi Laanemets


PS. There are 3 Form-level events for this form: Current, BeforeUpdate, and
OnLostFocus. They are listed here below in somewhat simplified form with
comments.

Private Sub Form_Current()
...
If Nz(txtField1, 0) = 0 Then
' A default value for bound control is calculated - when the control
is empty
' , the calculated value from unbound control is inserted
txtField1 = txtUnbound1
End If
...
' Values for some controls are calculated
' (I had to save some calculated values to table to avoid infinite
calculation loops)
Me.txtField2 = Me.txtUnbound2
...
' The row source for a combo is recalculated for every row
Me.cbbTankimine.RowSource = "SELECT ..."
Me.cbbTankimine.Requery
...
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
' It is your advice used here. As the form is for registering car routes,
any record without car id is abundant
If Nz(Me.cbbCarId, 0) = 0 Then
Cancel = True
End If
End Sub

Private Sub Form_LostFocus()
If Nz(Me.cbbCarID,0) <> 0 Then
Me.txtField3 = Me.txtUnbound3
Me.Dirty = False
End Id
End Sub

"Brendan Reynolds" <(E-Mail Removed)> wrote in message
news:uh$(E-Mail Removed)...
> There are a number of possible solutions.
>
> You could ensure that you have at least one required field with no default
> value.
>
> You could add code to the Form_BeforeUpdate event procedure to compare the
> values of the controls against their default values, and if they match set
> Cancel = True. Something like so ...
>
> Private Sub Form_BeforeUpdate(Cancel As Integer)
>
> If Trim$(Me.Text0 & vbNullString) = Me.Text0.DefaultValue Then
> If Trim$(Me.Text2 & vbNullString) = Me.Text2.DefaultValue Then
> Cancel = True
> End If
> End If
>
> End Sub
>
> You could use a table-level validation rule. Something like ...
>
> Field1 <> 0 Or Field2 <> 'x'
>
> ... where 0 and 'x' are the default values.
>
> --
> Brendan Reynolds
> Access MVP
>
> "Arvi Laanemets" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi
> >
> > Without any default values, when user is on new record, no other new
> > records
> > can be created. When the user leaves the new record without entering
> > anything, the record is not created.
> >
> > Now I have a form, where several cells have default values - one of them
> > on
> > table-level, others on form level. And when the user doesn't enter
> > anything,
> > the new record is saved when he leaves it. Even worse - he can create
> > unlimited number of new records with only default values entered
> > automatically.
> >
> > Is there some easy solution for this problem, short of using the design
> > with
> > all controls on form unbound (read data - edit data - save edited data)?
> >
> >
> > Thanks in advance!
> > Arvi Laanemets
> >
> >

>
>




 
Reply With Quote
 
Brendan Reynolds
Guest
Posts: n/a
 
      18th Feb 2006
Try adding a Me.Undo ...

If Nz(Me.cbbCarId, 0) = 0 Then
Cancel = True
Me.Undo
End If

Also, on reflection it occurs to me that the Form_BeforeInsert event
procedure might be a better place for this code than the Form_BeforeUpdate
event procedure. The BeforeUpdate event is fired when editing existing
records as well as when inserting new ones. The BeforeInsert event is fired
only when inserting a new record, which is what we want.

BTW: The LostFocus event of a form is never fired if the form includes any
controls capable of receiving focus, e.g. any enabled text box, combo box,
list box, command button, check box, toggle button or option button. So the
code in the Form_LostFocus event procedure is probably redundant.

--
Brendan Reynolds
Access MVP

"Arvi Laanemets" <(E-Mail Removed)> wrote in message
news:OKsZx%(E-Mail Removed)...
> Hi Brendan
>
> Thanks, it almost works now (I used AfterUpdate event - because some
> default
> values are inserted by code in form, I probably can't use table level
> validation here). But when I am on new record, then whenever I try to
> navigate to some other record, I'm returned to new record. I have to press
> Esc at first, and only then I can move to another record. Can I correct
> this
> also?
>
>
> Arvi Laanemets
>
>
> PS. There are 3 Form-level events for this form: Current, BeforeUpdate,
> and
> OnLostFocus. They are listed here below in somewhat simplified form with
> comments.
>
> Private Sub Form_Current()
> ...
> If Nz(txtField1, 0) = 0 Then
> ' A default value for bound control is calculated - when the
> control
> is empty
> ' , the calculated value from unbound control is inserted
> txtField1 = txtUnbound1
> End If
> ...
> ' Values for some controls are calculated
> ' (I had to save some calculated values to table to avoid infinite
> calculation loops)
> Me.txtField2 = Me.txtUnbound2
> ...
> ' The row source for a combo is recalculated for every row
> Me.cbbTankimine.RowSource = "SELECT ..."
> Me.cbbTankimine.Requery
> ...
> End Sub
>
> Private Sub Form_BeforeUpdate(Cancel As Integer)
> ' It is your advice used here. As the form is for registering car routes,
> any record without car id is abundant
> If Nz(Me.cbbCarId, 0) = 0 Then
> Cancel = True
> End If
> End Sub
>
> Private Sub Form_LostFocus()
> If Nz(Me.cbbCarID,0) <> 0 Then
> Me.txtField3 = Me.txtUnbound3
> Me.Dirty = False
> End Id
> End Sub
>
> "Brendan Reynolds" <(E-Mail Removed)> wrote in message
> news:uh$(E-Mail Removed)...
>> There are a number of possible solutions.
>>
>> You could ensure that you have at least one required field with no
>> default
>> value.
>>
>> You could add code to the Form_BeforeUpdate event procedure to compare
>> the
>> values of the controls against their default values, and if they match
>> set
>> Cancel = True. Something like so ...
>>
>> Private Sub Form_BeforeUpdate(Cancel As Integer)
>>
>> If Trim$(Me.Text0 & vbNullString) = Me.Text0.DefaultValue Then
>> If Trim$(Me.Text2 & vbNullString) = Me.Text2.DefaultValue Then
>> Cancel = True
>> End If
>> End If
>>
>> End Sub
>>
>> You could use a table-level validation rule. Something like ...
>>
>> Field1 <> 0 Or Field2 <> 'x'
>>
>> ... where 0 and 'x' are the default values.
>>
>> --
>> Brendan Reynolds
>> Access MVP
>>
>> "Arvi Laanemets" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > Hi
>> >
>> > Without any default values, when user is on new record, no other new
>> > records
>> > can be created. When the user leaves the new record without entering
>> > anything, the record is not created.
>> >
>> > Now I have a form, where several cells have default values - one of
>> > them
>> > on
>> > table-level, others on form level. And when the user doesn't enter
>> > anything,
>> > the new record is saved when he leaves it. Even worse - he can create
>> > unlimited number of new records with only default values entered
>> > automatically.
>> >
>> > Is there some easy solution for this problem, short of using the design
>> > with
>> > all controls on form unbound (read data - edit data - save edited
>> > data)?
>> >
>> >
>> > Thanks in advance!
>> > Arvi Laanemets
>> >
>> >

>>
>>

>
>
>



 
Reply With Quote
 
Arvi Laanemets
Guest
Posts: n/a
 
      18th Feb 2006
Thanks, it worked with both events. At moment I decided to use BeforeInsert
event - ofcourse it may be usefult when abundant entries anywhere are
removed automatically, but I'm afraid it's slippery ground there.

The LostFocus event is there probably, because I needed to save a record,
before another form is activated (I have an unbound multi-page form with
separate forms on different pages. Some of them get p.e. record sources for
combos from tables, which are sources for other forms). I tried various
events to save form's source data when another sheet/form is activated - I'm
not sure at moment, is this LostFocus event simply remained undeleted, or is
it really needed. I'll check it later.


Arvi Laanemets


"Brendan Reynolds" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Try adding a Me.Undo ...
>
> If Nz(Me.cbbCarId, 0) = 0 Then
> Cancel = True
> Me.Undo
> End If
>
> Also, on reflection it occurs to me that the Form_BeforeInsert event
> procedure might be a better place for this code than the Form_BeforeUpdate
> event procedure. The BeforeUpdate event is fired when editing existing
> records as well as when inserting new ones. The BeforeInsert event is

fired
> only when inserting a new record, which is what we want.
>
> BTW: The LostFocus event of a form is never fired if the form includes any
> controls capable of receiving focus, e.g. any enabled text box, combo box,
> list box, command button, check box, toggle button or option button. So

the
> code in the Form_LostFocus event procedure is probably redundant.
>
> --
> Brendan Reynolds
> Access MVP
>
> "Arvi Laanemets" <(E-Mail Removed)> wrote in message
> news:OKsZx%(E-Mail Removed)...
> > Hi Brendan
> >
> > Thanks, it almost works now (I used AfterUpdate event - because some
> > default
> > values are inserted by code in form, I probably can't use table level
> > validation here). But when I am on new record, then whenever I try to
> > navigate to some other record, I'm returned to new record. I have to

press
> > Esc at first, and only then I can move to another record. Can I correct
> > this
> > also?
> >
> >
> > Arvi Laanemets
> >
> >
> > PS. There are 3 Form-level events for this form: Current, BeforeUpdate,
> > and
> > OnLostFocus. They are listed here below in somewhat simplified form with
> > comments.
> >
> > Private Sub Form_Current()
> > ...
> > If Nz(txtField1, 0) = 0 Then
> > ' A default value for bound control is calculated - when the
> > control
> > is empty
> > ' , the calculated value from unbound control is inserted
> > txtField1 = txtUnbound1
> > End If
> > ...
> > ' Values for some controls are calculated
> > ' (I had to save some calculated values to table to avoid infinite
> > calculation loops)
> > Me.txtField2 = Me.txtUnbound2
> > ...
> > ' The row source for a combo is recalculated for every row
> > Me.cbbTankimine.RowSource = "SELECT ..."
> > Me.cbbTankimine.Requery
> > ...
> > End Sub
> >
> > Private Sub Form_BeforeUpdate(Cancel As Integer)
> > ' It is your advice used here. As the form is for registering car

routes,
> > any record without car id is abundant
> > If Nz(Me.cbbCarId, 0) = 0 Then
> > Cancel = True
> > End If
> > End Sub
> >
> > Private Sub Form_LostFocus()
> > If Nz(Me.cbbCarID,0) <> 0 Then
> > Me.txtField3 = Me.txtUnbound3
> > Me.Dirty = False
> > End Id
> > End Sub
> >
> > "Brendan Reynolds" <(E-Mail Removed)> wrote in message
> > news:uh$(E-Mail Removed)...
> >> There are a number of possible solutions.
> >>
> >> You could ensure that you have at least one required field with no
> >> default
> >> value.
> >>
> >> You could add code to the Form_BeforeUpdate event procedure to compare
> >> the
> >> values of the controls against their default values, and if they match
> >> set
> >> Cancel = True. Something like so ...
> >>
> >> Private Sub Form_BeforeUpdate(Cancel As Integer)
> >>
> >> If Trim$(Me.Text0 & vbNullString) = Me.Text0.DefaultValue Then
> >> If Trim$(Me.Text2 & vbNullString) = Me.Text2.DefaultValue Then
> >> Cancel = True
> >> End If
> >> End If
> >>
> >> End Sub
> >>
> >> You could use a table-level validation rule. Something like ...
> >>
> >> Field1 <> 0 Or Field2 <> 'x'
> >>
> >> ... where 0 and 'x' are the default values.
> >>
> >> --
> >> Brendan Reynolds
> >> Access MVP
> >>
> >> "Arvi Laanemets" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> > Hi
> >> >
> >> > Without any default values, when user is on new record, no other new
> >> > records
> >> > can be created. When the user leaves the new record without entering
> >> > anything, the record is not created.
> >> >
> >> > Now I have a form, where several cells have default values - one of
> >> > them
> >> > on
> >> > table-level, others on form level. And when the user doesn't enter
> >> > anything,
> >> > the new record is saved when he leaves it. Even worse - he can create
> >> > unlimited number of new records with only default values entered
> >> > automatically.
> >> >
> >> > Is there some easy solution for this problem, short of using the

design
> >> > with
> >> > all controls on form unbound (read data - edit data - save edited
> >> > data)?
> >> >
> >> >
> >> > Thanks in advance!
> >> > Arvi Laanemets
> >> >
> >> >
> >>
> >>

> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert a default record for all values Anthony Microsoft Access Queries 3 12th Mar 2010 02:00 PM
How to save a record with default values??? =?Utf-8?B?aHV6emxlcHV6emxl?= Microsoft Access Forms 5 2nd Mar 2010 02:01 PM
Saving a Record with only default values =?Utf-8?B?R2VvcmdpZUdpcmw=?= Microsoft Access Form Coding 1 20th Jun 2005 07:11 AM
Default values being ignored after added a new record using Datatable EFileTahi-A Badler via DotNetMonster.com Microsoft ADO .NET 0 10th Feb 2005 11:32 AM
how to default values from last record please help! Microsoft Access Forms 3 10th Sep 2004 03:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:48 PM.