DefaultValue: When Does it Become Effective?

C

croy

When the DefaultValue for a control is set via VBA, when
does it become effective.

In other words, when the focus moves to a new record, will
code in the form's Current Event that sets a new
DefaultValue show up in this record?

If not, how about if the code is moved to the LostFocus
Event of the last control in the previous record? If still
no, what about the Exit Event of the last control in the
previous record?

If still no, when is the last opportunity to set a default
value and have it effective in the next new record?
 
D

Dale Fye

The best way/time to change a controls default value will vary depending on
your application, and how you are moving from one record to another.

I prefer to use the AfterUpdate event of the control I want to change,
rather than in some other form event.

However, if you don't want to change the default unless the current record
is actually written, then you might want to consider using the Forms
AfterUpdate event (in conjunction with the controls AfterUpdate event. To
see whether the control you want to change the DefaultValue for actually
changed, you can checkout the OldValue and Value properties of the control).

You might also only want to change the default value only if the current
record you are working on is a new record, in which case, you might want to
use the Forms BeforeUpdate event.

IMHO, the best way to figure what will work best for you is to create a
simple form with two textboxes bound to a test table, and try each of the
options you mentioned to see what your results are. This may take longer
than asking the question on a newsgroup, but you will learn a whole lot more
that way.

HTH
Dale
-- ----
When the DefaultValue for a control is set via VBA, when does it become effective?

As soon as you set that value, but it will only be obvious when you move to
the next new record.
In other words, when the focus moves to a new record, will code in the form's Current Event that sets a new DefaultValue show up in this reco

If not, how about if the code is moved to the LostFocus
Event of the last control in the previous record? If still
no, what about the Exit Event of the last control in the
previous record?

If still no, when is the last opportunity to set a default
value and have it effective in the next new record?

Don''t forget to rate the post if it was helpful!

Email address is not valid.
Please reply to newsgroup only.
 
K

Klatuu

The default value property applies to only new records. You can set it in
the form current event:

If Me.NewRecord Then
Me.SomeControl.DefaultValue = "persimmons"
End If
 
C

croy

What exactly are you trying to do?


Well... (remember that *you* asked...) ;-)

I'm trying to make data-entry slick and pretty, in a case
where paper field forms come to the office with Page and
Line numbers filled in, and we want to keep track of those
to make cross-checking more efficient.

So I'm trying to get the DefaultValues for PageNumber and
LineNumber to auto-increment, such that when a set of forms
are entered, the PageNumber and LineNumber both start with
"1", and as the LineNumber increments up to 30, the
PageNumber will increment + 1, and LineNumber reset to "1".
The goal is for data-entry folks to be able to skip right
over those two fields, only having to stop and mouse 'em
when there's some sort of out-of-order case in the paper,
which happens rarely.

If I put the code for this in the form's Current event, then
I have to use DMax to get the values, because there's
nothing in a new record to add 1 to. But if I don't put it
in the form's Current event, then cycling thru records won't
cause the code to fire. I've had various versions of this
working, but it's never been pretty (and I want it pretty!
;-) ). By pretty, I mean that whenever a data-entry person
looks at those fields, they see the proper default values
showing, no matter where they are in the form/subform combo.
The best I've been able to do is get the field to "look
pretty" after the focus lands on the new record and the
Current event fires.

I'm just too picky for my own abilities to keep up with.

Thanks for the interest. I should probably stop bugging
others about this...
 
D

Dale Fye

I think your best bet is to use the forms BeforeUpdate event. Something like:

Private Sub Form_BeforeUpdate(Cancel as integer)

me.txSomeControl.DefaultValue = me.txtSomeControl.Value + 1

End Sub

Then, when you go to the next record, that control should contain the value
you are looking for.

HTH
Dale
 
C

croy

I think your best bet is to use the forms BeforeUpdate event. Something like:

Private Sub Form_BeforeUpdate(Cancel as integer)

