Assigning values to check-boxes

D

Dirk Goldgar

Bill Stanton said:
Dirk wrote:

"I'd certainly expect to be able to change the value of a field by
assigning to the control bound to it, so long as the recordset and
field are updatable and the control is enabled."

So would I, and that's exactly what is making this caper so
mysterious! Other than my having erroneously inserted the assignment
statements
in the OnOpen code rather than in the OnLoad section, this should be
about as "vanilla as it comes". Having said that, I don't understand
what PC Datasheet is saying about one not being able to assign values
to bound controls from code. To me, that's the same thing as saying
that code
can't act as proxy for the user in assigning values to a field.
However, I'm new enough at all of this to not challenge the experts,
so I remain clueless.

So, to clarify, I open a form in "acFormAdd" mode and attempt to
toggle
two of the check-box controls from the OnLoad section of the form's
code-sheet. With the repeated failures there, I modified the code to
attempt to assign text to one of the many text-boxes and encountered
the same error. With that, I've concluded the problem has nothing to
do with the check-boxes themselves, rather there is a problem in
general while trying to perform assignments to controls from code.

Be back in a few hours. (10:48AM PST)
Bill

Just out of curiosity, what happens if you put the code in the Current
event?
 
D

Dirk Goldgar

Bill Stanton said:
Well, Dirk, the code works just like I expected it to when
it was placed in the OnLoad code... the two assignments
were made without error and the check-boxes were toggled
from code!!!!!!!!!

Now, can you explain why they "blow up" when placed in
the OnLoad and not in the OnCurrent?

Given the information available, I can only guess that it's a timing
issue of some sort: in your form's Load event, at least when you're
specifying Data Entry mode at open time, no record is yet current at the
time your assignments are made, and so you can't assign to the boud
controls. In the Current event, you *know* that a record is current,
even if it is the "new" record.

However, a simple test form I made doesn't have any problem assigning to
bound controls in the Load event, so I wouldn't expect this usually to
be a problem. Maybe your form is so complex as to put enough time
between the Load and Current events to affect the assignment statements,
or maybe your data source is so slow to respond as to cause the problem,
or maybe there's something else we don't know about. This is all
conjecture, of course.

Is your form's recordsource a local table, or is it on a network server
somewhere?
 
J

John Spencer (MVP)

SHEER SPECULATION:

Bill, I see in another subthread that you got this working to some extent by
moving the checkbox settings to the On current event, so no real urgency.

I do notice that you run another bit of code at the end of your Form_Load event
- SetImagePath. Obviously, I don't know what happens in that code, but could
that code be doing something to the two checkboxes that makes them updatable
where before it runs they are not updatable?

Bill said:
Below is the OnOpen, OnLoad and, just added, the OnClick
for a command button that assigns values to the check-box
controls as you suggested. The OnClick code executes without
error, so there's something strange going on at the time the OnLoad
executes.

Bill

Private Sub Command142_Click()
Me.FamilyDonOnly = True
Me.FamilyNewsLetter = False
End Sub

Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize 'Open window maximized
End Sub

Private Sub Form_Load()
DoCmd.MoveSize 1, 1
Me.AddFamilyMembers.Visible = True

If IsNull(Me.OpenArgs) Then 'Not being invoked subordinate
to table form.
Me.NavigationButtons = False
Else 'Being invoked with parameters.
If Me.OpenArgs = "NewEntry" Then Exit Sub

If Me.OpenArgs = "RegisterDonor" Then
Me.NavigationButtons = False
Me.AddFamilyMembers.Visible = False
'Me.FamilyDonOnly = True
'Me.FamilyNewsLetter = False
Exit Sub
End If

'Move to the record specified in open parameter
Me.RecordsetClone.FindFirst "[FamilyID] = " & Me.OpenArgs
Me.Bookmark = Me.RecordsetClone.Bookmark
FamilyName.SetFocus
Me.AllowAdditions = False
Me.AddNewMember.Visible = True

End If

SetImagePath
End Sub

Albert D. Kallal said:
Ok, that does surprise me, but ok. As long as right after the forms loads,
the first thing you try is clicking on a that check box, and if it works ok,
then it most surprising why trying to modify the box in code would fail. I
suggest you try placing a simple button on the form, and the code behind the
button can go:

