Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Retreving an auto number field from Access

Retreving an auto number field from Access

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrGonzo
    wrote on last edited by
    #1

    Hi all.... I am working on a VB.net application at attaches to a database in disconnected mode. I am haveing trouble retreving an autonumber field. Here is the code as I have it now: Dim drw1 As DataRow = das3.Tables("tomdb").NewRow() drw1("Name") = TextBox3.Text drw1("function") = TextBox4.Text If RadioButton1.Checked Then drw1("accesslevel") = "1" ElseIf RadioButton2.Checked Then drw1("accesslevel") = "2" ElseIf RadioButton3.Checked Then drw1("accesslevel") = "3" Else drw1("accesslevel") = "3" End If drw1("UserID") = Integer.MaxValue das3.Tables("tomDB").Rows.Add(drw1) AddHandler dap3.RowUpdated, New OleDb.OleDbRowUpdatedEventHandler(AddressOf OnRowUpdated) -------------------------------------------------------------------------------------- Private Sub OnRowUpdated(ByVal sender As Object, ByVal args As OleDb.OleDbRowUpdatedEventArgs) Dim int1 As Integer = 0 Dim cmd1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT @@IDENTITY", cnn3) If args.StatementType = StatementType.Insert Then int1 = CInt(cmd1.ExecuteScalar()) args.Row("UserID") = int1 End If End Sub -------------------------------------------------------------------------------------- In my code it should retreve the next record number but all I get is a 0. Any ideas? Gonz

    B 1 Reply Last reply
    0
    • G GrGonzo

      Hi all.... I am working on a VB.net application at attaches to a database in disconnected mode. I am haveing trouble retreving an autonumber field. Here is the code as I have it now: Dim drw1 As DataRow = das3.Tables("tomdb").NewRow() drw1("Name") = TextBox3.Text drw1("function") = TextBox4.Text If RadioButton1.Checked Then drw1("accesslevel") = "1" ElseIf RadioButton2.Checked Then drw1("accesslevel") = "2" ElseIf RadioButton3.Checked Then drw1("accesslevel") = "3" Else drw1("accesslevel") = "3" End If drw1("UserID") = Integer.MaxValue das3.Tables("tomDB").Rows.Add(drw1) AddHandler dap3.RowUpdated, New OleDb.OleDbRowUpdatedEventHandler(AddressOf OnRowUpdated) -------------------------------------------------------------------------------------- Private Sub OnRowUpdated(ByVal sender As Object, ByVal args As OleDb.OleDbRowUpdatedEventArgs) Dim int1 As Integer = 0 Dim cmd1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT @@IDENTITY", cnn3) If args.StatementType = StatementType.Insert Then int1 = CInt(cmd1.ExecuteScalar()) args.Row("UserID") = int1 End If End Sub -------------------------------------------------------------------------------------- In my code it should retreve the next record number but all I get is a 0. Any ideas? Gonz

      B Offline
      B Offline
      Bo Hunter
      wrote on last edited by
      #2

      This is a text app I whiped up. The Button1_Click works and Button2_Click does not. You have to get the value before the connection is closed. Private m_connection As OleDb.OleDbConnection Private m_connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) MyBase.OnLoad( e ) m_connectionString &= HunterDev.Environment.GetUserFolderPath( SpecialFolders.Personal ) & "\db1.mdb" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If ( m_connection Is Nothing ) Then m_connection = New OleDb.OleDbConnection( m_connectionString ) End If Dim command As New OleDb.OleDbCommand( "INSERT INTO Sample ( Name ) VALUES ( '" & TextBox1.Text & "' )", m_connection ) Dim idCommand As New OleDb.OleDbCommand( "SELECT @@IDENTITY", m_connection ) If ( m_connection.State <> ConnectionState.Open ) Then m_connection.Open() End If command.ExecuteNonQuery() Dim id As Integer = CInt( idCommand.ExecuteScalar() ) If ( m_connection.State <> ConnectionState.Closed ) Then m_connection.Close() End If TextBox2.AppendText( id.ToString() & vbCrLf ) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim idCommand As New OleDb.OleDbCommand( "SELECT @@IDENTITY", m_connection ) If ( m_connection.State <> ConnectionState.Open ) Then m_connection.Open() End If Dim id As Integer = CInt( idCommand.ExecuteScalar() ) If ( m_connection.State <> ConnectionState.Closed ) Then m_connection.Close() End If TextBox2.AppendText( id.ToString() & vbCrLf ) End Sub Bo Hunter

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups