  | 
              
	              
	                
  
    |   | 
   
  
    
        
        ใช้ vb.net เมื่อกด save 1 ครั้ง ให้บันทึกลงฐานข้อมูล SQL โดยเริ่มต้นให้ label เริ่มจาก A001-A999 แล้วให้เปลี่ยนเป็น B001-B999 ไปเรื่อยๆครับ     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แก้ไขครับ ตัวเลขทั้งหมดมีแค่ 4 หลักครับ สูงสุด 9999 ถ้าเกินเมื่อไหร่ให้เริ่ม 0001                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-05-11 15:00:41 | 
                        By :
                            sakkapong | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 (อะไรก็ได้).ToString("A0000")                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-05-11 15:29:38 | 
                        By :
                            lamaka.tor | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                                          
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-05-11 17:03:35 | 
                        By :
                            sakkapong | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ก็น่าจะ select ข้อมูลมา 
แล้วเช็คเลยท้ายถ้าเป็น 9999 ก็เพิ่มตัวหนังสือครับ 
แล้วก็ reset เป็น 1 
 
 
select top 1 * from idone order by id desc 
ก็จะได้ row ล่าสุดมา 
แล้วเอาเข้า last_id 
last_id= สมมุติเป็น C9999 
last_int=mid(last_id,2,4) 'ตัวเลข 
last_head=mid(last_id,1,1) ' ตัวหนังสือ 
new_id="" 
 
if cint(last_int)=9999 then 
   last_head = ทำยังไงไม่รู้ให้ C กลายเป็น D 
    new_id= last_head + "0001" 
else 
  new_id=last_head + ("xxxx"+1) 
end if                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2017-05-11 17:15:53              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-05-11 17:14:47 | 
                        By :
                            fonfire | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองเขียนให้ 
ได้ประมาณนี้ครับ 
ยังไงเช็คอีกทีเพราะผมก็ไม่ค่อยแม่น 
 
Code (VB.NET) 
    Private Function AddNumber(_No As String) As String
        Dim _Head, _Digit As String
        Dim _Ch As String
        _Ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        _Head = Mid(_No, 1, 1)
        _Digit = Mid(_No, 2)
        If CInt(_Digit) >= 9999 Then
            _Digit = 1
            _Head = Mid(_Ch, InStr(_Ch, _Head) + 1, 1)
            If _Head.Trim = "" Then _Head = "A"
        Else
        _Digit = CInt(_Digit) + 1
        End If
        _Digit = CInt(_Digit).ToString("0000")
        Return _Head + _Digit
    End Function
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-05-12 08:32:40 | 
                        By :
                            fonfire | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ส่งค่าแบบนี้น่ะครับ 
 
MsgBox(AddNumber("C9999"))                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-05-12 08:37:52 | 
                        By :
                            fonfire | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (VB.NET) 
Function GetID(strID As String) As String
        Dim myMatch As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Matches(strID, "([A-Z]{1,})(\d{1,})")(0)
        Dim chr As String
        If Convert.ToInt32(myMatch.Groups(2).Value) + 1 >= 10000 Then
            chr = Convert.ToChar(Convert.ToByte(CChar("A")) + 1) 'มากกว่า 9999 เปลี่ยนตัวอักษร
        ElseIf String.IsNullOrEmpty(strID) Then
            chr = "A" 'เป็นค่าว่าง ให้เป็น A
        Else
            chr = myMatch.Groups(1).Value 'ปกติให้เป็นค่าเดิม
        End If
        Return chr _
               &
               CInt(IIf(Convert.ToInt32(myMatch.Groups(2).Value) + 1 >= 10000, 1, Convert.ToInt32(myMatch.Groups(2).Value) + 1)).ToString("0000")
        'มากกว่า 9999 ให้เป็น 1 ถ้าปกติให้ +1 จัดรูปแบบโดย ToString("0000")
    End Function
 
 
ใช้งาน 
 
Code (VB.NET) 
MessageBox.Show(GetID("A9999"))
 
 
ปล. อธิบาย (อะไรก็ได้).ToString("A0000") เป็นการเปลี่ยน object ให้เป็น string พร้อมกับจัดรูปแบบให้เป็น "A0000" ครับ 
https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx 
https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-05-12 10:15:18 | 
                        By :
                            lamaka.tor | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |