Solved

textwidth method -- syntax question

Posted on 2004-04-01
25
584 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
0
Comment
Question by:drozdzowski
  • 12
  • 11
  • +1
25 Comments
 
LVL 17

Expert Comment

by:walterecook
ID: 10736021
Access?

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

Walt
0
 
LVL 17

Expert Comment

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

Walt
0
 

Author Comment

by:drozdzowski
ID: 10736039
yeah, it's access.  i have to use the textwidth method.  width gives me only the width of the control, not it's contents
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10736109
Sorry.
Brain cloud.
  How about this:
ctl.width = TextWidth(Me(ctl))
Walt
0
 

Author Comment

by:drozdzowski
ID: 10736155
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
________________________________________________
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10736170
Yes I know but this code works in an Access 2002 report
Me.Box2 = TextWidth(Me("Box1"))
Me.Box3 = Me("box1").Width
0
 

Author Comment

by:drozdzowski
ID: 10736236
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?

0
 

Author Comment

by:drozdzowski
ID: 10736249
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
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10736286
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?
0
 

Author Comment

by:drozdzowski
ID: 10736333
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())...?
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10736369
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
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10736378
(oops I was missing a ( in the second example:

Me.Box2 = TextWidth(Me("testing testing testing"))
evaluates the string "testing testing testing"
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:drozdzowski
ID: 10736410
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.

****************
;(
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10736482
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")
0
 

Author Comment

by:drozdzowski
ID: 10736528
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.

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 10736577
0
 
LVL 32

Expert Comment

by:jadedata
ID: 10736578
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.
0
 

Author Comment

by:drozdzowski
ID: 10736614
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.
0
 

Author Comment

by:drozdzowski
ID: 10736626
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10736696
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))

0
 

Author Comment

by:drozdzowski
ID: 10736784
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


0
 
LVL 17

Accepted Solution

by:
walterecook earned 500 total points
ID: 10736914
Whew!
I've got this code working without error:

Dim ctl As Control
Dim someTextVar, someNumVar
For Each ctl In Me.Section(acDetail).Controls
    ' Get width of control to enable autosizing
    someTextVar = ctl.Name ' name of current control
    someTextVar = Me(ctl.Name) ' value in current control
    someNumVar = TextWidth(someTextVar) ' width of that value
    ctl.Width = someNumVar
Next

I just did it with variables so you can set a break point and track the values.
Hopefully that does it for you
Walt
0
 

Author Comment

by:drozdzowski
ID: 10737062
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----------------------------------------------------------

0
 

Author Comment

by:drozdzowski
ID: 10737206
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


0
 
LVL 17

Expert Comment

by:walterecook
ID: 10737756
Whew!  That one was a battle but we got her there.
Glad you've got it going.

Walt
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now