me.txSomeControl.DefaultValue = me.txtSomeControl.Value + 1

End Sub

Then, when you go to the next record, that control should contain the value
you are looking for.


Thanks for the reply, Dale.

Yes, that works very good when data-entry is rolling along
normally. But in some fairly common cases, the wheels come
off. If someone gets interupted during data-entry and has
to come back later and pick up in the middle of a paper
form, there's no "update" event to set the DefaultValue for
the first record they need to add.

Aside from that, it is positive and simple (no domain lookup
necessary). I haven't ruled it out, but I'm still hoping
for something that covers all the bases.

Here's what I've settled on for the moment:

If Me.Parent.NewRecord Then
Me![txtCountPage].DefaultValue = 1
Me![txtCountLine].DefaultValue = 1
ElseIf Me![txtCountLine].DefaultValue < 34 Then
Dim strWhere As String
Dim strWhere2 As String
strWhere = "[CountSurvId] = " & _
Me.Parent![CountSurvId]
strWhere2 = "[CountSurvId] = " & _
Me.Parent![CountSurvId] & " _
And [CountPage] = " & _
Me![txtCountPage].DefaultValue
Me![txtCountPage].DefaultValue = _
Nz(DMax("CountPage", "tblCountDetail", _
strWhere), 1)
Me![txtCountLine].DefaultValue = _
Nz(DMax("CountLine", "tblCountDetail", _
strWhere2), 0) + 1
Me![txtCountLine].Requery
ElseIf Me![txtCountLine].DefaultValue >= 34 Then
Dim strWhere3 As String
Dim strWhere4 As String
strWhere3 = "[CountSurvId] = " & _
Me.Parent![CountSurvId]
strWhere4 = "[CountSurvId] = " & _
Me.Parent![CountSurvId] & " And _
[CountPage] = " & _
Me![txtCountPage].DefaultValue
Me![txtCountPage].DefaultValue = _
Nz(DMax("CountPage", "tblCountDetail", _
strWhere3), 1) + 1
Me![txtCountLine].DefaultValue = 1
Me![txtCountLine].Requery
End If


I also put a call for Form_Current on the AfterUpdate event
of those two fields, so that when a crossed-out line on the
paper requires skipping a number, and the data-entry clerk
has to get mousey, the DefaultValues will fix themselves.
 
C

croy

I think your best bet is to use the forms BeforeUpdate event. Something like:

Private Sub Form_BeforeUpdate(Cancel as integer)

me.txSomeControl.DefaultValue = me.txtSomeControl.Value + 1

End Sub

Then, when you go to the next record, that control should contain the value
you are looking for.


Thanks for the reply, Dale.

Yes, that works very good when data-entry is rolling along
normally. But in some fairly common cases, the wheels come
off. If someone gets interupted during data-entry and has
to come back later and pick up in the middle of a paper
form, there's no "update" event to set the DefaultValue for
the first record they need to add.

Aside from that, it is positive and simple (no domain lookup
necessary). I haven't ruled it out, but I'm still hoping
for something that covers all the bases.

Here's what I've settled on for the moment:

If Me.Parent.NewRecord Then
Me![txtCountPage].DefaultValue = 1
Me![txtCountLine].DefaultValue = 1
ElseIf Me![txtCountLine].DefaultValue < 34 Then
Dim strWhere As String
Dim strWhere2 As String
strWhere = "[CountSurvId] = " & _
Me.Parent![CountSurvId]
strWhere2 = "[CountSurvId] = " & _
Me.Parent![CountSurvId] & " _
And [CountPage] = " & _
Me![txtCountPage].DefaultValue
Me![txtCountPage].DefaultValue = _
Nz(DMax("CountPage", "tblCountDetail", _
strWhere), 1)
Me![txtCountLine].DefaultValue = _
Nz(DMax("CountLine", "tblCountDetail", _
strWhere2), 0) + 1
Me![txtCountLine].Requery
ElseIf Me![txtCountLine].DefaultValue >= 34 Then
Dim strWhere3 As String
Dim strWhere4 As String
strWhere3 = "[CountSurvId] = " & _
Me.Parent![CountSurvId]
strWhere4 = "[CountSurvId] = " & _
Me.Parent![CountSurvId] & " And _
[CountPage] = " & _
Me![txtCountPage].DefaultValue
Me![txtCountPage].DefaultValue = _
Nz(DMax("CountPage", "tblCountDetail", _
strWhere3), 1) + 1
Me![txtCountLine].DefaultValue = 1
Me![txtCountLine].Requery
End If


