We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

textwidth method -- syntax question

drozdzowski
drozdzowski asked
on
Medium Priority
762 Views
Last Modified: 2008-02-01
Hi,

I want to autosize a textbox.  I put the following code in my reports detail "on format" event.  When I run the report, I get run-time error 438: object doesn't support this property or method

--------------------------start of code---------------------------
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim ctl As Control

For Each ctl In Me.Section(acDetail).Controls
    ' Get width of control to enable autosizing
    ctl.Width = Me.TextWidth(ctl)
Next

Set ctl = Nothing
End Sub
--------------------------end of code------------------------------


What am I doing wrong?

Thanks!

Mirek
Comment
Watch Question

Access?

Try something like this:
ctl.width = me(Ctl).width

Walt
Come to think of it you don't really need to do that - it is self-referencing.  Why not:
somevariable = me(ctl).width

Walt

Author

Commented:
yeah, it's access.  i have to use the textwidth method.  width gives me only the width of the control, not it's contents
Sorry.
Brain cloud.
  How about this:
ctl.width = TextWidth(Me(ctl))
Walt

Author

Commented:
no, something my come before textwidth (from MS VB helpfile):

------------------------------------------------------
object.TextWidth(strexpr)

The TextWidth method has the following arguments.

Argument Description
object The Report object that determines the font and font point size.
strexpr The text string for which the text width will be determined
-------------------------------------------------------------------------------------

as in

---------------------------------------------------------
Private Sub Detail_Print(Cancel As Integer, _
    PrintCount As Integer)
    ' Set unit of measure to twips (default scale).
    Me.Scalemode = 1
    ' Print name and font size of report font.
    Debug.Print "Report Font: "; Me.FontName
    Debug.Print "Report Font Size: "; Me.FontSize
    ' Print height and width required for text string.
    Debug.Print "Text Height (Twips): "; _
        Me.TextHeight("Product Report")
    Debug.Print "Text Width (Twips): "; _
        Me.TextWidth("Product Report")
End Sub
________________________________________________
Yes I know but this code works in an Access 2002 report
Me.Box2 = TextWidth(Me("Box1"))
Me.Box3 = Me("box1").Width

Author

Commented:
we are getting somewhere here...
now i get a new error message:

Run-time error '2465': Microsoft Access can't find the field 'testing testing testing' referred to in your expression.



"testing testing testing"  is sample date that is supposed to be displayed in this control, not a field name

textwidth(string) takes a string parameter...  but me() takes a field name?

Author

Commented:
here is the code so far:


Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim ctl As Control

For Each ctl In Me.Section(acDetail).Controls
    ' Get width of control to enable autosizing
    ctl.Width = TextWidth(Me(ctl))
Next

Set ctl = Nothing
End Sub
Yes
me.textboxName is the same as
me("textboxName")
in your case it's a variable so
me(ctl)

so textWidth(me(ctl)) says:
give me the width of the text housed in my control identified by the variable ctl.

Make sense?

Author

Commented:
makes sense to me, but doesn't work:  

****************
Run-time error '2465': Microsoft Access can't find the field 'testing testing testing' referred to in your expression.

****************


'testing testing testing' is not the name of the field, it's the data (string) that's supposed to be evaluated.  textwidth() should just take it, but combined with me() it somehow doesn't get it.  perhaps this needs to be converted explicitely to a string inside (me())...?
If you were just trying to test the function TextWidth you don't need Me()
Me.Box2 = TextWidth("testing testing testing")
evaluates the string "testing testing testing"

Me.Box2 = TextWidth(Me"testing testing testing"))
evaluates the width of text contained in a field called "testing testing testing" on Me.

