2014-12-22 2 views
1

자 동 번호에서 사용자 지정 자동 번호를 얻으려고합니다. 그러나 데이터베이스 테이블에 데이터가 있으면 원활하게 실행되며 데이터베이스 테이블에 데이터가 없으면 오류가 발생합니다.맞춤형 자판 번호를 얻는 방법은 무엇입니까?

Private Sub CustomNo() 
    Dim comm As OleDbCommand 
    Dim commStr As String = "SELECT MAX(ID) FROM Table" 
    Dim RD As OleDbDataReader 
    Dim i As Integer 

    conn = New OleDbConnection(connStr) 
    conn.Open() 

    comm = New OleDbCommand(commStr, conn) 
    RD = comm.ExecuteReader 

    While RD.Read 
     If Not IsDBNull(RD.GetInt32(0)) = False Then 
      i = RD.GetInt32(0) 
      CustN = "ABC-" & i + 1 
      Custom_NoTextBox.Text = CustN 
      Exit While 
     Else 

      i = 0 
      CustN = "ABC-" & i + 1 
      Custom_NoTextBox.Text = CustN 

     End If 
    End While 

    conn.Close() 
End Sub 
+0

좋아. 나는 그것을 직접 풀었다. –

답변

0
Private Sub CustomNo() 

    Dim CountCom As OleDbCommand 
    Dim CountComStr As String = "SELECT COUNT(*) FROM Table" 
    Dim j As Integer 
    Dim i As Integer 

    conn = New OleDbConnection(connStr) 
    conn.Open() 

    CountCom = New OleDbCommand(CountComStr, conn) 
    j = CountCom.ExecuteScalar() 

    If j = 0 Then 
     i = 1 
    Else 
     Dim comm As OleDbCommand 
     Dim commStr As String = "SELECT MAX (ID) FROM Table" 
     Dim RD As OleDbDataReader 

     comm = New OleDbCommand(commStr, conn) 
     RD = comm.ExecuteReader 
     While RD.Read 
      i = RD.GetInt32(0) 
     End While 
     i = i + 1 
    End If 

    Custom_NoTextBox.Text = "ABC-" & i 

    conn.Close() 
End Sub