Marshall Barton help!

  • Thread starter Thread starter riccifs
  • Start date Start date
R

riccifs

Hi Marsh,
I'm again in trouble with the AutoSizeTextBox function.
I'm realizing that the code you modified, works only on a existing
text.
When I add a new record the code does not fire anymore!

Please answer me as you can,
Bye Stefano.
 
Hi Marsh,
I'm again in trouble with the AutoSizeTextBox function.
I'm realizing that the code you modified, works only on a existing
text.
When I add a new record the code does not fire anymore!


I check into it tomorrow as I have commitments all day
today.
 
Hi Marsh,
I'm again in trouble with the AutoSizeTextBox function.
I'm realizing that the code you modified, works only on a existing
text.
When I add a new record the code does not fire anymore!


I doubt that a new record has anything to do with it and I
can not get it to fail. Maybe you did something
differently?

If you'll post your code, I will take a look to see if I can
spot something.
 
I doubt that a new record has anything to do with it and I
can not get it to fail. Maybe you did something
differently?

If you'll post your code, I will take a look to see if I can
spot something.

Hi Marsh,
I'll post you back both the module and the form's code as I'm using
them, so you can take a look.
May be I make some mistakes fixing up the lines wrapping or something
like that, I don't known

****start Form's code****
Option Compare Database
Option Explicit

Private Type sRectInteger
Left As Integer
top As Integer
right As Integer
Bottom As Integer
End Type


Private Sub Form_Current()
FudgeIt fAutoSizeTextBoxM(Me.txtExtraInfo, False)
End Sub

Private Sub FudgeIt(sRect As RECT)
With sRect
' ***************NOTE***************
' Be aware of the Fudge values you
' see here if you are using these routines
' to align multiple controls to simulate
' one larger Control.
' ***************NOTE***************

' Becasue of the internal fomatting(Margins) Access
' uses we have to fudge the Control's Height a bit.
If .Bottom > 0 Then
If .Bottom < Me.Detail.Height Then
Me.txtExtraInfo.Height = .Bottom * 1.1
Else
Me.txtExtraInfo.Height = Me.Detail.Height
End If
End If

' Fudge Problem
' on a relative narrow box, <1440 twips, the Text is not rendered
' correctly with my .02 Fudge factor. Access must be using
' an inset margin and the .02 Fudge is not sufficient atnarrower
widths.
' I stuck this IIF statement in for now until I figure out
' the method Access is using.
If .right > 0 Then
If .right < Me.Width Then
Me.txtExtraInfo.Width = .right + IIf((.right * 0.1) < 50,
50, .right * 0.1)
Else
Me.txtExtraInfo.Width = Me.Width
End If
End If
End With
End Sub

Private Sub txtExtraInfo_Change()
FudgeIt fAutoSizeTextBoxM(Me.txtExtraInfo, True)
End Sub
****end Form's code****

****start Module's code****
Option Compare Database
Option Explicit

Public Type RECT
Left As Long
top As Long
right As Long
Bottom As Long
End Type

' Declare API functions
Private Declare Function apiCreateFont Lib "gdi32" _
Alias "CreateFontA" (ByVal H
As Long, _
ByVal W As Long, ByVal E As
Long, _
ByVal O As Long, ByVal W As
Long, _
ByVal i As Long, ByVal u As
Long, _
ByVal S As Long, ByVal c As
Long, _
ByVal OP As Long, ByVal CP As
Long, _
ByVal Q As Long, ByVal PAF As
Long, _
ByVal F As String) As Long

Private Declare Function apiSelectObject Lib "gdi32" _
Alias "SelectObject" (ByVal
hDC As Long, _
ByVal hObject As Long) As Long

Private Declare Function apiDeleteObject Lib "gdi32" _
Alias "DeleteObject" (ByVal
hObject As Long _
) As Long

Private Declare Function apiGetDeviceCaps Lib "gdi32" _
Alias "GetDeviceCaps" (ByVal
hDC As Long, _
ByVal nIndex As Long) As Long

Private Declare Function apiMulDiv Lib "kernel32" _
Alias "MulDiv" (ByVal nNumber
As Long, _
ByVal nNumerator As Long, _
ByVal nDenominator As Long) As
Long

