Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have code here ...
VB
<pre>   If Not dr.HasRows Then
            penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF" + "0001"
        Else
            penfaktur.Text = Val(Microsoft.VisualBasic.Mid(dr.Item("no_faktur").ToString, 11, 4)) + 1
            If Len(penfaktur.Text) = 1 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF000" & penfaktur.Text & ""
            ElseIf Len(penfaktur.Text) = 2 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF00" & penfaktur.Text & ""
            ElseIf Len(penfaktur.Text) = 3 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF0" & penfaktur.Text & ""
            ElseIf Len(penfaktur.Text) = 4 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF" & penfaktur.Text & ""
            End If
        End If

I want every date change the number back to "0001" ..
help me..

ex :

01072017NF0099
01072017NF0100
02072017NF0001 <--- Change back to NF + 0001 here, since the date changed
02072017NF0002
03072017NF0001 <--- Change back to NF + 0001 here, since the date changed

What I have tried:

If Not dr.HasRows Then
            penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF" + "0001"
        Else
            penfaktur.Text = Val(Microsoft.VisualBasic.Mid(dr.Item("no_faktur").ToString, 11, 4)) + 1
            If Len(penfaktur.Text) = 1 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF000" & penfaktur.Text & ""
            ElseIf Len(penfaktur.Text) = 2 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF00" & penfaktur.Text & ""
            ElseIf Len(penfaktur.Text) = 3 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF0" & penfaktur.Text & ""
            ElseIf Len(penfaktur.Text) = 4 Then
                penfaktur.Text = "" + Format(Now, "ddMMyyyy") + "NF" & penfaktur.Text & ""
            End If
        End If
Posted
Updated 12-Jul-17 0:02am
Comments
Michael_Davies 12-Jul-17 6:08am    
Rather than testing the length with so many nested If's you could just;

penfaktur.Text = "" + Format(Now, "ddMMyyyy") + penfaktur.Text.PadLeft("0",4) & ""

Also you are using old BASIC function calls like Val and Mid when there are methods and functions on strings in .Net.

Have not tested this but just to show;

penfaktur.Text = (Integer.Parse(dr.Item("no_faktur").ToString.SubString(10, 4)) + 1).Tostring

Also you need to be certain that there was a number in the string or you will get an exception.
Khabibb Mubarakk 13-Jul-17 4:40am    
please give me a complete code

1 solution

Maintain the last assigned Datetime and compare its Date part with the one of the Datetime you are currently assigning. On mismatch restart the counter.
 
Share this answer
 
Comments
Khabibb Mubarakk 13-Jul-17 4:43am    
i know i want cek on database if date isn't same then back to 0001
but i'm still confuse how to implementation to code..because i'm really amateur on vb

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900