[AccessD] Limit entry in unbound forms

Bert-Jan Brinkhuis thevigil at kabelfoon.nl
Thu Apr 10 15:22:16 CDT 2003


Mark,

In A2K I use a class module (From Access 2000 Developer's Handbook, Volume I
(like commented in the code)) and on the change event from a control i like
to limit the chracters for i put:

Call adhLimitChars(ctlName, lngNumberOfCharacters)

The class module is called basLimitChars
Code:

Option Compare Database
Option Explicit

' From Access 2000 Developer's Handbook, Volume I
' by Getz, Litwin, and Gilbert (Sybex)
' Copyright 1999.  All rights reserved.

Private Declare Function SendMessageLong _
 Lib "user32" Alias "SendMessageA" _
 (ByVal hWnd As Long, ByVal wMsg As Long, _
 ByVal wParam As Long, lngValue As Long) As Long

Private Declare Function GetFocus _
 Lib "user32" () As Long

Private Const EM_SETLIMITTEXT As Long = &HC5

Public Sub adhLimitChars(txt As TextBox, lngLimit As Long)
    ' You actually CAN use SendMessage with
    ' Access controls, but you must remember that
    ' the changes you make are only active
    ' as long as this control has the focus.
    ' Therefore, if you want to limit the text in a text
    ' box, you MUST do it each time you enter the
    ' control. To be safe, the only place you can really
    ' do this is in reaction to the Change, BeforeUpdate
    ' or AfterUpdate events.

    Dim hWnd As Long
    Dim lngResult As Long
    Dim lngNewMax As Long

    ' Get the window handle for the current window.
    hWnd = GetFocus()

    ' Hey, what if there's ALREADY too much text in
    ' there?  Limiting the text would make it
    ' impossible to type in there at all.  You want
    ' to set the limit to be the max of the amount
    ' you want and the amount that's in there!
    lngNewMax = Len(txt.Text)
    If lngNewMax < lngLimit Then
        lngNewMax = lngLimit
    End If

    ' Send the message to the current text box
    ' to limit itself to lngNewMax characters.
    SendMessageLong hWnd, EM_SETLIMITTEXT, lngNewMax, 0
End Sub

HTH

Bert-Jan

----- Original Message -----
From: "MarkH" <markH at bitgen.co.uk>
To: <accessd at databaseadvisors.com>
Sent: Thursday, April 10, 2003 10:47 AM
Subject: [AccessD] Limit entry in unbound forms


> Hello all...
>
> I am working with class modules and unbound forms in XP. What I need to
> do is limit the length of text a user can enter. I would like to do this
> as they type so that its not possible to add text beyond whats allowed.
> No warning necessary, just stop at the limit.
>
> I intend to store the max length allowed as properties in the class so
> that the frontend can retrive them without having to hard code them. I
> did have a function that could do this but I am away from the office for
> a few days so don't have it with me :o( (and I didn't write it in the
> first place so have no memory of how it was done)
>
> Any help greatly appreciated... Also, if theres a better way to do it
> I'd very much like to hear that too.
>
> Cheers
>
> Mark
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.449 / Virus Database: 251 - Release Date: 27/01/2003
>
>
> _______________________________________________
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com



More information about the AccessD mailing list