Private Declare Function apiCreateIC Lib "gdi32" _
Alias "CreateICA" (ByVal
lpDriverName As String, _
ByVal lpDeviceName As String,
_
ByVal lpOutput As String, _
lpInitData As Any) 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 apiDeleteDC Lib "gdi32" _
Alias "DeleteDC" (ByVal hDC As
Long) As Long

Private Declare Function apiDrawText Lib "user32" _
Alias "DrawTextA" (ByVal hDC
As Long, _
ByVal lpStr As String, ByVal
nCount As Long, _
lprect As RECT, ByVal wFormat
As Long) As Long

' CONSTANTS
Private Const TWIPSPERINCH = 1440
' Used to ask System for the Logical pixels/inch in Y axis
Private Const LOGPIXELSY = 90

' DrawText() Format Flags
Private Const DT_TOP = &H0
Private Const DT_LEFT = &H0
Private Const DT_SINGLELINE = &H20
Private Const DT_CALCRECT = &H400

Public Function fAutoSizeTextBoxM(ctl As Control, _
UseText As Boolean) As RECT

'Name fAutoSizeTextBoxM
'
'Purpose: Returns Control Width & Height needed to
' display the contents of the Control passed
' to this function. This function
' uses the Control's font attributes to Build
' a Font for the required Calculations into the
' Control passed to this Function
'Version: 2.0 RAW :-)
'Calls: Text API stuff. DrawText performs the actual
' calculation to determine Control Width/Height
'Returns: Standard Rectangle Structure
'Created by: Stephen Lebans
'Credits: Dimitri Furman for debugging the Function
'Date: Jan. 14, 2000
'Time: 12:19:23pm
'Feedback: (e-mail address removed)
'My Web Page: www.lebans.com
'Copyright: Lebans Holdings Ltd.
' May not be resold in whole or part
' but may be used without restriction
' in any application you develop.
'
'Bugs:
'Not tested enough to tell. Let me know
'NEEDS ERROR CHECKING!!!!!
'
'Enjoy
'Stephen Lebans
'***************Code Start***************

' Did we get a valid control passed to us?
If IsNull(ctl.FontSize) Then Exit Function

' Did we get a valid control passed to us?
If Len(ctl & "") = 0 Then Exit Function

' Structure for DrawText calc
Dim sRect As RECT

' Handle to Report's window
Dim hwnd As Long

' Reports Device Context
Dim hDC As Long

' Holds the current screen resolution
Dim lngYdpi As Long

Dim newfont As Long
' Handle to our Font Object we created.
' We must destroy it before exiting main function

Dim oldfont As Long
' Device COntext's Font we must Select back into the DC
' before we exit this function.

' Temporary holder for returns from API calls
Dim lngRet As Long

' Calculate screen Font height
Dim fheight As Long

' Get Controls Parents Window handle
hwnd = ctl.Parent.hwnd
If hwnd = 0 Then Exit Function

' retrieve a handle to a display device context (DC)
' for the client area of the specified window
hDC = apiGetDC(hwnd)

' Because Access control's do not have a permanent
' Device Context, we cannot depend on what we find
' selected into the DC unless the Control has the focus.
' In this case we are simply using the Control's Font
' attributes to build our own font in whatever
' DC is handy. We must Save this DC's Font so we can
' restore the Font when we exit this function.

' Clear our return value
lngRet = 0

' Temporary Information Context for Screen info.
Dim lngIC As Long

' Modified to allow for different screen resolutions
' and printer output. Needed to Calculate Font size
lngIC = apiCreateIC("DISPLAY", vbNullString, _
vbNullString, vbNullString)
If lngIC <> 0 Then
lngYdpi = apiGetDeviceCaps(lngIC, LOGPIXELSY)
apiDeleteDC (lngIC)
Else
lngYdpi = 120 'Default average value
End If

' Calculate/Convert requested Font Height
' into Font's Device Coordinate space
fheight = apiMulDiv(ctl.FontSize, lngYdpi, 72)

' We use a negative value to signify
' to the CreateFont function that we want a Glyph
' outline of this size not a bounding box.

With ctl
newfont = apiCreateFont(-fheight, 0, _
0, 0, .FontWeight, _
.FontItalic, .FontUnderline, _
0, 0, 0, _
0, 0, 0, .FontName)
End With

' Select the new font into our DC.
oldfont = apiSelectObject(hDC, newfont)

' Use DrawText to Calculate height of Rectangle
' required to hold the current contents of the
' Control passed to this function

