Log in

View Full Version : [SLEEPER:] Character to number



olimonty
02-04-2020, 07:36 PM
Hello guys,

I'm having a little issue right now with a function. When the user enter a caracter, I need to convert this caracter in number.



Function Convert(ByVal Value As String) As Integer
If Value= "A" Then
Convert = 10
ElseIf Value = "B" Then
Convert = 11
ElseIf Value = "C" Then
Convert = 12
ElseIf Value = "D" Then
Convert = 13
ElseIf Value = "E" Then
Convert = 14
ElseIf Value = "F" Then
Convert = 15
Else: Convert = Value
End If
End Function



Thank you

Aussiebear
04-06-2025, 09:30 PM
Try a Case select method


Function Convert(ByVal Value As String) As Integer
Select Case Value
Case "A"
Convert = 10
Case "B"
Convert = 11
Case "C"
Convert = 12
Case "D"
Convert = 13
Case "E"
Convert = 14
Case "F"
Convert = 15
Case Else
' Assuming Value should be converted to an integer if it's not A-F
' You might want to add error handling here if Value isn't always a valid number
If IsNumeric(Value) Then
Convert = CInt(Value)
Else
' Handle the case where Value is not A-F and not a number
' For example:
Convert = 0
' Or raise an error, or return a specific error code
End If
End Select
End Function

June7
04-06-2025, 10:48 PM
Doubt OP is still hanging around looking for answer so we'll never know if they found resolution. And their code works anyway except for case sensitivity. Maybe that's what they were dealing with.

Instead of If Then or Select Case, could:

Convert = Asc(UCase(Value)) - 55

Could even calc in query or textbox without VBA.

June7
04-07-2025, 08:17 AM
Oops, disregard my comment about case sensitivity. Unfortunately, can no longer edit my earlier post.

arnelgp
04-14-2025, 09:00 AM
Public Function Convert(Byval Value As String) As Integer
On Error Resume Next
Convert = CInt("&H" & Value)
End Function

June7
04-14-2025, 09:47 AM
Nice, arnel. And again, can be expression in query or textbox.