this post was actually a 'sub-post' to my bigger issue... maybe one of you
could shed some more light on this for me. It is ado.net related.
http://support.microsoft.com/default.aspx?scid=kb;en-us;815629&Product=adonet
the above link is the only answer I have so far. This is my response after
reading it:
I must say I am shocked at how complex a task this seems. It should be so
simple, and was so simple in Access. I can only assume the difference is due
to ado.net being 'disconnected'. What I need to do is so common: insert a
record in one table, store the ID that was just used, use that ID to create
related records in a different table.
I am very frustrated and disappointed. With all the power offered by
asp.net/ado.net why do I have to write pages and pages of code for what
should not even be one page worth?
-------and this is the original post with my issue/questions:
2 tables. The ID field in table1 is primary key in table1 and foriegn key in
table2. In older MS access apps I used something like the following for
handling the need to have the autonumber field automatically generated by
Access to create the next set of records in a related table.
-----------------------------------------------------------
With rst 'table one
.AddNew
.Fields("Name") = UCase(Trim(Me.txtImageName))
.Fields("Sysprepped") = Trim(Me.cboSysprepped)
.Fields("MachineType") = UCase(Trim(Me.txtMachineType))
.Fields("TechID") = UCase(Trim(Me.cboTechID))
.Fields("OSID") = Trim(Me.cboOSID)
.Fields("SPLevel") = Trim(Me.txtSPLevel)
.Fields("DateCreated") = Trim(Me.txtDateCreated)
.Fields("Notes") = Trim(Me.txtNotes)
.Update
ID = .Fields("ImageID") 'here is where I snag the auto ID
number field
.Close
End With
sql = "SELECT * FROM [tblImage-Software] WHERE
[tblImage-Software.ImageID] = 0"
rst.Open sql, options:=adCmdText
i = 0 'reinitialize
With rst 'table two
For i = 0 To lstInstalledSoftware.ListCount - 1 'clear selections
from source lst box
.AddNew
.Fields("ImageID") = ID 'Here is where I use the previously
snagged ID
.Fields("SoftwareID") = lstInstalledSoftware.ItemData(i)
.Update
Next i
.Close
End With
----------------------------------------------------------------------
This part:
.Update
ID = .Fields("ImageID")
.Close
is where I stored the autonumber id field from that record to use later in
this code to enter several records into a seperate related table.
1) can this be done with ado.net in the same way? is so, is it ok
(recommended) to do it this way?
1a) could someone give me a short example using an ado.net dataset?
2) is using the select @@identity query a better way?
I know ado.net is 'disconnected' so I don't know if the method shown in the
sample code will work anymore. I have also never used the @@identity method
and don't know if it is recommended/reliable since several users could be
adding records at the same time... how do you know you got yours? Aside from
really needing answers to 1) and 2) above, I am wondering if the same
applies to sql server identity columns?
any info is appreciated. Thanks.
------------------
any enlightement is greatly appreciated. Thanks.
MGFoster said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
The @@IDENTITY is a function in MS SQL Server, only. It is not
available in Access/JET dbs. If you are using a recordset to
save/update records you can use the .LastModified property of the
recordset. E.g.:
dim rs as dao.recordset
... assign rs & open ...
with rs
.addnew
' ... assign column values
.update
' Move current record pointer to the most recently
' changed or added record.
.Bookmark = .LastModified
debug.print !autonumber_column
end with
!autonumber_column is a reference to the AutoNumber column in the JET
table, it can have any name you want (as long as it conforms to the
Access naming conventions). AutoNumbers are, basically, the same thing
as an Identity column in a SQL server table.
For more info, read the Access VBA Help on LastModified and AutoNumbers.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBQTZPAoechKqOuFEgEQIXOACfQVF5e4arGD9k36KZrj+oqDuZ5IAAoO4X
3CUT0CP2mYWt594ezyrrNQS9
=9sRC
-----END PGP SIGNATURE-----