title: Getting Primary key values (auto number ) VB
body: <p>I have a database on <code>Access</code> and I want to insert into 2 tables </p>
  
  <ul>
  <li><strong>ReportReq</strong> </li>
  <li><strong>req_sysino</strong></li>
  </ul>
  
  <p>I want to get the last value of primary key (auto numbered) and insert it into <strong>req_sysino</strong>
  , I am stuck with this code and I dont know how to proccess</p>
  
  <pre><code>Private Function InsertSysInvToDB(intSysInv As Integer) As Integer
  
      Dim strSQLStatement As String = String.Empty
      Dim intNoAffectedRows As Integer = 0
      Dim con As New OleDb.OleDbConnection("PROVIDER = Microsoft.ace.OLEDB.12.0; Data Source = C:\Users\felmbanF\Documents\Visual Studio 2012\Projects\WebApplication3\WebApplication3\App_Data\ReportReq.accdb")
      Dim cmd As OleDb.OleDbCommand
      Dim reqnum As String = "Select  @@REQ_NUM from ReportReq"
      strSQLStatement = "INSERT INTO req_sysino (Req_num, sysinvo_ID)" +
          " VALUES (" &amp; reqnum &amp; "','" &amp; intSysInv &amp; ")"
  
      cmd = New OleDb.OleDbCommand(strSQLStatement, con)
  
      cmd.Connection.Open()
  
      intNoAffectedRows = cmd.ExecuteNonQuery()
      cmd.Connection.Close()
  
      Return intNoAffectedRows
  End Function
  </code></pre>
  
  <p>this is my insert code that should generate autonumber </p>
  
  <pre><code>   Dim dbProvider = "PROVIDER = Microsoft.ace.OLEDB.12.0;"
      Dim dbSource = " Data Source = C:\Users\felmbanF\Documents\Visual Studio 2012\Projects\WebApplication3\WebApplication3\App_Data\ReportReq.accdb"
  
      Dim sql = "INSERT INTO ReportReq (Emp_EmpID, Req_Date,Req_expecDate,Req_repnum, Req_name, Req_Descrip, Req_columns, Req_Filtes, Req_Prompts)" +
          "VALUES (@reqNUM,@reqName,@reqDescrip,@reqcolumns,@reqfilters,@reqprompts)"
  
      Using con = New OleDb.OleDbConnection(dbProvider &amp; dbSource)
          Using cmd = New OleDb.OleDbCommand(sql, con)
              con.Open()
              cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text)
              cmd.Parameters.AddWithValue("@reqDate", DateTime.Today)
              cmd.Parameters.AddWithValue("@reqExpecDate", DateTime.Parse(txtbxExpecDate.Text).ToShortDateString())
              cmd.Parameters.AddWithValue("@reqNUM", txtRep_NUM.Text)
              cmd.Parameters.AddWithValue("@reqName", txtRep_Name.Text)
              cmd.Parameters.AddWithValue("@reqDescrip", txtbxRep_Desc.Text)
              cmd.Parameters.AddWithValue("@reqcolumns", txtbxColReq.Text)
              cmd.Parameters.AddWithValue("@reqfilters", txtbxFilReq.Text)
              cmd.Parameters.AddWithValue("@reqprompts", txtbxPromReq.Text)
              cmd.ExecuteNonQuery()
          End Using
      End Using
  </code></pre>