Private Sub AutoGenerateMemberID()
Dim ItemID As Integer
Dim s As String = "select MAX( Bill_ID ) FROM Billing "
Dim dr As OleDbDataReader
dr = ExecuteReader(s)
While dr.Read()
ItemID = CInt(dr(0))
ItemID = ItemID + ("BG" & (CInt("600182") + 1))
tb_billingNo.Text = CStr(ItemID)
End While
dr.Close()
End Sub
Private Sub tb_billingNo_TextChanged(sender As Object, e As EventArgs) Handles tb_billingNo.TextChanged
AutoGenerateMemberID()
End Sub
ตัวอย่างโค๊ดครับ แต่ขึ้น Error Conversion from string "BG600182" to type 'Integer' is not valid ครับ
Private Sub AutoGenerateMemberID()
Dim ItemID As String
Dim s As String = "select MAX( Bill_ID ) FROM Billing "
Dim dr As OleDbDataReader
dr = ExecuteReader(s)
While dr.Read()
ItemID = "BG" & (CInt(dr(0).ToString().Replace("BG", "")) + 1)
tb_billingNo.Text = CStr(ItemID)
End While
dr.Close()
End Sub
ขึ้นอันนี้แทนครับ Invalid attempt to call Read when reader is closed
Private Sub tb_billingNo_TextChanged(sender As Object, e As EventArgs) Handles tb_billingNo.TextChanged
AutoGenerateMemberID()
End Sub
Private Sub AutoGenerateMemberID()
Dim ItemID As String
Dim s As String = "select MAX( Bill_ID ) FROM Billing "
Dim dr As OleDbDataReader
dr = ExecuteReader(s)
While dr.Read()
ItemID = "BG" & (CInt(dr(0).ToString().Replace("BG", "")) + 1)
tb_billingNo.Text = CStr(ItemID)'ตัวนี้จะเรียก AutoGenerateMemberID() ทำให้วน loop ไม่รู้จบ
End While
dr.Close()
End Sub
Private Sub tb_billingNo_TextChanged(sender As Object, e As EventArgs) Handles tb_billingNo.TextChanged
Code (VB.NET)
Private Sub tb_billingNo_TextChanged(sender As Object, e As EventArgs) Handles tb_billingNo.TextChanged
RemoveHandler tb_billingNo.TextChanged, Address Of billingNo_TextChanged
AutoGenerateMemberID()
AddHandler tb_billingNo.TextChanged, Address Of billingNo_TextChanged
'ป้องกันการวนแบบที่เราต้องการให้รู้จบ (ไม่รู้จบเราุคุมไม่ได้)
End Sub
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show(ReturnID("AA6005-001"))
MessageBox.Show(ReturnID("AA6006-011"))
End Sub
Function ReturnID(strID As String) As String
'AA6006-001
Dim s As String = strID.Replace("AA", "") '6006-001
Dim ss() As String = s.Split("-") '6006,001
If ss(0).Substring(2, 2) <> Now.Month().ToString("00") Then
Return "AA" & Now.ToString("yyMM") & "-001"
Else
Return "AA" & ss(0) & (CInt(ss(1)) + 1).ToString("-000")
End If
End Function
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show(ReturnID("AA6005-001"))
MessageBox.Show(ReturnID("AA6006-011"))
End Sub
Private Sub AutoGenerateMemberID()
Dim ItemID As String
Dim s As String = "select MAX( ID_Payment) FROM Payment "
Dim dr As OleDbDataReader
dr = ExecuteReader(s)
While dr.Read()
ItemID = returnID(dr(0).ToString())
tb_payid.Text = ItemID
End While
dr.Close()
End Sub