Checkboxes on a subform not giving consistent results

T

Todd

I'm baffled. I have frmMain and frmSub. On frmMain, I have chkMain1,
chkMain2, chkMain3. On frmSub I have chkSub1, chkSub2

When I click a button on the main form, it runs code like this...

If Me!chkMain1 = True Then...
If Me!chkMain2 = True Then...
If Me!chkMain3 = True Then...
If Me!frmSub!chkSub1 = True Then...
If Me!frmSub!chkSub2 = True Then...

Actually, I loop the code above but I thought it would be easier to
understand if I listed it out. Anyways, all of the chkMain tests work
perfectly every time. The checkboxes on the subform, however, are acting
very erratic. Sometimes chkSub1 will test true even if it is false as will
chkSub2. I can't figure out why. I tried adding Forms!frmMain.Refresh but
that didn't help.

---
I'm not sure that you need this info, but I'll provide it just in case. The
above mentioned code is actually done via a loop. My checkbox field names
are stored in a table (field name is txtDbaseField). The ones in the main
subform are stored as chkMain1 and the ones in the subform are stored as
frmSub!chkSub1 and brought up with this code:
Dim rstFiles As Recordset
Set rstFiles = CurrentDb.OpenRecordset("TLA-Files")
Do While Not rstFiles.EOF
If Me(rstFiles!txtDbaseField) = True Then
...Do some stuff...
End If
rstFiles.MoveNext
Loop
rstFiles.Close
 
D

Dirk Goldgar

Todd said:
I'm baffled. I have frmMain and frmSub. On frmMain, I have chkMain1,
chkMain2, chkMain3. On frmSub I have chkSub1, chkSub2

When I click a button on the main form, it runs code like this...

If Me!chkMain1 = True Then...
If Me!chkMain2 = True Then...
If Me!chkMain3 = True Then...
If Me!frmSub!chkSub1 = True Then...
If Me!frmSub!chkSub2 = True Then...

Actually, I loop the code above but I thought it would be easier to
understand if I listed it out. Anyways, all of the chkMain tests work
perfectly every time. The checkboxes on the subform, however, are acting
very erratic. Sometimes chkSub1 will test true even if it is false as
will chkSub2. I can't figure out why. I tried adding
Forms!frmMain.Refresh but that didn't help.

---
I'm not sure that you need this info, but I'll provide it just in case.
The above mentioned code is actually done via a loop. My checkbox field
names are stored in a table (field name is txtDbaseField). The ones in
the main subform are stored as chkMain1 and the ones in the subform are
stored as frmSub!chkSub1 and brought up with this code:
Dim rstFiles As Recordset
Set rstFiles = CurrentDb.OpenRecordset("TLA-Files")
Do While Not rstFiles.EOF
If Me(rstFiles!txtDbaseField) = True Then
...Do some stuff...
End If
rstFiles.MoveNext
Loop
rstFiles.Close


You mean that, for fields on the subform, rstFiles!txtDbaseField evaluates
to "frmSub!chkSub1", or the equivalent for the other check boxes on the
subform? I wouldn't expect that *ever* to work, and in Access 2003, at
least, it raises an error when I try it. Do you have some sort of
error-handling in place, or On Error Resume Next, that would suppress the
display of an error message?

If you're going to do something like this, I'd suggest that you parse out
the subform name and control name and handle them specially, like this (air
code):

Dim strControl As String
Dim strSubControl As String
Dim I As Long
Dim blnChecked As Boolean

Do While Not rstFiles.EOF

strControl = rstFiles!txtDbaseField

I = InStr(strControl, "!")
If I > 0 Then
strSubControl = Mid$(strControl, I + 1)
strControl = Left$(strControl, I - 1)
blnChecked = _
Me.Controls(strControl).Form.Controls(strSubControl).Value
Else
blnChecked = Me.Controls(strControl).Value
End If

If blnChecked Then
...Do some stuff...
End If

rstFiles.MoveNext

Loop
 
P

Paul Shapiro

Some Access versions had a problem when you referred to a checkbox control
without explicitly using the .Value property.
Also, when you refer to a control on a subform, I believe you have to
specify the .Form property of the subform control.
Try using the . notation instead of !, and make sure it compiles and runs.
With the dot you should get compile errors instead of runtime errors, which
makes it easier to debug. Then you can change it however you want.

If Me.frmSub.Form.chkSub1.Value = True Then...
 
D

Dirk Goldgar

Paul Shapiro said:
Some Access versions had a problem when you referred to a checkbox control
without explicitly using the .Value property.

It was this syntax that had the problem:

If Me!chkMyCheckbox Then

The following versions of doing the same thing were all okay:

If Me!chkMyCheckbox.Value Then

If (Me!chkMyCheckbox) Then

If Me!chkMyCheckbox = True Then

If Me!chkMyCheckbox.Value = True Then

I think that bug has long since been fixed, but I still avoid the syntax
that was subject to it.
Also, when you refer to a control on a subform, I believe you have to
specify the .Form property of the subform control.

I gather that this began to be an issue in Access 2007, under certain
circumstances. I'm pretty sure, though, that it is not an issue in VBA
code.
 
T

Todd

You were spot on with your assessment. I forgot that there was an error
reporting override in the code (which is required for part of the code for
various reasons). The syntax you provided cleared up my problems. Thank
you!!!!!
 

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