Walt
(oops I was missing a ( in the second example:

Me.Box2 = TextWidth(Me("testing testing testing"))
evaluates the string "testing testing testing"

Author

Commented:
When I put a string like in your example, I still get the same error:  
************************************************
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim ctl As Control

For Each ctl In Me.Section(acDetail).Controls
    ' Get width of control to enable autosizing
    ctl.Width = TextWidth(Me("blah"))
Next

Set ctl = Nothing
End Sub
***********************************************

throws:

****************
Run-time error '2465': Microsoft Access can't find the field 'blah' referred to in your expression.

****************
;(
No.
Like I said Me makes it look for a field name.
TextWidth(Me("blah"))
is looking for a field on me called "blah".  It is NOT evaluating the string "blah"

If you want the width of the word blah don't use Me():
TextWidth("blah")

Author

Commented:
ok, but if i do

textwidth(ctl)

we are back to square one -- the variable messes things up:


*************************************
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim ctl As Control
For Each ctl In Me.Section(acDetail).Controls
   
    ' Get width of control to enable autosizing
    ctl.Width = TextWidth(ctl)
Next

Set ctl = Nothing
End Sub

********************************
throws this:

"Run-time error '438': Object doesn't support this property or method.

CERTIFIED EXPERT
Top Expert 2016

Commented:
jadedataMS Access Systems Creator
CERTIFIED EXPERT

Commented:
On the advice of a friend, Ive been reading this question with great interest.  

It occurs to me that if you simply turn off the borders of the text based controls that their width would no longer even be an issues since the only visible data will be the text in the control.

Author

Commented:
jadedata -- no, i have this hooked up to a code that creates double underline which is as long as the control's width. that's why i need to be able to set the width to the size of the text inside the control. otherwise my double underline will be fixed width.
droz.

textwidth("blah") says test the string blah.
textwidth(me("blah")) says test the field blah on the form Me
textwidth(me(blah)) says find the value of the variable blah.  Find that field on Me and test its value.
So
if ctl is a variable use this:
textwidth(me(ctl))

You may want to try it this way:
textwidth(me(ctl.name))

Author

Commented:
ctl.Width = TextWidth("blah")  // works -- sets every control's width to the length of "blah"
ctl.Width = TextWidth(Me("blah"))  // throws error 2465
ctl.Width = TextWidth(Me(blah))  // also works -- set every control to a size about 3 times the length of "blah"
ctl.Width = TextWidth(Me(ctl))  // throws error 2465
ctl.Width = TextWidth(Me(ctl.Name))  // throws error 438


Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Still doesn't work.  I created this code from yours and got error 438:

------------------------------------start of code--------------------------------------------------

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim ctl As Control
Dim ctlName As String
Dim ctlValue As String
Dim ctlWidth As Integer

For Each ctl In Me.Section(acDetail).Controls
   
    ' Get width of control to enable autosizing
       
    ctlName = ctl.Name
    ctlValue = Me(ctl.ctlName) '<------------ this is where the debugger stops.  Run-time Error 438: Object doesn't support this property or method.
    ctlWidth = TextWidth(ctlValue)
    ctl.Width = ctlWidth
   
Next

Set ctl = Nothing
End Sub
--------------------------------------end of code----------------------------------------------------------

Author

Commented:
DONE!!!  Thank you very much Walt!!  It was getting stuck on Labels.  I was going through all the controls, but only Textboxes were supposed to be analyzed. Labels threw the "object doesn't support" error.   I implemented a tag that identifies textboxes and now it's working!!

Here is the final code:

-------------------------------------------------
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

Dim ctl As Control
Dim ctlWidth As Integer
Dim ctlValue As String

For Each ctl In Me.Section(acDetail).Controls
   
    ' Get width of control to enable autosizing if tag = 'dbl'
    If ctl.Tag = "dbl" Then
        ctlValue = Me(ctl.Name)
        ctlWidth = TextWidth(ctlValue)
        ctl.Width = ctlWidth
   End If
Next

Set ctl = Nothing
End Sub
------------------------------------------------------

Wheeeeeeew.  Thanks again Walt!

Mirek


Whew!  That one was a battle but we got her there.
Glad you've got it going.

Walt
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.