bu kod çalışmıyor: Private Sub NotYaziniz_AfterUpdate() Dim ctrl As Control Set ctrl =…
- User
bu kod çalışmıyor: Private Sub NotYaziniz_AfterUpdate() Dim ctrl As Control Set ctrl = Me.NotYaziniz
' Metin kutusundaki toplam satır sayısını hesaplayın Dim intTotalLines As Integer intTotalLines = GetLineCount(ctrl) ' Her satır için yükseklik değeri (twip cinsinden) Dim lngHeightPerLine As Long lngHeightPerLine = 550 ' Bu değeri fontunuza göre ayarlayabilirsiniz ' Yeni yüksekliği hesaplayın ctrl.Height = (intTotalLines * lngHeightPerLine) + 120 ' Ek 120 twip boşluk için ' Değişikliği yansıtmak için formu yenileyin Me.RefreshEnd Sub
' Metin kutusundaki satır sayısını hesaplayan fonksiyon Private Function GetLineCount(ctrl As Control) As Integer Dim sText As String sText = ctrl.Text
' Satır sayısını yeni satır karakterlerine göre hesaplayın Dim intLines As Integer intLines = UBound(Split(sText, vbCrLf)) + 1 ' Metnin uzunluğunu da değerlendirerek satır sayısını artırabiliriz Dim intAvgCharsPerLine As Integer intAvgCharsPerLine = 100 ' Tahmini satır başına karakter sayısı intLines = intLines + (Len(sText) \ intAvgCharsPerLine) GetLineCount = intLinesEnd Function
- Assistant
It sounds like you're trying to dynamically adjust the height of a text box (
NotYaziniz) in a Microsoft Access form based on the number of lines of text it contains. The code you've provided seems close to achieving this, but there are a few issues that might be causing it not to work.Possible Issues and Solutions:
-
Accessing the
.TextProperty Without Focus:In Access VBA, the
.Textproperty of a control can only be accessed when that control has focus. If you try to access.Textwhen the control doesn't have focus, you'll receive an error. In your code, you're accessing.Textin theGetLineCountfunction:sText = ctrl.TextSolution:
Use the
.Valueproperty instead of.Text. The.Valueproperty can be accessed regardless of whether the control has focus.Revised Code:
sText = ctrl.Value & ""Note: The
& ""ensures thatsTextis a string even if the control's value isNull. -
Event Timing and Control Refresh:
The
AfterUpdateevent occurs after the control's data is updated, but the form may not have visually updated yet. Also, resizing a control and then callingMe.Refreshmay not immediately reflect the change.Solution:
After changing the control's height, use
Me.Repaintinstead ofMe.Refreshto force the form to redraw and reflect the new size of the control.Revised Code:
Me.Repaint -
Calculating the Number of Lines Accurately:
The
GetLineCountfunction estimates the number of lines based on the number ofvbCrLf(carriage return + line feed) characters and an average characters per line. This might not be accurate, especially if text wraps within the control.Solution:
Use the
TextWidthmethod to calculate the width of the text and determine how many lines it would occupy based on the width of the text box.However, Access doesn't provide
TextWidthdirectly for forms, but you can use theTextHeightandTextWidthmethods via aReportobject or use theForm'sPrinterobject if available. Yet, this can get complex.Alternatively, you can adjust the
intAvgCharsPerLineto better match your font and control size, or consider using a monospaced font to make character-based calculations more accurate. -
Units of Measurement:
Access uses Twips for measurements (1 inch = 1440 twips). Ensure that
lngHeightPerLineis correctly calculated based on the font size and that it's appropriate for your specific control.Solution:
You may need to adjust
lngHeightPerLineto match the actual line height of your text in the control.Revised Code:
lngHeightPerLine = Me.NotYaziniz.FontSize * 20 ' Approximate line height in twips
Revised Full Code:
Private Sub NotYaziniz_AfterUpdate() Dim ctrl As Control Set ctrl = Me.NotYaziniz ' Calculate the total number of lines in the text box Dim intTotalLines As Integer intTotalLines = GetLineCount(ctrl) ' Height per line (in twips) Dim lngHeightPerLine As Long lngHeightPerLine = ctrl.FontSize * 20 ' Adjust based on the font size ' Calculate the new height ctrl.Height = (intTotalLines * lngHeightPerLine) + 120 ' Add extra space if needed ' Refresh the form to reflect changes Me.Repaint End Sub ' Function to calculate the number of lines in the text box Private Function GetLineCount(ctrl As Control) As Integer Dim sText As String sText = ctrl.Value & "" ' Calculate the number of lines based on carriage returns Dim intLines As Integer intLines = UBound(Split(sText, vbCrLf)) + 1 ' Adjust for text wrapping within the control width ' Estimate the number of additional lines due to wrapping Dim intCharsPerLine As Integer intCharsPerLine = (ctrl.Width / (ctrl.FontSize * 7)) ' Approximate character width Dim intTotalChars As Integer intTotalChars = Len(sText) ' Calculate estimated lines due to wrapping Dim intWrapLines As Integer intWrapLines = intTotalChars \ intCharsPerLine ' Total estimated lines intLines = intLines + intWrapLines GetLineCount = intLines End FunctionExplanation of Changes:
-
Accessed
.ValueInstead of.Text: ChangedsText = ctrl.TexttosText = ctrl.Value & ""to safely get the control's text without needing focus and handling potentialNullvalues. -
Adjusted Line Height Calculation: Used
ctrl.FontSize * 20to better estimate the height of a line based on the font size. -
Improved Line Count Estimation: Included an estimation of how many lines the text will occupy based on the control's width and font size, which accounts for text wrapping.
-
Used
Me.Repaint: ChangedMe.RefreshtoMe.Repaintto ensure the form redraws and the size changes are reflected immediately.
Additional Suggestions:
-
Test Incrementally: Try running the code after each change to see if the issue is resolved.
-
Error Handling: Add error handling to your code to capture and debug any runtime errors.
On Error Resume Next ' Your code here If Err.Number <> 0 Then MsgBox "An error occurred: " & Err.Description Err.Clear End If -
Debugging: Use
Debug.Printstatements or breakpoints to check the values of variables during execution.
Final Note:
Adjusting control sizes dynamically can be tricky due to variations in font metrics, control properties, and how Access handles rendering. If you continue to experience issues, consider using a fixed control size with scroll bars enabled or restructuring your form to accommodate varying text lengths in a different way.
-