me.FieldCheckBox = True

If the above code works when you press the button, then something along the
way in the event code is being missed here. Perhaps you have (or have been)
doing something else after the form loads, and NOT telling me about it. If
the above button code works, then the field is updateable after the form
loads.

No doubt if the query is a multi-table query (and that is a BIG if), then
you generally must write out the parent table FIRST, since then the key id
is created and written to disk. Then, and only then can child records be
added. It is possible that you have some other code that when the forms
finally loads you run. Perhaps you have been by habit editing other things
on the form first BEFORE you click on the check box, and that is causing the
key id to be created . However, based on my above questions, I have been
clearly stating to try the check box before you do anything else on the
form. It has been clearly stated that first thing you try is the check box
when the form loads.

Try placing two buttons on the form right beside that check box. One button
can set the check box to true, and the other button to false. When you try
the button code, does the check box toggle ok? Now trying actually clicking
on the check box, does is again toggle ok?

However, if the query is in fact a multi-table join, then you DO have to
force the parent record to disk before adding (or in your case editing) the
child record fields.


--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
B

Bill Stanton

John,
SetImagePath attends to setting the link path
to a jpg image that is to be displayed in via
an image control and doesn't touch any of the
other controls.

I tried moving the two lines of code back in
the initialization sequence to OnActivate, but
I get the same error there as I did in the
OnLoad event.

Since my OnCurrent code attends to other
tasks, I'm not happy leaving the check-box
initialization code there, as the user might very
well have changed them to their liking. That
being the case, this caper isn't over yet.

Bill
John Spencer (MVP) said:
SHEER SPECULATION:

Bill, I see in another subthread that you got this working to some extent by
moving the checkbox settings to the On current event, so no real urgency.

I do notice that you run another bit of code at the end of your Form_Load event
- SetImagePath. Obviously, I don't know what happens in that code, but could
that code be doing something to the two checkboxes that makes them updatable
where before it runs they are not updatable?

Bill said:
Below is the OnOpen, OnLoad and, just added, the OnClick
for a command button that assigns values to the check-box
controls as you suggested. The OnClick code executes without
error, so there's something strange going on at the time the OnLoad
executes.

Bill

Private Sub Command142_Click()
Me.FamilyDonOnly = True
Me.FamilyNewsLetter = False
End Sub

Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize 'Open window maximized
End Sub

Private Sub Form_Load()
DoCmd.MoveSize 1, 1
Me.AddFamilyMembers.Visible = True

If IsNull(Me.OpenArgs) Then 'Not being invoked subordinate
to table form.
Me.NavigationButtons = False
Else 'Being invoked with parameters.
If Me.OpenArgs = "NewEntry" Then Exit Sub

If Me.OpenArgs = "RegisterDonor" Then
Me.NavigationButtons = False
Me.AddFamilyMembers.Visible = False
'Me.FamilyDonOnly = True
'Me.FamilyNewsLetter = False
Exit Sub
End If

'Move to the record specified in open parameter
Me.RecordsetClone.FindFirst "[FamilyID] = " & Me.OpenArgs
Me.Bookmark = Me.RecordsetClone.Bookmark
FamilyName.SetFocus
Me.AllowAdditions = False
Me.AddNewMember.Visible = True

End If

SetImagePath
End Sub

Albert D. Kallal said:
Albert, sorry I didn't reply completely. With the two assignment
statements commented out, the form works perfectly. That is,
all check boxes respond normally, data can be entered and
records created accordingly.
Bill

Ok, that does surprise me, but ok. As long as right after the forms loads,
the first thing you try is clicking on a that check box, and if it
works
ok,
then it most surprising why trying to modify the box in code would fail. I
suggest you try placing a simple button on the form, and the code
behind
the
button can go:

me.FieldCheckBox = True

If the above code works when you press the button, then something
along
the
way in the event code is being missed here. Perhaps you have (or have been)
doing something else after the form loads, and NOT telling me about it. If
the above button code works, then the field is updateable after the form
loads.

