Error message boxes (VBA newbie)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickhufc
    New Member
    • Nov 2006
    • 5

    Error message boxes (VBA newbie)

    Hi, theres a few things I would like a hand with, and unfortunatly I have only been working with VBA on excel for a few weeks, thanks to a module at university, and so I dont have a great deal of knowledge, and need things explained in simple terms.
    I have userforms with text boxes, but the only data I want inputted into the boxes is numeric, how do I do this, and also how can I get an error message box to appear if text is entered.
    I am using visual basic 6.3 on XP.

    Thanks in advance

    Nick
  • nickhufc
    New Member
    • Nov 2006
    • 5

    #2
    Also I need to only allow positive numbers to be entered into the text boxes, how would I do this.

    Thanks again

    Nick

    Comment

    • albertw
      Contributor
      • Oct 2006
      • 267

      #3
      Originally posted by nickhufc
      Also I need to only allow positive numbers to be entered into the text boxes, how would I do this.

      Thanks again

      Nick
      hi

      open a new method of your textbox -- KeyPress

      Private Sub Text1_KeyPress( Index As Integer, KeyAscii As Integer)
      If InStr("01234567 89" + Chr(8), Chr(KeyAscii)) = 0 Then KeyAscii = 0
      End Sub

      the only keystrokes your textbox will swallow are figures :)

      Comment

      • nickhufc
        New Member
        • Nov 2006
        • 5

        #4
        Originally posted by albertw
        hi

        open a new method of your textbox -- KeyPress

        Private Sub Text1_KeyPress( Index As Integer, KeyAscii As Integer)
        If InStr("01234567 89" + Chr(8), Chr(KeyAscii)) = 0 Then KeyAscii = 0
        End Sub

        the only keystrokes your textbox will swallow are figures :)
        Thanks for the help, but is there anyway I can do it without the keypress, or Ascii parts, as I shouldn't know how to use them for my course!

        Comment

        • albertw
          Contributor
          • Oct 2006
          • 267

          #5
          Originally posted by nickhufc
          Thanks for the help, but is there anyway I can do it without the keypress, or Ascii parts, as I shouldn't know how to use them for my course!
          hi

          you can also call

          if IsNumeric(Text1 .Text)=True or Val(text1.Text) <0 then
          msgbox "Enter only digits and positive values",vbOKOnl y+vbExclamation "
          Text1.SetFocus
          endif

          but you can use this statement only after input of data in the textbox

          Comment

          Working...