That solution requires the control to have the current focus. For Forms,
controls are not able to receive the focus during certain events and
obviously for reports a control cannot receive the focus. Further, that
solution does not possess multiline capability. Finally, for reports,
the DC of the printer must be used to guarantee accurate results.
For all of the reasons listed above I would reccommend the code here:
http://www.lebans.com/textwidth-height.htm
Just my $.02 Chuck
--
HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Chuck Grimsby" <c.*******@worldnet.att.net.invalid> wrote in message
news:h2********************************@4ax.com...
I think this is Dev's code, but I forgot to properly credit it in my
code library. (Sorry.)
Private Declare Function apiGetDeviceCaps _
Lib "gdi32" _
Alias "GetDeviceCaps" _
(ByVal hDC As Long, _
ByVal nIndex As Long) _
As Long
Private Declare Function apiGetFocus _
Lib "user32" _
Alias "GetFocus" () _
As Long
Private Declare Function apiGetDC _
Lib "user32" _
Alias "GetDC" _
(ByVal hwnd As Long) _
As Long
Private Declare Function apiReleaseDC _
Lib "user32" _
Alias "ReleaseDC" _
(ByVal hwnd As Long, _
ByVal hDC As Long) _
As Long
Private Declare Function apiGetTextExtentPoint32 _
Lib "gdi32" _
Alias "GetTextExtentPoint32A" _
(ByVal hDC As Long, _
ByVal lpsz As String, _
ByVal cbString As Long, _
lpSize As SIZE) _
As Long
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY = 90
Private Const TWIPSPERINCH = 1440
Public Function fGetTextWidth( _
ByVal strText As String, _
ctl As Control) _
As Long
Dim hDC As Long
Dim hwnd As Long
Dim lpSize As SIZE
Dim lngRet As Long
On Error Resume Next
If Err = 0 Then
hwnd = apiGetFocus()
hDC = apiGetDC(hwnd)
lngRet = apiGetTextExtentPoint32(hDC, _
strText, _
Len(strText), _
lpSize)
If lngRet <> 0 Then
With lpSize
ConvertPixelsToTwips .cx, .cy
fGetTextWidth = .cx
End With
End If
End If
End Function
Public Sub ConvertPixelsToTwips(x As Long, y As Long)
Dim hDC As Long, hwnd As Long, RetVal As Long
Dim XPIXELSPERINCH As Long, YPIXELSPERINCH As Long
hDC = apiGetDC(Application.hWndAccessApp)
XPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSX)
YPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSY)
RetVal = apiReleaseDC(Application.hWndAccessApp, hDC)
x = (x / XPIXELSPERINCH) * TWIPSPERINCH
y = (y / YPIXELSPERINCH) * TWIPSPERINCH
End Sub
On Wed, 18 Feb 2004 20:17:21 GMT, "Neil Ginsberg" <nr*@nrgconsult.com>
wrote:Is there a function for determining the length of a string of text in
inchesor twips? I need to move a text box over based on the length of a
string oftext in another text box. Thanks!
--
Scotty!, Beam Me Augggg! #$^&% No Carrier