Error 2448 ("You can't assign a value to this object") cause?

A

Andrew R

Hi

I have the following sub in an Access database, the idea of which is to
clear all controls on a passed form. But when I run it, I get the error
message "You can't assign a value to this object" at the very first
text box it hits! I assign a value to the text box (from a recordset)
at the load event without problem, so I'm a bit stumped...

Any thoughts?

Andrew



Public Sub ClearForm(frmClearedForm As Form)
Dim ctl As Control
'Cycle through the controls
For Each ctl In frmClearedForm

'Test if they are textboxes, listboxes etc
If TypeOf ctl Is TextBox Then ctl = "a"
If TypeOf ctl Is ComboBox Or TypeOf ctl Is ListBox Then
ctl.ListIndex = 0
If TypeOf ctl Is CheckBox Then ctl.Value = False
If TypeOf ctl Is OptionButton Then ctl.Value = False
Next ctl
End Sub
 
D

Douglas J Steele

See whether

If TypeOf ctl Is TextBox Then ctl.Text = "a"

works any better.
 
A

Andrew R

Thanks for the suggestion, but not really working. The reason I didn't
use the .text property is that if you do, you get the error message
"Error 2185: You can't reference a property or method for a control
unless the control has focus".

I could use the .setfocus method for each control, but that seems very
clunky, and I don't see why the original solution fails.

Incidentally, I should perhaps explain that setting the value to "a"
was not the original idea - it was originally ctl="" to clear it out! I
changed it to ="a" to see if a more visible result would help matters!!

Thanks
Andrew
 
A

Andrew R

Hello again!

I managed to sort it out, so just wanted to mention the solution in
case it helps anyone else out.

The problem was that a couple of the text boxes on the form were not
unbound - their control sources were set to calculations of length of
service as a customer etc. This meant that I was not able to change
their value, resulting in the error described above.

The solution (or at least my solution) was as below

Thanks again for your help

Andrew

Public Sub ClearForm(frmClearedForm As Form)
Dim ctl As Control

'Cycle through the controls
For Each ctl In frmClearedForm
'Test if they are textboxes, listboxes etc
If TypeOf ctl Is TextBox Then
'Check if it's unbound
If ctl.ControlSource = "" Then
ctl = ""
End If
End If
If TypeOf ctl Is ComboBox Or TypeOf ctl Is ListBox Then
ctl.ListIndex = 0
If TypeOf ctl Is CheckBox Then ctl.Value = False
If TypeOf ctl Is OptionButton Then ctl.Value = False
Next ctl
End Sub
 

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