I also put a call for Form_Current on the AfterUpdate event
of those two fields, so that when a crossed-out line on the
paper requires skipping a number, and the data-entry clerk
has to get mousey, the DefaultValues will fix themselves.


But it has problems, I now see...
 
D

Dale Fye

Personally, I think your best bet, when they open that form is to make them
enter the Page# or take them to the last entry that was made. Then, you can
use DMAX( ) to determine the maximum line# that already exists for that
Page#, and go to the record that corresponds to the Page/Line combination.
Once there, you can set the default value, so that the next new record they
go to will have Dmax( ) +1. Then, in the BeforeUpdate event, you could test
to see whether it is a new record, and only update the default value if you
are on a new record.

HTH
Dale
--
Don''''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.



croy said:
I think your best bet is to use the forms BeforeUpdate event. Something like:

Private Sub Form_BeforeUpdate(Cancel as integer)

me.txSomeControl.DefaultValue = me.txtSomeControl.Value + 1

End Sub

Then, when you go to the next record, that control should contain the value
you are looking for.


Thanks for the reply, Dale.

Yes, that works very good when data-entry is rolling along
normally. But in some fairly common cases, the wheels come
off. If someone gets interupted during data-entry and has
to come back later and pick up in the middle of a paper
form, there's no "update" event to set the DefaultValue for
the first record they need to add.

Aside from that, it is positive and simple (no domain lookup
necessary). I haven't ruled it out, but I'm still hoping
for something that covers all the bases.

Here's what I've settled on for the moment:

If Me.Parent.NewRecord Then
Me![txtCountPage].DefaultValue = 1
Me![txtCountLine].DefaultValue = 1
ElseIf Me![txtCountLine].DefaultValue < 34 Then
Dim strWhere As String
Dim strWhere2 As String
strWhere = "[CountSurvId] = " & _
Me.Parent![CountSurvId]
strWhere2 = "[CountSurvId] = " & _
Me.Parent![CountSurvId] & " _
And [CountPage] = " & _
Me![txtCountPage].DefaultValue
Me![txtCountPage].DefaultValue = _
Nz(DMax("CountPage", "tblCountDetail", _
strWhere), 1)
Me![txtCountLine].DefaultValue = _
Nz(DMax("CountLine", "tblCountDetail", _
strWhere2), 0) + 1
Me![txtCountLine].Requery
ElseIf Me![txtCountLine].DefaultValue >= 34 Then
Dim strWhere3 As String
Dim strWhere4 As String
strWhere3 = "[CountSurvId] = " & _
Me.Parent![CountSurvId]
strWhere4 = "[CountSurvId] = " & _
Me.Parent![CountSurvId] & " And _
[CountPage] = " & _
Me![txtCountPage].DefaultValue
Me![txtCountPage].DefaultValue = _
Nz(DMax("CountPage", "tblCountDetail", _
strWhere3), 1) + 1
Me![txtCountLine].DefaultValue = 1
Me![txtCountLine].Requery
End If


I also put a call for Form_Current on the AfterUpdate event
of those two fields, so that when a crossed-out line on the
paper requires skipping a number, and the data-entry clerk
has to get mousey, the DefaultValues will fix themselves.
 

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