Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only Numbers in textbox

Status
Not open for further replies.

DaveRussell

Programmer
Jul 31, 2003
22
CA
Hi there, first... thanks for all the contributions you people make in this forum, i have gotten many a question answered just by reading the posts.

second: I am trying to create a text box that will only allow numbers to be imputted into it when a certain contition is true... here is my code:

Code:
Private Sub bSearch_Click()

Dim searchResults As DAO.Recordset
Dim selectStr As String

If ogSearchBy.Value = 1 Then
    selectStr = "SELECT * FROM lclCustomerInfo WHERE ciLastName = '" & tbSearch.Value & "';"
ElseIf ogSearchBy.Value = 2 Then
    selectStr = "SELECT * FROM lclCustomerInfo WHERE ciUserName = '" & tbSearch.Value & "';"
ElseIf ogSearchBy.Value = 3 Then
    selectStr = "SELECT * FROM lclCustomerInfo WHERE AcctID = " & tbSearch.Value & ";"
End If

Set db = CurrentDb
Set searchResults = db.OpenRecordSet(selectStr)

MsgBox (searchResults.RecordCount)

End Sub

Private Sub tbSearch_KeyPress(KeyAscii As Integer)

If ogSearchBy.Value = 3 Then
  'only allow numbers to be entered
End If
End Sub

So, if anyone has any ideas... i got this to work in .NET using a SELECT/CASE statement, but i can't figure it out now. go figure. thanks in advance

dave.
 
Okay, well i just figured out one way using the IsNumeric() Function:

Code:
If ogSearchBy.Value = 1 Then
    selectStr = "SELECT * FROM lclCustomerInfo WHERE ciLastName = '" & tbSearch.Value & "';"
ElseIf ogSearchBy.Value = 2 Then
    selectStr = "SELECT * FROM lclCustomerInfo WHERE ciUserName = '" & tbSearch.Value & "';"
ElseIf ogSearchBy.Value = 3 Then

    If IsNumeric(tbSearch.Value) = False Then
     MsgBox ("This is not a valid search term for this search type.  Please enter a valid account number")
     Exit Sub
    Else
      selectStr = "SELECT * FROM lclCustomerInfo WHERE AcctID = " & tbSearch.Value & ";"
    End If
    
End If

it would still be nice to just validate as they entered it into the field though.
 
Hi!

[tt]Private Sub tbSearch_KeyPress(KeyAscii As Integer)

If ogSearchBy.Value = 3 Then
if keyascii<=48 or keyascii>=57 then
keyascii=0 ' cancelling the keystroke
end if
End If
End Sub[/tt]

- ensure the forms keypreview property is set to yes

Roy-Vidar
 
Hey! that's perfect Roy-Vidar, thanks! i don't suppose you know the backspace number off hand, do you? i'd like them to be able to use that one too :)
 
BasckSpace is 8

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top