"no spaces" code on Access form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barry Putt
    New Member
    • Jul 2010
    • 2

    "no spaces" code on Access form

    Hello,

    I'm trying to find code to use in order to prohibit users from entering spaces into a field on a form in MS Access 07?

    Any suggestions?

    Thanks.
    Barry
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Code:
    Private Sub YourTextBoxName_KeyDown(KeyCode As Integer, Shift As Integer)
     If KeyCode = vbKeySpace Then
      KeyCode = 0
     Else
      KeyCode = KeyCode
     End If
    End Sub
    Welcome to Bytes!

    Linq ;0)>

    Comment

    • OldBirdman
      Contributor
      • Mar 2007
      • 675

      #3
      That works if entry is by individual keys. It will fail with Copy/Paste.
      I don't know if replacing all the blanks (" ") with zero-length strings ("") will be OK, but that could be done on the BeforeUpdate of the control.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        I don't think there's a really satisfactory way of doing it I'm afraid.

        The InputMask options don't seem to cover that. The KeyDown suffers from being open to Pasting. The BeforeUpdate allows the data to be typed, even if it doesn't allow for the character to be passed to the underlying field. I would suggest the Change event procedure to catch each character entered and use (as suggested by OB) :
        Code:
        Private Sub XXX_Change()
            Me.XXX = Replace(Me.XXX, " ", "")
        End Sub

        Comment

        Working...