With sRect
.Left = 0
.top = 0
.Bottom = 0 'ctl.Height / (TWIPSPERINCH / lngYdpi)
.right = 0 'ctl.Width / (TWIPSPERINCH / lngYdpi)
If UseText Then
lngRet = apiDrawText(hDC, ctl.Text, -1, sRect, DT_CALCRECT _
Or DT_TOP Or DT_LEFT)
Else
lngRet = apiDrawText(hDC, ctl.Value, -1, sRect, DT_CALCRECT _
Or DT_TOP Or DT_LEFT)
End If

' Cleanup
lngRet = apiSelectObject(hDC, oldfont)
' Delete the Font we created
apiDeleteObject (newfont)

lngRet = apiReleaseDC(hwnd, hDC)

' Convert RECT values to TWIPS
.Bottom = .Bottom * (TWIPSPERINCH / lngYdpi)
.right = .right * (TWIPSPERINCH / lngYdpi)
End With
fAutoSizeTextBoxM = sRect

End Function
****end Module's code****

I hope you'll find what I'm doing wrong!
Sorry for the problems I'm creating to you with that...

Bye,
Stefano.
 
Hi Marsh,
I'll post you back both the module and the form's code as I'm using
them, so you can take a look.
May be I make some mistakes fixing up the lines wrapping or something
like that, I don't known

****start Form's code****
Option Compare Database
Option Explicit

Private Type sRectInteger
Left As Integer
top As Integer
right As Integer
Bottom As Integer
End Type

Private Sub Form_Current()
FudgeIt fAutoSizeTextBoxM(Me.txtExtraInfo, False)
End Sub

Private Sub FudgeIt(sRect As RECT)
With sRect
' ***************NOTE***************
' Be aware of the Fudge values you
' see here if you are using these routines
' to align multiple controls to simulate
' one larger Control.
' ***************NOTE***************

' Becasue of the internal fomatting(Margins) Access
' uses we have to fudge the Control's Height a bit.
If .Bottom > 0 Then
If .Bottom < Me.Detail.Height Then
Me.txtExtraInfo.Height = .Bottom * 1.1
Else
Me.txtExtraInfo.Height = Me.Detail.Height
End If
End If

' Fudge Problem
' on a relative narrow box, <1440 twips, the Text is not rendered
' correctly with my .02 Fudge factor. Access must be using
' an inset margin and the .02 Fudge is not sufficient atnarrower
widths.
' I stuck this IIF statement in for now until I figure out
' the method Access is using.
If .right > 0 Then
If .right < Me.Width Then
Me.txtExtraInfo.Width = .right + IIf((.right * 0.1) < 50,
50, .right * 0.1)
Else
Me.txtExtraInfo.Width = Me.Width
End If
End If
End With
End Sub

Private Sub txtExtraInfo_Change()
FudgeIt fAutoSizeTextBoxM(Me.txtExtraInfo, True)
End Sub
****end Form's code****

****start Module's code****
Option Compare Database
Option Explicit

Public Type RECT
Left As Long
top As Long
right As Long
Bottom As Long
End Type

' Declare API functions
Private Declare Function apiCreateFont Lib "gdi32" _
Alias "CreateFontA" (ByVal H
As Long, _
ByVal W As Long, ByVal E As
Long, _
ByVal O As Long, ByVal W As
Long, _
ByVal i As Long, ByVal u As
Long, _
ByVal S As Long, ByVal c As
Long, _
ByVal OP As Long, ByVal CP As
Long, _
ByVal Q As Long, ByVal PAF As
Long, _
ByVal F As String) As Long

Private Declare Function apiSelectObject Lib "gdi32" _
Alias "SelectObject" (ByVal
hDC As Long, _
ByVal hObject As Long) As Long

Private Declare Function apiDeleteObject Lib "gdi32" _
Alias "DeleteObject" (ByVal
hObject As Long _
) As Long

Private Declare Function apiGetDeviceCaps Lib "gdi32" _
Alias "GetDeviceCaps" (ByVal
hDC As Long, _
ByVal nIndex As Long) As Long

Private Declare Function apiMulDiv Lib "kernel32" _
Alias "MulDiv" (ByVal nNumber
As Long, _
ByVal nNumerator As Long, _
ByVal nDenominator As Long) As
Long

