none
FUNCTION PROBLEM RRS feed

  • Question

  • HI

    Use this function to generate incremental code
    The code begins with a fixed value
    After that, incremental values are added after the fixed value
    For example

    82 = FIXED VALUE 

    1   = ADD VALUE +1 

    FULL CODE =FIXED VALUE  82 &  ADD VALUE +1

    FULL CODE = 821 : 822 :823 :824 ETIC

    The problem occurs with me after 10 numbers 8210

    Do not increase after the process.

    CODE

     Public Function serial(ByVal ds As DataSet, ByVal tb As String, ByVal tbcode As String) As String
            ' MAX NUMBER FROM DS
            Dim bignum As String = CType(ds.Tables(tb).Compute("Max(" & tbcode & ")", tbcode & " >= 0"), String)
            Dim mainval, mainvalB, result As String
            mainval = bignum.Substring(0, 2)
            mainvalB = bignum.Remove(0, 2)
            result = CStr(CLng(mainvalB) + 1)
            serial = mainval + result
        End Function


    • Edited by ahmeddc Thursday, December 13, 2018 8:29 AM
    Thursday, December 13, 2018 8:29 AM

Answers

  •    Public Function serial(ByVal ds As DataSet, ByVal tb As String, ByVal tbcode As String) As String
    
            Dim bignum As String = CType(ds.Tables(tb).Compute("Max(" & tbcode & ")", tbcode & " >= 0"), String)
    
    
            Dim mainval, mainvalB, result As Integer
    
            mainval = CInt(bignum.Substring(0, 2))
            mainvalB = CInt(bignum.Remove(0, 2))
            result = mainvalB + 1
    
            If result > 9 Then
                mainval += 1
                result = 0
            End If
    
            serial = Str(mainval) & Str(result)
    
        End Function
    
    

    • Marked as answer by ahmeddc Friday, December 14, 2018 3:58 PM
    Thursday, December 13, 2018 9:14 AM
  • Hi,

    fix your code:

      If result = 10 Then
                serial = (CLng(mainval) + 1).ToString + "0"
            Else
                serial = mainval + result
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Friday, December 14, 2018 3:57 PM
    Thursday, December 13, 2018 9:13 AM

