-
umm i have a slight problem. i don't want a user pressing the space bar in a text box, so i put in this:
Code:
If KeyAscii = 32 Then ' 32 is the KeyAscii for space bar
MsgBox ("Don't Press The Space Bar", vbExclamation, "NO SPACES")
so you can see i want it's Title to be "NO SPACES", the actual message to be "Don't Press The Space Bar" and it to be an exclamation style box...any help would be appreciated
-
try:
Code:
If KeyAscii = 32 Then ' 32 is the KeyAscii for space bar
MsgBox "Don't Press The Space Bar", vbExclamation, "NO SPACES"
End If
-
If you want to use your syntax then try setting a
var = to the message box -
If KeyAscii = 32 Then ' 32 is the KeyAscii for space bar
response = MsgBox ("Don't Press The Space Bar", vbExclamation, "NO SPACES")
-
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then ' 32 is the KeyAscii For space bar
KeyAscii = 0
MsgBox "Don't Press The Space Bar", vbExclamation, "NO SPACES"
End If
End Sub
-
Well ...
MsgBox is actually a function which returns a value corresponding to the user's response to the message. For e.g. if you provided two buttons, Yes and No, you can check the return value of the MsgBox statement to see if the user clicked on Yes or on No.
If you do not wish to use this return value, for e.g. you are only displaying a message for the user's information, it has only a single button OK and so the user response is immaterial, you can use the MsgBox statement without the parentheses. If you use parentheses, as in your original code, VB expects you to handle the return value of the function. If you omit the parentheses, VB knows that you don't want to handle the return value.
So, the following codes will produce an identical result:
Code:
Msgbox "This is just a warning",vbExclamation + vbOKOnly, "Just a warning"
Code:
Dim MyVar As Integer
MyVar = Msgbox("This is just a warning",vbExclamation + vbOKOnly, "Just a warning")
Code:
Call Msgbox("This is just a warning",vbExclamation + vbOKOnly, "Just a warning")
.
-
JamesM
may i ask why u changed Ascii to 0 in ur example. waste of a line of code
-
Well ...
If the KeyAscii = 0 is omitted, after displaying the message box, the space bar might still be entered in the textbox. KeyAscii corresponds to the character the user has pressed when the KeyPress event is fired. You can manipulate this variable, so that when the event closes, the actual value received by the textbox is something which you desire. If you set this variable to 0, the textbox receives nothing. If you do not set it to zero, the textbox might receive 32, which you do not want.
.