Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    cowboy is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    291

    Run-time error '424': Object required

    I am trying to make a function that when I pass the control it will resize the control according to the length of the data in the given text box. I am trying to do this in a report only. Anyone know why I am getting this error?
    Run-time error '424':
    Object required

    Resize (Me.txtLocationTest)




    Function Resize(ctl As Control)
    Dim tlength As Long
    test = Me.TextWidth(ctl)
    Me.ctl.Width = test
    End Function

  2. #2
    evander is offline Competent Performer
    Windows 7 Access 2003
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    206
    I think you should declare a generic textbox object first like (Dim Something as textbox?), and reference it to the actual textbox (set something = me.yourtextbox).

    Haven't tried this myself, but I hope it works for you.

    Good luck.

  3. #3
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Since you're passing the Report field to a function, you shouldn't have to add the "Me!" at the beginning.

    Try changing your code to the following and see if it works (changes in bold)
    Code:
    Resize (Me.txtLocationTest)
    
    Function Resize(ctl As Control)
      Dim test As Long
      test = Me.TextWidth(ctl)
      ctl.Width = test
    End Function

  4. #4
    cowboy is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    291
    I forgot to change all my variables when I changed names which you have reminded me about, and I made those changes. The same error is occuring and it highlights the line Resize (Me.txtLocationTest) as the one with the error.

    so I call the function with
    Resize (Me.txtLocationTest)

    and the function code is
    Function Resize(ctl As Control)
    Dim tlength As Long
    tlength = Me.TextWidth(ctl)
    ctl.Width = tlength
    End Function

  5. #5
    cowboy is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    291
    I have now also tried

    Code:
    Dim gtextbox As TextBox
        Set gtextbox = Me.txtLocationTest
        Resize (gtextbox)
    Code:
    Function Resize(ctl As Control)
        Dim tlength As Long
        tlength = Me.TextWidth(ctl)
        ctl.Width = tlength
    End Function
    I end up with the same error and Resize (gtextbox) is highlighted. Is the functions variable (ctl As Control) expecting it to pass something other than a textbox?

  6. #6
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    I did a little looking around and "Resize" may be a reserved word (there's a Resize Event). Try changing the name of the function and see if that helps.

  7. #7
    cowboy is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    291
    I tried

    Code:
    Dim gtextbox As TextBox
    Set gtextbox = Me.txtLocationTest
    fSizeChange (gtextbox)

    Code:
    Function fSizeChange(ctl As Control)
        Dim tlength As Long
        tlength = Me.TextWidth(ctl)
        ctl.Width = tlength
    End Function
    and it gave me the same error with the same line highlighted. Thanks for the idea though, that was a good thought.

  8. #8
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Go through the link I think it will help.
    http://www.techonthenet.com/access/q...j_required.php

  9. #9
    cowboy is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    291
    Didnt work :/

  10. #10
    evander is offline Competent Performer
    Windows 7 Access 2003
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    206
    Why should it be a function? Can you try making it a sub instead? Functions are supposed to return values (though I know they can be used as sub procedures too).

  11. #11
    evander is offline Competent Performer
    Windows 7 Access 2003
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    206
    Code:
    Function fSizeChange(ctl As Control) ??
        Dim tlength As Long
        tlength = Me.TextWidth(ctl)
        ctl.Width = tlength
        ???
    End Function
    ?? - There is no data type assigned to the fSizeChange function.
    ??? - The function does not return anything.

  12. #12
    evander is offline Competent Performer
    Windows 7 Access 2003
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    206
    Regarding maximus's suggestion, did you check if the form that contains your textbox is visible in the project explorer of the VBA window?

  13. #13
    evander is offline Competent Performer
    Windows 7 Access 2003
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    206
    Also, where did you place the Resize(Textbox) function? If you placed it in the Form_Load() event, chances are the controls (including your textbox) are not visible yet. And your function requires a "visible" textbox object by the time you summon your function.

    When using form events, try Form_Activate() instead.

    Hope this works. Good luck.

  14. #14
    evander is offline Competent Performer
    Windows 7 Access 2003
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    206
    Also, try experimenting with this code that changes a textbox control.

    Code:
    Private Sub Command1_Click()
       Me.Textbox1.SetFocus
       Me.Textbox1.Width = Len(Me.Textbox1.Text)
    End Sub
    Hope it works. Good luck.

  15. #15
    cowboy is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    291
    As you can see from the first picture the text box is associated with the report and the VBA editor. The second picture will show an example of the code using a sub but it is still giving me that same error. Here is an example of code that is working:
    Code:
    Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
        ShowPage
        Dim testlen As Long
        If IsNull(Me.txtLocationTest) = False Then
            testlen = Me.TextWidth(Me.txtLocationTest)
            Me.txtLocationTest.Width = testlen
        End If
    End Sub
    In an effort to make my code shorter I have not gone and done applied this too every text box I want to resize itself. I didnt think making a sub or function for this was going to be this trick. If no one sees my mistake soon I will just go through and do this for each and every textbox.
    Thanks for all your input, I hate when I get caught up on a syntax error of some sort.

    *Pictures didnt upload, they are on next post sorry

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 04-15-2010, 09:43 AM
  2. Replies: 3
    Last Post: 03-30-2010, 01:21 PM
  3. Object required
    By duckie10 in forum Access
    Replies: 1
    Last Post: 05-15-2009, 02:11 PM
  4. Replies: 2
    Last Post: 03-23-2009, 05:39 AM
  5. Replies: 2
    Last Post: 02-28-2009, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums