|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
loading variable with AutoNumber field value after insert?an Access Database "Customer.MDB" with a table "CustInfo" that has the following design: Field: DataType CustID AutoNumber Name Text Title Text Phone Text '========================================================= Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\customer.mdb;") Dim MyCommand As New OleDbCommand Dim ra As Integer Conn.open cmndSTR = "Insert into CustInfo ([Name], [Title],[Phone]) VALUES (NameTxt, TitleText, PhoneNum) MyCommand = New OleDbCommand(cmndSTR, conn) ra = MyCommand.ExecuteNonQuery() '============================================================ My question is this: Once this code has been executed and a record has been added, what is an easy way for me to load a variable (call it "intCustID") with the CustID value (the AutoNumber field) of the record that was just added? A code snippet would be GREATLY appreciated... Jmar
Show quote
Hide quote
"jmar" <jmarz***@yahoo.com> wrote in message Try searching the web with "ADO.Net + get identity column" (without the news:1168787283.160528.121140@11g2000cwr.googlegroups.com... > I'm hoping someone out there can give me a little guidance. I have > an Access Database "Customer.MDB" with a table "CustInfo" that > has the following design: > > Field: DataType > CustID AutoNumber > Name Text > Title Text > Phone Text > > '========================================================= > Dim conn As New > OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data > Source=c:\customer.mdb;") > Dim MyCommand As New OleDbCommand > Dim ra As Integer > > Conn.open > cmndSTR = "Insert into CustInfo ([Name], [Title],[Phone]) VALUES > (NameTxt, TitleText, PhoneNum) > MyCommand = New OleDbCommand(cmndSTR, conn) > ra = MyCommand.ExecuteNonQuery() > '============================================================ > > > My question is this: Once this code has been executed and a record has > been added, what is an easy way for me to load a variable (call it > "intCustID") with the CustID value (the AutoNumber field) of the > record that was just added? > > A code snippet would be GREATLY appreciated... > > Jmar > quotes). You will gets lots of help. I appreciate your response, but I'm wondering if there is a more
specific way to do it considering the method I am using to add the record to the table When I search the web, I can find several ways, but I'm looking to see if there is some way that I can do it within the context of my code (perhaps a Select command or something?). Any ideas out there? Thanks, Jmar Harry Strybos wrote: Show quoteHide quote > "jmar" <jmarz***@yahoo.com> wrote in message > news:1168787283.160528.121140@11g2000cwr.googlegroups.com... > > I'm hoping someone out there can give me a little guidance. I have > > an Access Database "Customer.MDB" with a table "CustInfo" that > > has the following design: > > > > Field: DataType > > CustID AutoNumber > > Name Text > > Title Text > > Phone Text > > > > '========================================================= > > Dim conn As New > > OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data > > Source=c:\customer.mdb;") > > Dim MyCommand As New OleDbCommand > > Dim ra As Integer > > > > Conn.open > > cmndSTR = "Insert into CustInfo ([Name], [Title],[Phone]) VALUES > > (NameTxt, TitleText, PhoneNum) > > MyCommand = New OleDbCommand(cmndSTR, conn) > > ra = MyCommand.ExecuteNonQuery() > > '============================================================ > > > > > > My question is this: Once this code has been executed and a record has > > been added, what is an easy way for me to load a variable (call it > > "intCustID") with the CustID value (the AutoNumber field) of the > > record that was just added? > > > > A code snippet would be GREATLY appreciated... > > > > Jmar > > > Try searching the web with "ADO.Net + get identity column" (without the > quotes). You will gets lots of help. ' Begin a transaction here
cmndSTR = "Insert into CustInfo ([Name], [Title],[Phone]) VALUES (NameTxt, TitleText, PhoneNum) MyCommand = New OleDbCommand(cmndSTR, conn) ra = MyCommand.ExecuteNonQuery() MyCommand.CommandText = "select max(CustID) from CustInfo" intCustID = MyCommand.ExecuteScalar() ' Commit the transaction here Show quoteHide quote "jmar" <jmarz***@yahoo.com> wrote in message news:1168832858.474638.19330@s34g2000cwa.googlegroups.com... >I appreciate your response, but I'm wondering if there is a more > specific way to do it considering the method I am using to add the > record to the table > > When I search the web, I can find several ways, but I'm looking to see > if there is some way that I can do it within the context of my code > (perhaps a Select command or something?). Any ideas out there? > > Thanks, > > Jmar > > > Harry Strybos wrote: >> "jmar" <jmarz***@yahoo.com> wrote in message >> news:1168787283.160528.121140@11g2000cwr.googlegroups.com... >> > I'm hoping someone out there can give me a little guidance. I have >> > an Access Database "Customer.MDB" with a table "CustInfo" that >> > has the following design: >> > >> > Field: DataType >> > CustID AutoNumber >> > Name Text >> > Title Text >> > Phone Text >> > >> > '========================================================= >> > Dim conn As New >> > OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data >> > Source=c:\customer.mdb;") >> > Dim MyCommand As New OleDbCommand >> > Dim ra As Integer >> > >> > Conn.open >> > cmndSTR = "Insert into CustInfo ([Name], [Title],[Phone]) VALUES >> > (NameTxt, TitleText, PhoneNum) >> > MyCommand = New OleDbCommand(cmndSTR, conn) >> > ra = MyCommand.ExecuteNonQuery() >> > '============================================================ >> > >> > >> > My question is this: Once this code has been executed and a record has >> > been added, what is an easy way for me to load a variable (call it >> > "intCustID") with the CustID value (the AutoNumber field) of the >> > record that was just added? >> > >> > A code snippet would be GREATLY appreciated... >> > >> > Jmar >> > >> Try searching the web with "ADO.Net + get identity column" (without the >> quotes). You will gets lots of help. > SELECT Max(CustID) FROM CustInfo
If you were using SQL Server, you could use a stored procedure that did teh insert and then returns the record inserted. jmar wrote: Show quoteHide quote > I appreciate your response, but I'm wondering if there is a more > specific way to do it considering the method I am using to add the > record to the table > > When I search the web, I can find several ways, but I'm looking to see > if there is some way that I can do it within the context of my code > (perhaps a Select command or something?). Any ideas out there? > > Thanks, > > Jmar > > > Harry Strybos wrote: > > "jmar" <jmarz***@yahoo.com> wrote in message > > news:1168787283.160528.121140@11g2000cwr.googlegroups.com... > > > I'm hoping someone out there can give me a little guidance. I have > > > an Access Database "Customer.MDB" with a table "CustInfo" that > > > has the following design: > > > > > > Field: DataType > > > CustID AutoNumber > > > Name Text > > > Title Text > > > Phone Text > > > > > > '========================================================= > > > Dim conn As New > > > OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data > > > Source=c:\customer.mdb;") > > > Dim MyCommand As New OleDbCommand > > > Dim ra As Integer > > > > > > Conn.open > > > cmndSTR = "Insert into CustInfo ([Name], [Title],[Phone]) VALUES > > > (NameTxt, TitleText, PhoneNum) > > > MyCommand = New OleDbCommand(cmndSTR, conn) > > > ra = MyCommand.ExecuteNonQuery() > > > '============================================================ > > > > > > > > > My question is this: Once this code has been executed and a record has > > > been added, what is an easy way for me to load a variable (call it > > > "intCustID") with the CustID value (the AutoNumber field) of the > > > record that was just added? > > > > > > A code snippet would be GREATLY appreciated... > > > > > > Jmar > > > > > Try searching the web with "ADO.Net + get identity column" (without the > > quotes). You will gets lots of help.
Show quote
Hide quote
On 14 Jan 2007 07:08:03 -0800, "jmar" <jmarz***@yahoo.com> wrote: If using VB2005, search the help index for 'auto-incremented values'. See the>I'm hoping someone out there can give me a little guidance. I have >an Access Database "Customer.MDB" with a table "CustInfo" that >has the following design: > >Field: DataType >CustID AutoNumber >Name Text >Title Text >Phone Text > >'========================================================= >Dim conn As New >OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data >Source=c:\customer.mdb;") >Dim MyCommand As New OleDbCommand >Dim ra As Integer > >Conn.open >cmndSTR = "Insert into CustInfo ([Name], [Title],[Phone]) VALUES >(NameTxt, TitleText, PhoneNum) >MyCommand = New OleDbCommand(cmndSTR, conn) >ra = MyCommand.ExecuteNonQuery() >'============================================================ > > >My question is this: Once this code has been executed and a record has >been added, what is an easy way for me to load a variable (call it >"intCustID") with the CustID value (the AutoNumber field) of the >record that was just added? > >A code snippet would be GREATLY appreciated... > >Jmar section, "Retriving Microsoft Access Autonumber Values". Gene
Network Communications
Menu - Eliminate The Triangular Arror pointer thing - How to do it? Comparing objects Is WSE 3.0 in VS2005(VB) required to migrate from VS2003? Programmatically modifying the controls in FormView's PagerTemplate Passing parameter to windows service... Logical Mistake Hide property at runtime. DataGridView ComboBox lookup question how to read image from database and store in file |
|||||||||||||||||||||||