none
Can not set Checkbox.Name property with variable in VBA RRS feed

  • Question

  • Hi,

    I'm trying to write a sub that autogenerates checkboxes based on data in a Access database. I would like the Name property of a checkbox to correspond with the ID for the particular entry in the database, but I get "Run time error 40044 Application-defined or object-defined error" when I try to set it by using a String variable that holds the name.

     

     

    Sub AddCheckBoxes()
     Dim ish As InlineShape
     Dim chk As MSForms.checkBox
     
     Dim dbPath As String
     Dim connection As New ADODB.connection
     dbPath = Path & "\data\myDatabase.mdb"
     connection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath
     Dim rs As New ADODB.Recordset
     
     Dim query As String
     query = "SELECT Nummer, Navn FROM Tiltak"
     rs.Open query, connection, adOpenDynamic, adLockOptimistic
     
     Do While Not rs.EOF
      Dim Id, caption As String
    
      caption = rs.Fields.Item("Name")
      Id = rs.Fields.Item("ID").Value
    
       
      'Create the checkbox
      Set ish = ActiveDocument.InlineShapes.AddOLEControl(ClassType:="Forms.CheckBox.1")
      
      
      With ish
       .Width = 150
       .Height = 29.25
      End With
      
      
      'Get the checkbox object
      Set chk = ish.OLEFormat.Object
    
      With chk
        .Name = Id
        .Caption = caption
        .Font.Name = "Arial"
      End With
      
      rs.MoveNext
      
     Loop
     
     rs.Close
     connection.Close
     
    
    End Sub
    

     

     

     

    I get the error when I set the Name property as shown in the code above. It works however if I do it like this:

     

    With chk
      .Name = "StaticName"
      .Caption = caption
      .Font.Name = "Arial"
    End With
    

     

    I have tried casting Id using chk.Name = Cstr(Id) and making new strings like this chk.Name = "Id" & Id.

    Does anybody know anything about this? It would be much appreciated :)

    PS! When the code has executed Set ish = ActiveDocument.InlineShapes.AddOLEControl(ClassType:="Forms.CheckBox.1") I can no longer enter break-mode in debugging. Does anybody know why?

     

    -L

    • Edited by Lundsern Thursday, March 17, 2011 1:43 PM Readability
    Thursday, March 17, 2011 1:42 PM

All replies

  • Hi Lundsern,

    Thanks for posting in the MSDN Forum.

    Would you please clarify some questions?

    1.                  Where this snippet is? Is it in a VBA project?

    2.                  What Version your Access is?

    3.                  What Version your Word is?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 21, 2011 5:58 AM
    Moderator
  • Hi Tom,

    Thanks for responding :)

    1. The snippet is taken from a sub written in a Word document, so I guess that would make it a VBA project? I'm quite new to VBA so I don't know when you call it a project
    2. I'm running Access 2007, but the database is saved in a Access 2002-2003 fileformat
    3. I'm running Word 2007

    Please ask if there is relevant info I have omitted.

     

    -L

    Monday, March 21, 2011 10:49 AM