Private Declare Function apiCreateIC Lib "gdi32" _
Alias "CreateICA" (ByVal
lpDriverName As String, _
ByVal lpDeviceName As String,
_
ByVal lpOutput As String, _
lpInitData As Any) 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 apiDeleteDC Lib "gdi32" _
Alias "DeleteDC" (ByVal hDC As
Long) As Long

Private Declare Function apiDrawText Lib "user32" _
Alias "DrawTextA" (ByVal hDC
As Long, _
ByVal lpStr As String, ByVal
nCount As Long, _
lprect As RECT, ByVal wFormat
As Long) As Long

' CONSTANTS
Private Const TWIPSPERINCH = 1440
' Used to ask System for the Logical pixels/inch in Y axis
Private Const LOGPIXELSY = 90

' DrawText() Format Flags
Private Const DT_TOP = &H0
Private Const DT_LEFT = &H0
Private Const DT_SINGLELINE = &H20
Private Const DT_CALCRECT = &H400

Public Function fAutoSizeTextBoxM(ctl As Control, _
UseText As Boolean) As RECT

'Name fAutoSizeTextBoxM
'
'Purpose: Returns Control Width & Height needed to
' display the contents of the Control passed
' to this function. This function
' uses the Control's font attributes to Build
' a Font for the required Calculations into the
' Control passed to this Function
'Version: 2.0 RAW :-)
'Calls: Text API stuff. DrawText performs the actual
' calculation to determine Control Width/Height
'Returns: Standard Rectangle Structure
'Created by: Stephen Lebans
'Credits: Dimitri Furman for debugging the Function
'Date: Jan. 14, 2000
'Time: 12:19:23pm
'Feedback: (e-mail address removed)
'My Web Page: www.lebans.com
'Copyright: Lebans Holdings Ltd.
' May not be resold in whole or part
' but may be used without restriction
' in any application you develop.
'
'Bugs:
'Not tested enough to tell. Let me know
'NEEDS ERROR CHECKING!!!!!
'
'Enjoy
'Stephen Lebans
'***************Code Start***************

' Did we get a valid control passed to us?
If IsNull(ctl.FontSize) Then Exit Function

' Did we get a valid control passed to us?
If Len(ctl & "") = 0 Then Exit Function

' Structure for DrawText calc
Dim sRect As RECT

' Handle to Report's window
Dim hwnd As Long

' Reports Device Context
Dim hDC As Long

' Holds the current screen resolution
Dim lngYdpi As Long

Dim newfont As Long
' Handle to our Font Object we created.
' We must destroy it before exiting main function

Dim oldfont As Long
' Device COntext's Font we must Select back into the DC
' before we exit this function.

' Temporary holder for returns from API calls
Dim lngRet As Long

' Calculate screen Font height
Dim fheight As Long

' Get Controls Parents Window handle
hwnd = ctl.Parent.hwnd
If hwnd = 0 Then Exit Function

' retrieve a handle to a display device context (DC)
' for the client area of the specified window
hDC = apiGetDC(hwnd)

' Because Access control's do not have a permanent
' Device Context, we cannot depend on what we find
' selected into the DC unless the Control has the focus.
' In this case we are simply using the Control's Font
' attributes to build our own font in whatever
' DC is handy. We must Save this DC's Font so we can
' restore the Font when we exit this function.

' Clear our return value
lngRet = 0

' Temporary Information Context for Screen info.
Dim lngIC As Long

' Modified to allow for different screen resolutions
' and printer output. Needed to Calculate Font size
lngIC = apiCreateIC("DISPLAY", vbNullString, _
vbNullString, vbNullString)
If lngIC <> 0 Then
lngYdpi = apiGetDeviceCaps(lngIC, LOGPIXELSY)
apiDeleteDC (lngIC)
Else
lngYdpi = 120 'Default average value
End If

' Calculate/Convert requested Font Height
' into Font's Device Coordinate space
fheight = apiMulDiv(ctl.FontSize, lngYdpi, 72)

' We use a negative value to signify
' to the CreateFont function that we want a Glyph
' outline of this size not a bounding box.

With ctl
newfont = apiCreateFont(-fheight, 0, _
0, 0, .FontWeight, _
.FontItalic, .FontUnderline, _
0, 0, 0, _
0, 0, 0, .FontName)
End With

' Select the new font into our DC.
oldfont = apiSelectObject(hDC, newfont)

' Use DrawText to Calculate height of Rectangle
' required to hold the current contents of the
' Control passed to this function

