Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

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

Discussion in 'Information Technology' started by Andrew R, Jul 28, 2006.

  1. Andrew R

    Andrew R
    Expand Collapse
    Guest

    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
     
  2. Loading...


  3. Douglas J Steele

    Douglas J Steele
    Expand Collapse
    Guest

    See whether

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

    works any better.

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (no e-mails, please!)


    "Andrew R" <andrew.richards@cashette.com> wrote in message
    news:1147962982.840567.3000@j55g2000cwa.googlegroups.com...
    > 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
    >
     
  4. Andrew R

    Andrew R
    Expand Collapse
    Guest

    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
     
  5. Andrew R

    Andrew R
    Expand Collapse
    Guest

    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
     

Share This Page