CSdjohnson
Programmer
i have a database with a column called custnum of type char. if i have the following customer numbers in the database:
1
2
3
4
5
6
7
8
9
10
then it is stored like this in the table:
1
10
2
3
4
5
6
7
8
9
i am trying to get the highest custnum so far so that when a user is adding a new customer i can suggest the next highest number based on the numbers allready in use. the customer number can be alphanumeric. here is the code i am using:
If objPrefs.getPrefSettingDef("promptcust", "0" <> 0 And g_iCustMode = 3 Then
Screen.MousePointer = vbHourglass
strSQL = "SELECT MAX(custnum) AS newnum FROM" & Space$(1) & g_oDBIM.DBPfx(primarydb).PfxCusts & "customers"
strSQL = strSQL & Space$(1) & " WHERE type_cd = '" & strCustType & "'"
strSQL = strSQL & Space$(1) & " AND ISNUMERIC(custnum) <> 0"
OpendbDB
CS.OpenRecordset rstRS, strSQL, dbDB, csOpenForwardOnly, csLockReadOnly
If rstRS.EOF Or IsNull(rstRS("newnum") = True Then
strSQL = "SELECT MAX(custnum) AS newnum FROM " & Space$(1) & g_oDBIM.DBPfx(primarydb).PfxCusts & "customers"
strSQL = strSQL & Space$(1) & " WHERE type_cd = '" & strCustType & "'"
CS.OpenRecordset rstRS, strSQL, dbDB, csOpenForwardOnly, csLockReadOnly
End If
If Not rstRS.BOF And Not rstRS.EOF And Not IsNull(rstRS("newnum") Then
strNewNum = rstRS("newnum"
intNewNumLen = Len(strNewNum)
If Not IsNumeric(strNewNum) Then
For intCtr = 1 To Len(strNewNum)
If Not IsNumeric(Right$(strNewNum, intCtr)) Then
Exit For
End If
Next intCtr
If intCtr > 1 Then
lngMyNum = CLng(Right$(strNewNum, intCtr - 1)) + 1
Else
lngMyNum = 1
End If
strNewNum = Left$(strNewNum, Len(strNewNum) - (intCtr - 1))
If Len(strNewNum) + Len(lngMyNum) < Len(rstRS("newnum") Then
strNewNum = strNewNum & "0"
End If
strNewNum = strNewNum & CStr(PadX(lngMyNum, intCtr - 1))
Else
strNewNum = PadX(CLng(strNewNum) + 1, intNewNumLen)
End If
Else
strNewNum = 1
End If
txtTabFour(0).Text = strNewNum
End If
how do i get the correct max custnum so that the correct sugguestion is made to the user?
1
2
3
4
5
6
7
8
9
10
then it is stored like this in the table:
1
10
2
3
4
5
6
7
8
9
i am trying to get the highest custnum so far so that when a user is adding a new customer i can suggest the next highest number based on the numbers allready in use. the customer number can be alphanumeric. here is the code i am using:
If objPrefs.getPrefSettingDef("promptcust", "0" <> 0 And g_iCustMode = 3 Then
Screen.MousePointer = vbHourglass
strSQL = "SELECT MAX(custnum) AS newnum FROM" & Space$(1) & g_oDBIM.DBPfx(primarydb).PfxCusts & "customers"
strSQL = strSQL & Space$(1) & " WHERE type_cd = '" & strCustType & "'"
strSQL = strSQL & Space$(1) & " AND ISNUMERIC(custnum) <> 0"
OpendbDB
CS.OpenRecordset rstRS, strSQL, dbDB, csOpenForwardOnly, csLockReadOnly
If rstRS.EOF Or IsNull(rstRS("newnum") = True Then
strSQL = "SELECT MAX(custnum) AS newnum FROM " & Space$(1) & g_oDBIM.DBPfx(primarydb).PfxCusts & "customers"
strSQL = strSQL & Space$(1) & " WHERE type_cd = '" & strCustType & "'"
CS.OpenRecordset rstRS, strSQL, dbDB, csOpenForwardOnly, csLockReadOnly
End If
If Not rstRS.BOF And Not rstRS.EOF And Not IsNull(rstRS("newnum") Then
strNewNum = rstRS("newnum"
intNewNumLen = Len(strNewNum)
If Not IsNumeric(strNewNum) Then
For intCtr = 1 To Len(strNewNum)
If Not IsNumeric(Right$(strNewNum, intCtr)) Then
Exit For
End If
Next intCtr
If intCtr > 1 Then
lngMyNum = CLng(Right$(strNewNum, intCtr - 1)) + 1
Else
lngMyNum = 1
End If
strNewNum = Left$(strNewNum, Len(strNewNum) - (intCtr - 1))
If Len(strNewNum) + Len(lngMyNum) < Len(rstRS("newnum") Then
strNewNum = strNewNum & "0"
End If
strNewNum = strNewNum & CStr(PadX(lngMyNum, intCtr - 1))
Else
strNewNum = PadX(CLng(strNewNum) + 1, intNewNumLen)
End If
Else
strNewNum = 1
End If
txtTabFour(0).Text = strNewNum
End If
how do i get the correct max custnum so that the correct sugguestion is made to the user?