No doubt if the query is a multi-table query (and that is a BIG if), then
you generally must write out the parent table FIRST, since then the key id
is created and written to disk. Then, and only then can child records be
added. It is possible that you have some other code that when the forms
finally loads you run. Perhaps you have been by habit editing other things
on the form first BEFORE you click on the check box, and that is
causing
the
key id to be created . However, based on my above questions, I have been
clearly stating to try the check box before you do anything else on the
form. It has been clearly stated that first thing you try is the check box
when the form loads.

Try placing two buttons on the form right beside that check box. One button
can set the check box to true, and the other button to false. When you try
the button code, does the check box toggle ok? Now trying actually clicking
on the check box, does is again toggle ok?

However, if the query is in fact a multi-table join, then you DO have to
force the parent record to disk before adding (or in your case
editing)
the
child record fields.


--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
B

Bill Stanton

Timing could well be the issue. There's a photo insertion via
a link that doesn't get handled until the call to GetImagePath
is executed, you might have noticed that earlier in the thread
where I included all the open and load code. I'll try moving
that task back into the Open code where it was origianally
and see if the assignment statements will execute okay in
the OnLoad.

Bill
 
B

Bill Stanton

Nope! The only place I can run without error is for the
assignment statements to be in the OnCurrent code.
I tried deferring only those two assignments to the
OnLoad with the other tasks moved back into the
OnOpen, but the error persists.

To answer you question about the server. I have a simple
front-end/back-end arrangement on a single machine.

Unless someone chimes in with a idea, I'll build a "once function"
into the OnCurrent code and do the initialization of the two
check-boxes during the form's open sequence.

Thanks to everyone for their thoughts and efforts... the
problem remains a mystery.

Bill
 
D

Dirk Goldgar

Bill Stanton said:
Nope! The only place I can run without error is for the
assignment statements to be in the OnCurrent code.
I tried deferring only those two assignments to the
OnLoad with the other tasks moved back into the
OnOpen, but the error persists.

What happens if you comment out the loading of the image entirely? Do
the checkbox assignments work in the Load event then?
To answer you question about the server. I have a simple
front-end/back-end arrangement on a single machine.

I tested only on an unsplit database, so I suppose that could also be a
factor.
Unless someone chimes in with a idea, I'll build a "once function"
into the OnCurrent code and do the initialization of the two
check-boxes during the form's open sequence.

In the case where you're doing this, I thought you were only opening the
form to add a single record anyway. Did I misunderstand?
 
B

Bill Stanton

Dirk wrote:
"In the case where you're doing this, I thought you were only opening the
form to add a single record anyway. Did I misunderstand?"

No, you understood correctly. In the instance where I get the failure,
I've opened the form to add a single record.

And no, the error persists even when I comment out the call
to SetImagePath. The image control defaults to a file:
path.Default.jpg.

The form is, though, a multipurpose form that includes a sub-form
made visible under circumstances other than the one where I get
the failures. The sub-form has an entirely different RecordSource
and the filter for that form isn't established until several user actions
that lead to the activation of the sub-form. If I understand things
correctly, the sub-form would load as a part of the main form
loading, but would neither have the focus or any current records
at the time my failure occured.

Bill
 
A

Albert D. Kallal

Well, I think we kind of stuck this out quite good! eh?

I going to suggest that you do try and temporality turn off the on-current
event.

You can easily disable the on current event by declaring a variable at the
forms module

Option Explicit

dim bolDoNotRun as boolean

Then, in the forms on load event, the first line of code can be


bolDoNotRun = True

..... the rest of your on-load code
and, then the last line of on-load

bolDoNotRun = False


And, then in the first line of your on-current, you can go:

if bolDoNotRun = True then
exit sub
endif

And, if need be, put the

I will have to say I have come across a few errors when using the image
control
load. In fact, if you place a button on your form that does a

DoCmd.GoToRecord , , acNext

If you start whacking that above test button too fast, and get ahead of the
imaging load, then ms-access will suffer a execution error. (if you use
other then a button control, and use the click event of that control, then
the execute error does NOT occur).

So, I think this is timing issue, and the image guy is the bad egg.

You can give the above Boolean "flag" thing a try. If the above idea works,
then
I think that would be about the cleanest solution.

Good Luck Bill..and is a nasty little problem.

I suppose when you push the envelope, you will run into problems like this!
 

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