All replies

  • Hi,

    fix your code:

      If result = 10 Then
                serial = (CLng(mainval) + 1).ToString + "0"
            Else
                serial = mainval + result
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Friday, December 14, 2018 3:57 PM
    Thursday, December 13, 2018 9:13 AM
  •    Public Function serial(ByVal ds As DataSet, ByVal tb As String, ByVal tbcode As String) As String
    
            Dim bignum As String = CType(ds.Tables(tb).Compute("Max(" & tbcode & ")", tbcode & " >= 0"), String)
    
    
            Dim mainval, mainvalB, result As Integer
    
            mainval = CInt(bignum.Substring(0, 2))
            mainvalB = CInt(bignum.Remove(0, 2))
            result = mainvalB + 1
    
            If result > 9 Then
                mainval += 1
                result = 0
            End If
    
            serial = Str(mainval) & Str(result)
    
        End Function
    
    

    • Marked as answer by ahmeddc Friday, December 14, 2018 3:58 PM
    Thursday, December 13, 2018 9:14 AM
  •    Public Function serial(ByVal ds As DataSet, ByVal tb As String, ByVal tbcode As String) As String
    
            Dim bignum As String = CType(ds.Tables(tb).Compute("Max(" & tbcode & ")", tbcode & " >= 0"), String)
    
    
            Dim mainval, mainvalB, result As Integer
    
            mainval = CInt(bignum.Substring(0, 2))
            mainvalB = CInt(bignum.Remove(0, 2))
            result = mainvalB + 1
    
            If result > 9 Then
                mainval += 1
                result = 0
            End If
    
            serial = Str(mainval) & Str(result)
    
        End Function

    hi tommy

    1-I want to make the serial for  infinity value Does this code work?

    All programs depend on this function there is no possibility of errors

    2-Why does not the error occur only at the number 10


    • Edited by ahmeddc Thursday, December 13, 2018 9:57 AM
    Thursday, December 13, 2018 9:56 AM
  • Hi,

    fix your code:

      If result = 10 Then
                serial = (CLng(mainval) + 1).ToString + "0"
            Else
                serial = mainval + result
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    hi alex

    2-Why does not the error occur only at the number 10

    Thursday, December 13, 2018 9:57 AM
  •    Public Function serial(ByVal ds As DataSet, ByVal tb As String, ByVal tbcode As String) As String
    
            Dim bignum As String = CType(ds.Tables(tb).Compute("Max(" & tbcode & ")", tbcode & " >= 0"), String)
    
    
            Dim mainval, mainvalB, result As Integer
    
            mainval = CInt(bignum.Substring(0, 2))
            mainvalB = CInt(bignum.Remove(0, 2))
            result = mainvalB + 1
    
            If result > 9 Then
                mainval += 1
                result = 0
            End If
    
            serial = Str(mainval) & Str(result)
    
        End Function

    hi tommy

    1-I want to make the serial for  infinity value Does this code work?

    All programs depend on this function there is no possibility of errors

    2-Why does not the error occur only at the number 10


    "1-I want to make the serial for  infinity value Does this code work?

    All programs depend on this function there is no possibility of errors"

    I dont know. I can not say I dont know what you are doing to infinity. It depends how you call this function with what values and ranges. What is bignum range?

    You probably need to test it.

    I don't know if this function is correct or not. I guess what you want to do.


    "2-Why does not the error occur only at the number 10"

            bignum = 829

            mainval = bignum.Substring(0, 2)
            mainvalB = bignum.Remove(0, 2)
            result = CStr(CLng(mainvalB) + 1)
            serial = mainval + result

            mainval = 82
            mainvalB = 9

            result = 9 + 1 = 10

            serial = mainval + result

       this is string addition just adding characters or strings 10 to end of chrs 82

            serial = "82" & "10" = "8210"          where 82 and 10 are strings


       what we want is math numbers addition:

            serial = cstr( 820 + 10 ) = "830"      where 820 and 10 are integer number values

       correct? we want - 830 ?

    You should use math addition of the number values when you are adding numbers. I changed your original from str to int.






    Thursday, December 13, 2018 10:14 AM
  • PS No the function does not work to infinity because of this:

        bignum.Remove(0, 2)

    So you have to change that.

    I don't see why you do these numbers so I cant help much. I would not do string addition like you are doing in the first place. I don't know the purpose of the game.


    PS Actually now I am not sure again. You have to define the problem better. You should test it.
    Thursday, December 13, 2018 10:29 AM
  • PS No the function does not work to infinity because of this:

        bignum.Remove(0, 2)

    So you have to change that.

    I don't see why you do these numbers so I cant help much. I would not do string addition like you are doing in the first place. I don't know the purpose of the game.


    PS Actually now I am not sure again. You have to define the problem better. You should test it.

    This function generates a serial for each client and the serial is unique
    non dublicate
    For the program I do not know how much the user will add the names of clients to know number serial

    The same function is also used for sales invoice and has unspecified numbers
    There must be constant values for reviewing and searching for the procedure for each process
    There is a fixed value start serial

    82

    92

    Thursday, December 13, 2018 10:45 AM
  • PS No the function does not work to infinity because of this:

        bignum.Remove(0, 2)

    So you have to change that.

    I don't see why you do these numbers so I cant help much. I would not do string addition like you are doing in the first place. I don't know the purpose of the game.


    PS Actually now I am not sure again. You have to define the problem better. You should test it.

    This function generates a serial for each client and the serial is unique
    non dublicate
    For the program I do not know how much the user will add the names of clients to know number serial

    The same function is also used for sales invoice and has unspecified numbers
    There must be constant values for reviewing and searching for the procedure for each process
    There is a fixed value start serial

    82

    92

    Hi

    I can't understand anything you post, but, I saw the word 'serial' which is enough for me.

    Here is some code, created for a previous question containing the word 'serial' (but, it was an understandable post).

    This code creates a set of serial numbers, up to 32 charachter length and presented in groups.

    I bet you that this code is nothing like what you are wanting - go on bet me - go on .............

    ' GUID, Serial, Random, Generate
    
    ' Blank Form1 
    ' create up to 32 char Serial Numbers
    ' split into groups
    ' First TB = Serial length (trimmed)
    ' Second TB = group size
    ' Third = how many serials to generate
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Generated As Boolean = False
      Dim RegList As New List(Of String)
      Dim bs As New BindingSource()
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Dim iii As Integer = 0
    	Do
    	  Dim gb() As Byte = Guid.NewGuid().ToByteArray()
    
    	  iii = BitConverter.ToInt32(gb, 0)
    
    	  Dim lll As Long = BitConverter.ToInt64(gb, 0)
    	Loop While iii < 0
    
    	SetUpControls()
    	bs.DataSource = RegList
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs)
    	RegList.Clear()
    	Dim sn As New SerNum
        Dim sz As Integer = GetInteger(TextBox2.Text)
        Dim fld As Integer = GetInteger(TextBox3.Text)
        Dim howmany As Integer = GetInteger(TextBox1.Text) - 1
    
        If howmany < 0 Then howmany = 0
    
        If sz < 8 Then sz = 8
        If fld < 2 Then fld = 2
    
        For i As Integer = 0 To howmany
          RegList.Add(sn.Generate(sz, fld))
        Next
        ListBox1.DataSource = Nothing
        ListBox1.DataSource = bs
      End Sub
      Private Sub Button2_Click(sender As Object, e As EventArgs)
    	Dim s As String = Trim(TextBox4.Text).ToUpper
    	If s = Nothing Then Exit Sub
    
    	If ListBox1.Items.Contains(s) Then
    	  TextBox4.Text = "Successful registration"
    	Else
    	  TextBox4.Text = "Registration failed"
    	End If
      End Sub
      Function GetInteger(s As String) As Integer
    	Dim v As Integer = 0
    	If Integer.TryParse(s, v) Then Return v
    	Return 0
      End Function
    
      ' set up controls
      Dim WithEvents ListBox1 As New ListBox
      Dim WithEvents TextBox1, TextBox2, TextBox3, TextBox4 As New TextBox
      Dim WithEvents button1, button2 As New Button
      Sub SetUpControls()
    	With ListBox1
    	  .Location = New Point(10, 34)
    	  .Size = New Size(ClientSize.Width - 20, ClientSize.Height - 80)
    	  .Font = New Font("Microsoft Sans Serif", 14)
    	  .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    	End With
    	With TextBox1
    	  .Font = New Font("Microsoft Sans Serif", 12)
    	  .Location = New Point(80, 3)
    	  .Width = 60
    	  .Text = "20"
    	  .TextAlign = HorizontalAlignment.Center
    	End With
    	With TextBox2
    	  .Font = New Font("Microsoft Sans Serif", 12)
    	  .Location = New Point(10, 3)
    	  .Width = 30
    	  .Text = "32"
    	  .TextAlign = HorizontalAlignment.Center
    	End With
    	With TextBox3
    	  .Font = New Font("Microsoft Sans Serif", 12)
    	  .Location = New Point(44, 3)
    	  .Width = 30
    	  .Text = "10"
    	  .TextAlign = HorizontalAlignment.Center
    	End With
    	With TextBox4
    	  .Font = New Font("Microsoft Sans Serif", 12)
    	  .Location = New Point(10, ListBox1.Bottom + 8)
    	  .Width = ListBox1.Width - 80
    	  .Text = Nothing
    	  .TextAlign = HorizontalAlignment.Center
    	  .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
    	End With
    	With button1
    	  .Font = New Font("Microsoft Sans Serif", 10)
    	  .AutoSize = True
    	  .Text = "Generate"
    	  .Location = New Point(TextBox1.Right + 10, 3)
    	End With
    	With button2
    	  .Font = New Font("Microsoft Sans Serif", 10)
    	  .AutoSize = True
    	  .Text = "Check"
    	  .Location = New Point(ListBox1.Right - button2.Width, TextBox4.Top + 4)
    	  .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
    	End With
    
    	Controls.AddRange({ListBox1, TextBox1, TextBox2, TextBox3, TextBox4, button1, button2})
    	AddHandler button1.Click, AddressOf Button1_Click
    	AddHandler button2.Click, AddressOf Button2_Click
      End Sub
    End Class
    
    ' ---- based on a post by Karen Payne
    Public Class SerNum
      Public Shared Property Cols As Integer
      Public ReadOnly Property Generate(len As Integer, fieldsize As Integer) As String
    	Get
    	  Cols = 1
    	  Dim maxlen As Integer = 32
    	  If fieldsize > maxlen Then fieldsize = maxlen
    	  Do While Not len Mod fieldsize = 0
    		len += 1
    	  Loop
    	  Do While len > maxlen
    		len -= fieldsize
    	  Loop
    	  If len < fieldsize Then
    		len = fieldsize
    	  End If
    	  Return GetNewSerNum(len, fieldsize)
    	End Get
      End Property
    
      Private Function GetNewSerNum(len As Integer, fieldsize As Integer) As String
    
    	Dim SerNum As String = Guid.NewGuid.ToString("N").Substring(0, len).ToUpper
    	Dim a() As Char = SerNum.ToCharArray()
    	Dim result As String = Nothing
    	Dim j As Integer = 0
    	For i As Integer = 0 To len - 1
    	  For j = i To fieldsize + i - 1
    		result += a(j)
    	  Next
    	  If j = len Then
    		Exit For
    	  Else
    		i = (j) - 1
    		result += "-"
    		Cols += 1
    	  End If
    	Next
    	Return result
      End Function
    End Class


    Regards Les, Livingston, Scotland

    Thursday, December 13, 2018 1:56 PM
  • Hi,

    fix your code:

      If result = 10 Then
                serial = (CLng(mainval) + 1).ToString + "0"
            Else
                serial = mainval + result
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    hi alex

    2-Why does not the error occur only at the number 10

    Hi,

    error:839
             mainval = bignum.Substring(0, 2)  83
             mainvalB = bignum.Remove(0, 2) 9
             result = CStr(CLng(mainvalB) + 1)10=9+1
             serial = mainval + result "8310"="83"+"10"

    right:839

              mainval = bignum.Substring(0, 2)  83

            mainvalB = bignum.Remove(0, 2) 9
            result = CStr(CLng(mainvalB) + 1)10=9+1
            serial = mainval + result  "840"="83+1"+"0"

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 14, 2018 1:47 AM