Disabling multi-line fields in MS Access

我的梦境 提交于 2020-01-03 20:22:07

问题


Is there a way to disable entering multi-line entries in a Text Box (i.e., I'd like to stop my users from doing ctrl-enter to get a newline)?


回答1:


I was able to do it on using KeyPress event. Here's the code example:

Private Sub SingleLineTextBox_ KeyPress(ByRef KeyAscii As Integer)
    If KeyAscii = 10 _
        or KeyAscii = 13 Then
            '10 -> Ctrl-Enter. AKA ^J or ctrl-j
            '13 -> Enter.      AKA ^M or ctrl-m
        KeyAscii = 0  'clear the the KeyPress
    End If
End Sub



回答2:


The way I did it before (and the last time I worked in Access was around '97 so my memory is not so hot) was raising a key-up event and executing a VBA function. It's a similar method to what you do with an AJAX suggest text box in a modern webform application, but as I recall it could get tripped up if your Access form has other events which tend to occur frequently such a onMouseMove over the entire form object.




回答3:


Using the KeyPress event means that your code will fire every time the user types. This can lead to screen flickering and other problems (the OnChange event would be the same).

It seems to me that you should use a single event to strip out the CrLf's, and the correct event would be AfterUpdate. You'd simply do this:

  If InStr(Me!MyMemoControl, vbCrLf) Then
     Me!MyMemoControl = Replace(Me!MyMemoControl, vbCrLf, vbNullString)
  End If

Note the use of the Access global constants, vbCrLf (for Chr(10) & Chr(13)) and vbNullString (for zero-length string).

Using a validation rule means that you're going to pop up an ugly error message to your user, but provide them with little in the way of tools to correct the problem. The AfterUpdate approach is much cleaner and easier for the users, seems to me.




回答4:


Thanks Ian and BIBD. I created a public sub based on your answer that is reusable.

Public Sub PreventNewlines(ByRef KeyAscii As Integer)
    If KeyAscii = 10 Or KeyAscii = 13 Then KeyAscii = 0
End Sub

Private Sub textbox_KeyPress(KeyAscii As Integer)
    Call PreventNewlines(KeyAscii)
End Sub

Screen flicker should never be an issue, as these are handled events, not constant polling (and it's per control further limiting the scope). Seems to me like an invalid argument, as every text editor is executing some code per keystroke.

Thanks




回答5:


not entirely sure about that one, you should be able to remove the line breaks when you render the content though, or even run a vbscript to clear it out, you just need to check for chr(13) or vbCrLf.




回答6:


If you don't want an event interfering, you can set up the Validation Rule property for the textbox to be

NOT LIKE "*"+Chr(10)+"*" OR "*"+Chr(13)+"*"

You will probably also want to set the Validation Text to explain specifically why Access is throwing up an error box.



来源:https://stackoverflow.com/questions/18955/disabling-multi-line-fields-in-ms-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!