TheSQLGuru
12/2/2007 1:43:00 AM
Recalling that I am NOT an ADO guru - despite having a copy of Vaughn's
book - I tried to get this to work. First I found that in ADO 2.8 there is
no ExecuteScalar (at least not that I found). So I tried it using a
recordset, with the following code:
Private Sub Command1_Click()
'here is the table def I used
'use northwind
'go
'create table c (c int identity, b char(1))
' connection, command, and recordset variables
Dim Cnxn As Connection
Dim cmdChange As Command
Dim rs As Recordset
' Open connection
Set Cnxn = New Connection
Cnxn.Open "Provider='sqloledb';Data Source='(local)';Initial
Catalog='Northwind';Integrated Security='SSPI';"
' Create command object
Set cmdChange = New Command
Set cmdChange.ActiveConnection = Cnxn
cmdChange.CommandText = "insert c (b) values ('A'); select
scope_identity() as a"
Set rs = cmdChange.Execute()
rs.MoveFirst
Do While Not rs.EOF
id = rs.Fields(0)
Loop
Cnxn.Close
Set rstTitles = Nothing
Set Cnxn = Nothing
Exit Sub
End Sub
I get the following message when I step on rs.MoveFirst - Operation is not
allowed when the object is closed.
Perhaps it can be done using a parameter with the command execute?
--
Kevin G. Boles
TheSQLGuru
Indicium Resources, Inc.
"William Vaughn" <billvaNoSPAM@betav.com> wrote in message
news:60D10EA7-B3DC-4799-B203-DBD9D6010E5D@microsoft.com...
> The approach in VB6 is virtually identical.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant, Dad, Grandpa
> Microsoft MVP
> INETA Speaker
> www.betav.com
> www.betav.com/blog/billva
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> __________________________________
> Visit www.hitchhikerguides.net to get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> -----------------------------------------------------------------------------------------------------------------------
>
> "Kerry Moorman" <KerryMoorman@discussions.microsoft.com> wrote in message
> news:166C19B7-E834-4810-AD56-7B6DB65E0FDC@microsoft.com...
>> Kevin,
>>
>> My example was using ADO.Net.
>>
>> Kerry Moorman
>>
>>
>> "TheSQLGuru" wrote:
>>
>>> I just checked back with the developer that had the issue. He swears
>>> that
>>> using VB6 and ADO classic your example fails. Were you using ADOc or
>>> ADO.NET?
>>>
>>> --
>>> Kevin G. Boles
>>> TheSQLGuru
>>> Indicium Resources, Inc.
>>>
>>>
>>> "Kerry Moorman" <KerryMoorman@discussions.microsoft.com> wrote in
>>> message
>>> news:6AD6FF28-692A-4DFA-9BCB-7D8E9A2302C1@microsoft.com...
>>> > Kevin,
>>> >
>>> > ExecuteScalar returns the first column of the first row in the result
>>> > set
>>> > returned by the query.
>>> >
>>> > Kerry Moorman
>>> >
>>> >
>>> > "TheSQLGuru" wrote:
>>> >
>>> >>
>>> >> One additional question since I am not an ADO guru. Does the Select
>>> >> Scope_identity() not return a single-column single-row result set,
>>> >> which
>>> >> the
>>> >> executescalar isn't expecting?
>>> >>
>>> >> --
>>> >> Kevin G. Boles
>>> >> TheSQLGuru
>>> >> Indicium Resources, Inc.
>>> >>
>>> >
>>>
>>>
>>>
>