hello please help me on this i want to use ascii but all i want i will not use keypress only my cmdadd...is this possible without using keypress?..hoping for your positive responds....


sample

public function keytrap(keyascii as integer)

if keyascii >=48 and keyascii<=57 then
msgbox "invalid input"
exit function
end if

end function

public sub txt1.text(keyascii as integer)
call keytrap(keyascii)
end sub

but i want to call keytrap in my cmdadd...is this possible? please help me on this...

Dani AI

Generated

— the main confusion is that KeyAscii comes from a KeyPress/KeyDown event. You cannot declare a control property like txt1.Text(...) or expect the Click handler of a button to receive a KeyAscii value. To validate input when the user clicks the Add button, read the TextBox text and validate that string there.

A simple, robust pattern: write a validator that accepts the TextBox string and returns True/False, then call it from your cmdAdd_Click handler. Example approaches (they show different techniques; pick the one that fits your VB version):

' VB.NET: checks each character with the framework helper
Private Function IsAllDigits(value As String) As Boolean
    If String.IsNullOrEmpty(value) Then Return False
    For Each ch As Char In value
        If Not Char.IsDigit(ch) Then Return False
    Next
    Return True
End Function
' VB6: quick pattern test using Like (returns True only if every char is 0-9)
Private Function IsDigits(s As String) As Boolean
    If s = "" Then IsDigits = False: Exit Function
    IsDigits = Not (s Like "*[!0-9]*")
End Function

Call either validator from cmdAdd_Click, e.g. If Not IsAllDigits(Trim(txt1.Text)) Then MsgBox "Only digits allowed": Exit Sub.

Notes and caveats:

  • IsNumeric accepts decimal points, signs and other numeric formats — use it only when those are acceptable. See IsNumeric docs: IsNumeric.
  • For parsing use Integer.TryParse (VB.NET) to safely convert strings to numbers: Int32.TryParse.
  • Char.IsDigit rejects non-digit characters cleanly: Char.IsDigit.

pointed toward using events; that works if you want to block keys at entry time. ’s idea to inspect characters is valid — the examples above show cleaner, maintainable ways to do that and how to call them from your button click.

Recommended Answers

All 2 Replies

Hi jemz,
if i understand right then you have to call the keytrap function in the Keypress event of command button...
(*The keypress event you will find it by double clicking on the command button, up & right the dropdown list which its value is "Click" -> change it.)

Good luck..

hi jemz,

I assume that you are using a text box and a command button, and you have to check if the text box has only numbers , without using the key press event of the text box and by using the click event of the command button.

if so... then
you can put a loop in the on click event of the command button, using the mid function split the string in text box and check character by character if it is a number if it is not a number set some flag as false(or what ever you want), after the end of the loop if the flag is false display a message box stating that the value entered is not a valid number...

more simple way to do it is (if u dont want the ascii), use IsNumeric() function

hope this will help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.