Clearing fields

B

BruceM

Sounds like it could be a farming question from the subject line, but it is
about Access forms. After updating a bound text box (txtComments), two
other fields are populated with CurrentUser and today's date if they were
null to start. I use the text box After Update event to do this:

If IsNull (Me.txtUser) Then
Me.txtUser = CurrentUser()
End If

If IsNull(Me.txtResponseDate) Then
Me.txtResponseDate = Date
End If

This works fine, but the reverse does not. If I clear txtComments, I would
like to also clear txtUser and txtResponseDate. In the After Update event:

If IsNull(Me.txtComments) Then
' Me.txtUser becomes Null
' Me.txtResponseDate becomes Null
End If

I have discovered that I need to refresh the record before txtComments will
register as Null, but I can't find a way to take care of the other two
fields.

By the way, I'm not always certain if I should be referring the the field or
to the control bound to the field.
 
T

tina

try

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
End If

normally, if the field is bound to a control on the form, i refer to the
control name, and only use the fieldname if i encounter a problem with the
running code.

hth
 
B

Barry Gilbert

You should be able to use:
If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
Endif

without needing to refresh anything. Does this not work?

Barry
 
B

BruceM

Thanks for replying, and thanks also to Barry, who responded elsewhere in
this thread. You both suggested the same thing. What you suggested is what
I tried at first, but with no success.
This plus some error handling was the whole After Update event for
txtComments:

If IsNull (Me.txtUser) Then
Me.txtUser = CurrentUser()
End If

If IsNull(Me.txtResponseDate) Then
Me.txtResponseDate = Date
End If

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
End If

As I said, the first two sections (populating txtUser and txtResponseDate)
work fine, but the last part does not unless I either explicitly save the
record first, or add Me.Refresh before that code runs. I don't get it, but
there it is. What bugs me is that I don't understand what's going on here,
even though I have gotten it to work.
 
T

tina

try re-thinking the logic a little bit. if the Comments control is Null, you
don't want the other two controls populated, so there's no point in doing
that *before* you check the value of the Comments control. see if the
following will work for you, as

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
Else
If IsNull (Me.txtUser) Then
Me.txtUser = CurrentUser()
End If

If IsNull(Me.txtResponseDate) Then
Me.txtResponseDate = Date
End If
End If

hth
 
B

BruceM

Thans again for your reply. I see your point about the logic. I tried what
you suggested, and it works fine, but only if it starts with Me.Refresh.
Without Me.Refresh I can set the value of txtUser and txtResponseDate to
CurrentUser() and today's date respectively, but I cannot get those same
fields to reset to null. I tried placing this code into a command button's
Click event as a test:

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
End If

If I delete the contents of txtComments and click the button, nothing
happens. If I navigate away from the record and then back again (i.e. save
the record), the command button works as intended. If I add a message box:
msgbox "Control is null" after If IsNull(Me.txtComments), I only see the
message after saving the record. Before that the control does not register
as null, and I do not see the message.

My actual field is Response, not Comments; the control is txtResponse, not
txtComments. [User] is actually [Responder]; the control is txtResponder.
[ResponseDate] and txtResponseDate are the actual names. I don't see any
reserved words there or anything like that.

I'm stumped, but I guess I won't worry about it too much. I keep thinking
there is an important concept lurking here somewhere, but darned if I know
what.
 
T

tina

well, since you're having trouble when referring to the control names in the
code, try referring to the field names instead - maybe that will make the
difference.

hth


BruceM said:
Thans again for your reply. I see your point about the logic. I tried what
you suggested, and it works fine, but only if it starts with Me.Refresh.
Without Me.Refresh I can set the value of txtUser and txtResponseDate to
CurrentUser() and today's date respectively, but I cannot get those same
fields to reset to null. I tried placing this code into a command button's
Click event as a test:

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
End If

If I delete the contents of txtComments and click the button, nothing
happens. If I navigate away from the record and then back again (i.e. save
the record), the command button works as intended. If I add a message box:
msgbox "Control is null" after If IsNull(Me.txtComments), I only see the
message after saving the record. Before that the control does not register
as null, and I do not see the message.

My actual field is Response, not Comments; the control is txtResponse, not
txtComments. [User] is actually [Responder]; the control is txtResponder.
[ResponseDate] and txtResponseDate are the actual names. I don't see any
reserved words there or anything like that.

