Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

textwidth method -- syntax question

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
drozdzowski
Asked:
drozdzowski
  • 12
  • 11
  • +1
1 Solution
 
walterecookCommented:
Access?

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

Walt
0
 
walterecookCommented:
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
 
drozdzowskiAuthor 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
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
walterecookCommented:
Sorry.
Brain cloud.
  How about this:
ctl.width = TextWidth(Me(ctl))
Walt
0
 
drozdzowskiAuthor 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
________________________________________________
0
 
walterecookCommented:
Yes I know but this code works in an Access 2002 report
Me.Box2 = TextWidth(Me("Box1"))
Me.Box3 = Me("box1").Width
0
 
drozdzowskiAuthor 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?

0
 
drozdzowskiAuthor 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
0
 
walterecookCommented:
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
 
drozdzowskiAuthor 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())...?
0
 
walterecookCommented:
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
 
walterecookCommented:
(oops I was missing a ( in the second example:

Me.Box2 = TextWidth(Me("testing testing testing"))
evaluates the string "testing testing testing"
0
 
drozdzowskiAuthor 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.

****************
;(
0
 
walterecookCommented:
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
 
drozdzowskiAuthor 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.

0
 
Rey Obrero (Capricorn1)Commented:
0
 
jadedataMS Access Systems CreatorCommented:
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
 
drozdzowskiAuthor 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.
0
 
drozdzowskiAuthor Commented:
0
 
walterecookCommented:
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
 
drozdzowskiAuthor 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


0
 
walterecookCommented:
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
 
drozdzowskiAuthor 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----------------------------------------------------------

0
 
drozdzowskiAuthor 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


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

Walt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 12
  • 11
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now