Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

bu kod çalışmıyor: Private Sub NotYaziniz_AfterUpdate() Dim ctrl As Control Set ctrl =…

  1. 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.Refresh
    

    End 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 = intLines
    

    End Function

  2. 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:

    1. Accessing the .Text Property Without Focus:

      In Access VBA, the .Text property of a control can only be accessed when that control has focus. If you try to access .Text when the control doesn't have focus, you'll receive an error. In your code, you're accessing .Text in the GetLineCount function:

      sText = ctrl.Text
      

      Solution:

      Use the .Value property instead of .Text. The .Value property can be accessed regardless of whether the control has focus.

      Revised Code:

      sText = ctrl.Value & ""
      

      Note: The & "" ensures that sText is a string even if the control's value is Null.

    2. Event Timing and Control Refresh:

      The AfterUpdate event occurs after the control's data is updated, but the form may not have visually updated yet. Also, resizing a control and then calling Me.Refresh may not immediately reflect the change.

      Solution:

      After changing the control's height, use Me.Repaint instead of Me.Refresh to force the form to redraw and reflect the new size of the control.

      Revised Code:

      Me.Repaint
      
    3. Calculating the Number of Lines Accurately:

      The GetLineCount function estimates the number of lines based on the number of vbCrLf (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 TextWidth method 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 TextWidth directly for forms, but you can use the TextHeight and TextWidth methods via a Report object or use the Form's Printer object if available. Yet, this can get complex.

      Alternatively, you can adjust the intAvgCharsPerLine to better match your font and control size, or consider using a monospaced font to make character-based calculations more accurate.

    4. Units of Measurement:

      Access uses Twips for measurements (1 inch = 1440 twips). Ensure that lngHeightPerLine is correctly calculated based on the font size and that it's appropriate for your specific control.

      Solution:

      You may need to adjust lngHeightPerLine to 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 Function
    

    Explanation of Changes:

    • Accessed .Value Instead of .Text: Changed sText = ctrl.Text to sText = ctrl.Value & "" to safely get the control's text without needing focus and handling potential Null values.

    • Adjusted Line Height Calculation: Used ctrl.FontSize * 20 to 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: Changed Me.Refresh to Me.Repaint to 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.Print statements 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.