[AccessD] Practical way to handle non-standard length "long text" fields

John Colby jwcolby at gmail.com
Thu Mar 7 16:49:43 CST 2024


I tend to do something like

On error resume next
set mLbl = mtxt.controls(0)

Then I can play with the label assuming that it exists.

On Thu, Mar 7, 2024 at 3:09 PM Ryan Wehler <wrwehler at gmail.com> wrote:

> Jack,
>   Here is my current class:
>
>   Not sure how I should handle "going over" other than just warn the user.
> I don't want to truncate their text because I can't even guarantee the text
> they added was at the end of the textbox.
>
>   I am using the label control that is bound to the textbox in the
> "LabelName" field on the property sheets to use that as a "sure fire" way
> to write out the amount of characters left onto the screen...
>
> Option Compare Database
> Option Explicit
>
> Private WithEvents mTextbox As TextBox
> Private mInitialBackColor   As Long
> Private mLength             As Long
> Private mWasWarned          As Boolean
> Const C_EVENT = "[Event Procedure]"
>
> Private Sub Class_Terminate()
>     Set mTextbox = Nothing
> End Sub
>
> Public Sub mInit(tBox As TextBox, length As Long)
>     Set mTextbox = tBox
>     mLength = length
>     mTextbox.OnChange = C_EVENT
>     mInitialBackColor = mTextbox.BackColor
> End Sub
>
> Private Sub mTextbox_Change()
>     Dim chars As Long
>     With mTextbox
>         chars = mLength - (Len(.text))
>
>         'label bound to control
>         'control.LabelName doesn't allow for compile to reference by name.
>
>         If ControlExists(mTextbox.Controls(0).Name, mTextbox.Parent) Then
>             mTextbox.Controls(0).Caption = _
>                 printf("{0} character{1} remaining.", chars, IIf(chars > 1,
> "s", ""))
>         End If
>
>         Select Case Round((Len(.text) / mLength) * 100)
>             Case 0 To 79
>                 .BackColor = mInitialBackColor
>                 mWasWarned = False
>             Case 80 To 89
>                 .BackColor = vbYellow
>                 mWasWarned = False
>             Case 90 To 99
>                 .BackColor = vbRed
>                 mWasWarned = False
>             Case Is > 100
>                 'avoid repeated warnings when backing off
>                 If mWasWarned = False Then TimedMessageBox printf("You have
> exceeded the maximum length of this field. ({0})", mLength), 1
>                 mWasWarned = True
>         End Select
>     End With
> End Sub
>
> On Thu, Mar 7, 2024 at 11:55 AM jack drawbridge <jackandpat.d at gmail.com>
> wrote:
>
> > Ryan,
> > I'd like to see whatever your solution is. JColby and "class" seems
> > automatic and appropriate, but the copy/paste comment could be a killer.
> I
> > guess it depends on your environment--you know the situation.
> >
> > I know you have mentioned without Change event etc, but thought I should
> > offer this link <https://www.isladogs.co.uk/set-character-limit/> which
> > you
> > can review or ignore.
> >
> > Good luck.
> >
> > jack
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > https://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> >
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


-- 
John W. Colby
Colby Consulting


More information about the AccessD mailing list