none
Compile Error - Method or Data member not found RRS feed

  • Question

  • Getting compile error for following line of code:'

        Set txt1 = Me.Controls.Add("VB.TextBox", "txt1")

    Using VBA 7.1

    Any idea as to why?

    Thursday, September 15, 2016 12:49 PM

Answers

  • Add is a method of the Controls collection on a userform in for example Word or Excel. It does not apply to Access.

    Access has the CreateControl method. This only works if the form is in Design view or in Layout view, not if the form is in Form view. So if you open the form in Form view, the code will cause an error.

    Example:

        Set txt1 = CreateControl(FormName:="Form1", ControlType:=acTextBox, _
            Section:=acDetail, Left:=1440, Top:=1440, Width:=1440, Height:=240)


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, September 16, 2016 11:52 AM

All replies

  • Are you trying to create a text box on a userform? If so, use Forms.TextBox.1 instead of VB.TextBox:

        Set txt1 = Me.Controls.Add("Forms.TextBox.1", "txt1", True)


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, September 15, 2016 2:33 PM
  • It appears the problem is that the .Add method is not recognized by the compiler.
    Friday, September 16, 2016 12:36 AM
  • Where do you use the code? Are there any error message?

    Please share more detail information with us, so that we could help you fix the issue.

    Besides, if you are using userform, I suggest you use the code Hans shared.

    Friday, September 16, 2016 5:40 AM
    Moderator
  • It appears the problem is that the .Add method is not recognized by the compiler.

    I asked "Are you trying to create a text box on a userform?" If not, where are you trying to create the text box?

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, September 16, 2016 9:50 AM
  • I am using MS Access. Starting with a blank form I added a Button control to which I added the following code to the Click event:

    Option Explicit
    Private WithEvents txt1 As TextBox

    Private Sub Command0_Click()
        Set txt1 = Me.Controls.Add("VB.TextBox", "txt1")
    End Sub

    Upon compiling I get the following error (.Add is highlighted):

    Method or Data Member not found.

    Friday, September 16, 2016 11:39 AM
  • Add is a method of the Controls collection on a userform in for example Word or Excel. It does not apply to Access.

    Access has the CreateControl method. This only works if the form is in Design view or in Layout view, not if the form is in Form view. So if you open the form in Form view, the code will cause an error.

    Example:

        Set txt1 = CreateControl(FormName:="Form1", ControlType:=acTextBox, _
            Section:=acDetail, Left:=1440, Top:=1440, Width:=1440, Height:=240)


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, September 16, 2016 11:52 AM
  • Got it! Thank you.
    Friday, September 16, 2016 12:14 PM