With sRect
.Left = 0
.top = 0
.Bottom = 0 'ctl.Height / (TWIPSPERINCH / lngYdpi)
.right = 0 'ctl.Width / (TWIPSPERINCH / lngYdpi)
If UseText Then
lngRet = apiDrawText(hDC, ctl.Text, -1, sRect, DT_CALCRECT _
Or DT_TOP Or DT_LEFT)
Else
lngRet = apiDrawText(hDC, ctl.Value, -1, sRect, DT_CALCRECT _
Or DT_TOP Or DT_LEFT)
End If

' Cleanup
lngRet = apiSelectObject(hDC, oldfont)
' Delete the Font we created
apiDeleteObject (newfont)

lngRet = apiReleaseDC(hwnd,
...

leggi tutto

I think I've found, if I try to comment this part: If Len(ctl & "") =
0 Then Exit Function, it's seem to work, but later I receive an error
on "invalid use of Null"
I don't know...

Bye,
Stefano.
 
I think I've found, if I try to comment this part: If Len(ctl & "") =
0 Then Exit Function, it's seem to work, but later I receive an error
on "invalid use of Null"
I don't know...

Bye,
Stefano.

Hi Marsh,
I'm checked it over and over and I'm having always the same problems.
Could you try to do this sequence for me one more time, please?
1) add a new record
2)start to write something in the textbox
Now, what I'm asking to you is: Could you see the box starts to fit
the text that you have just written?
In my experience, the box will auto fit the text ONLY after the first
time you hit the tab, not early.
may be you have designed it to works in this way...and I'm simply miss
understanding that!

Let me known something, please.
Thanks a lot for your patient anywhere...

Bye,
Stefano.
 
(e-mail address removed) wrote:
I'm again in trouble with the AutoSizeTextBox function.
I'm realizing that the code you modified, works only on a existing
text.
When I add a new record the code does not fire anymore!
I doubt that a new record has anything to do with it and I
can not get it to fail. Maybe you did something
differently?
If you'll post your code, I will take a look to see if I can
spot something.

I'll post you back both the module and the form's code as I'm using
them, so you can take a look.
May be I make some mistakes fixing up the lines wrapping or something
like that, I don't known [snip]
' Did we get a valid control passed to us?
If Len(ctl & "") = 0 Then Exit Function
[snip]
I think I've found, if I try to comment this part: If Len(ctl & "") =
0 Then Exit Function, it's seem to work, but later I receive an error
on "invalid use of Null"
[snip]


Nice detective work. That is indeed the problem. I didn't
see it because the field bound to the text box had a default
value so that If never exited. When I removed the field's
DefaultValue, I did see the problem.

That If needs to check the UseText argument.
Change it to:

If UseText Then
If Len(ctl.Text & "") = 0 Then Exit Function
Else
If Len(ctl & "") = 0 Then Exit Function
End If
 
On 24 Set, 09:06, (e-mail address removed) wrote:
(e-mail address removed) wrote:
I'm again in trouble with the AutoSizeTextBox function.
I'm realizing that the code you modified, works only on a existing
text.
When I add a new record the code does not fire anymore!
I doubt that a new record has anything to do with it and I
can not get it to fail. Maybe you did something
differently?
If you'll post your code, I will take a look to see if I can
spot something.
I'll post you back both the module and the form's code as I'm using
them, so you can take a look.
May be I make some mistakes fixing up the lines wrapping or something
like that, I don't known [snip]
' Did we get a valid control passed to us?
If Len(ctl & "") = 0 Then Exit Function [snip]
I think I've found, if I try to comment this part: If Len(ctl & "") =
0 Then Exit Function, it's seem to work, but later I receive an error
on "invalid use of Null"

[snip]

Nice detective work. That is indeed the problem. I didn't
see it because the field bound to the text box had a default
value so that If never exited. When I removed the field's
DefaultValue, I did see the problem.

That If needs to check the UseText argument.
Change it to:

If UseText Then
If Len(ctl.Text & "") = 0 Then Exit Function
Else
If Len(ctl & "") = 0 Then Exit Function
End If

--Marsh
MVP [MS Access]

Hi Marsh,
you fixed it one more times!
Now everything is working absolutely well, like a... Ferrari's car!
Many, many.... many thanks for your patient. I really appreciated
that.

Bye,
Stefano.
 
Back
Top