Question : Get MAX number from a text field

I've been using some code to find the max number in a text field. It's been working great for several months now. Even though it was a text field, the only data entered were numbers. Recently that has changed and now occasionally text gets entered along with the numbers and that is causing the code to not work.

I need help in figuring how to get around this so it will work again.

I have attached a picture of the query that pulled the records with this issue. The left column, "recnum" is a number field, so it works fine. The other is where I am experiencing the problem.

When I run this code I get the "Run-time error '13': Type mismatch" error on the two text fields on lines 4 and 6.

You will notice there is no standard location of where the text may be entered; maybe on the end, or maybe in the middle of the string.

Thanks,
George
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Private Sub BtnTest_Click()
'Get Max Record Id, Max Ordnum Record, Max InvNumRecord
 Dim maxrecord As Integer
    maxrecord = DMax("[recnum]", "[srvinv]") + 1
 Dim maxordnum As Integer
    maxordnum = CInt(DMax("format([ordnum],""0000"")", "[srvinv]")) + 1
 Dim maxinvnum As Integer
    maxinvnum = CInt(DMax("format([invnum],""0000"")", "[srvinv]")) + 1
MsgBox "Max Record = " & maxrecord & "Max Order = " & maxordnum & "Max Inv = " & maxinv
End Sub

Answer : Get MAX number from a text field

example

maxinvnum = DMax("val([invnum])", "[srvinv]") + 1

Random Solutions  
 
programming4us programming4us