I'm stumped, but I guess I won't worry about it too much. I keep thinking
there is an important concept lurking here somewhere, but darned if I know
what.

tina said:
try re-thinking the logic a little bit. if the Comments control is Null,
you
don't want the other two controls populated, so there's no point in doing
that *before* you check the value of the Comments control. see if the
following will work for you, as

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
Else
If IsNull (Me.txtUser) Then
Me.txtUser = CurrentUser()
End If

If IsNull(Me.txtResponseDate) Then
Me.txtResponseDate = Date
End If
End If

hth


with
the but
it
 
B

BruceM

Thanks for your further thoughts and comments on this question. Neither
Me.txtComments (the text box) nor Me.Comments (the field) evaluates to Null
unless I refresh at the beginning of the After Update event. However,
starting with If Me.txtComments = "" produces the desired results. I guess
deleting all of the text in a field produces an empty string until either
the form is refreshed or the record is saved. At least I'm adding to my
understanding of the differences between Null and an empty string.

tina said:
well, since you're having trouble when referring to the control names in
the
code, try referring to the field names instead - maybe that will make the
difference.

hth


BruceM said:
Thans again for your reply. I see your point about the logic. I tried what
you suggested, and it works fine, but only if it starts with Me.Refresh.
Without Me.Refresh I can set the value of txtUser and txtResponseDate to
CurrentUser() and today's date respectively, but I cannot get those same
fields to reset to null. I tried placing this code into a command button's
Click event as a test:

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
End If

If I delete the contents of txtComments and click the button, nothing
happens. If I navigate away from the record and then back again (i.e. save
the record), the command button works as intended. If I add a message box:
msgbox "Control is null" after If IsNull(Me.txtComments), I only see the
message after saving the record. Before that the control does not register
as null, and I do not see the message.

My actual field is Response, not Comments; the control is txtResponse,
not
txtComments. [User] is actually [Responder]; the control is
txtResponder.
[ResponseDate] and txtResponseDate are the actual names. I don't see any
reserved words there or anything like that.

I'm stumped, but I guess I won't worry about it too much. I keep
thinking
there is an important concept lurking here somewhere, but darned if I
know
what.

tina said:
try re-thinking the logic a little bit. if the Comments control is
Null,
you
don't want the other two controls populated, so there's no point in doing
that *before* you check the value of the Comments control. see if the
following will work for you, as

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
Else
If IsNull (Me.txtUser) Then
Me.txtUser = CurrentUser()
End If

If IsNull(Me.txtResponseDate) Then
Me.txtResponseDate = Date
End If
End If

hth


Thanks for replying, and thanks also to Barry, who responded elsewhere in
this thread. You both suggested the same thing. What you suggested
is
what
I tried at first, but with no success.
This plus some error handling was the whole After Update event for
txtComments:

If IsNull (Me.txtUser) Then
Me.txtUser = CurrentUser()
End If

If IsNull(Me.txtResponseDate) Then
Me.txtResponseDate = Date
End If

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
End If

As I said, the first two sections (populating txtUser and
txtResponseDate)
work fine, but the last part does not unless I either explicitly save the
record first, or add Me.Refresh before that code runs. I don't get
it,
but
there it is. What bugs me is that I don't understand what's going on
here,
even though I have gotten it to work.

try

If IsNull(Me.txtComments) Then
Me.txtUser = Null
Me.txtResponseDate = Null
End If

normally, if the field is bound to a control on the form, i refer to
the
control name, and only use the fieldname if i encounter a problem with
the
running code.

hth


Sounds like it could be a farming question from the subject line, but
it
is
about Access forms. After updating a bound text box (txtComments),
two
other fields are populated with CurrentUser and today's date if
they
were
null to start. I use the text box After Update event to do this:

If IsNull (Me.txtUser) Then
Me.txtUser = CurrentUser()
End If

If IsNull(Me.txtResponseDate) Then
Me.txtResponseDate = Date
End If

This works fine, but the reverse does not. If I clear txtComments, I
would
like to also clear txtUser and txtResponseDate. In the After
Update
event:

If IsNull(Me.txtComments) Then
' Me.txtUser becomes Null
' Me.txtResponseDate becomes Null
End If

I have discovered that I need to refresh the record before txtComments
will
register as Null, but I can't find a way to take care of the other two
fields.

By the way, I'm not always certain if I should be referring the the
field
or
to the control bound to the field.
 

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