Access Weirdness when converting / filtering user entered text


Keywords:ms  access 


Question: 

Can someone explain why this works:

Private Sub PartNum_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 47 To 57
            KeyAscii = KeyAscii
        Case 97 To 122
            KeyAscii = KeyAscii - 32
        Case Else
            KeyAscii = 0
    End Select
End Sub

but why this doesn't work:

Function gUC(ByVal gKey As Integer) As Integer
    Select Case gKey
        Case 47 To 57
            gKey = gKey
        Case 97 To 122
            gKey = gKey - 32
        Case Else
            gKey = 0
    End Select
End Function

Private Sub PartNum_KeyPress(KeyAscii As Integer)
    KeyAscii = gUC(KeyAscii)
End Sub

I have multiple fields on my form that I'd like to filter like this and would rather just one function to call rather than re-writing the code for every field


1 Answer: 

I think you're not returning a value properly.

Function gUC(ByVal gKey As Integer) As Integer
    Select Case gKey
        Case 47 To 57
            gUC= gKey
        Case 97 To 122
            gUC= gKey - 32
        Case Else
            gUC= 0
    End Select
End Function