Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

ADO Recordset Updates ?

I have an ADO recordset, and here is how I set it up:
Dim oConnection As New ADODB.Connection
Dim oRecordset As New ADODB.Recordset
sConnectString = "DSN=Quickbooks Data;OLE DB Services=-2;"
oConnection.Open(sConnectString)
oRecordset.Open(sSQL, oConnection, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)

oRecordset.AddNew()
Orecordset.Fields("Name").value = "xxxxx"
Orecordset.Update

Now here I would like to get the value of the key that is created

x = oRecordSet.Field("ListID").value

But oRecordSet.Field("ListID").value returns Nothing.
If I break here and I query it I will find that the ListID does have a value.

How can i have this oRecordset show me the value of ListID ?
0
Jess31
Asked:
Jess31
  • 2
1 Solution
 
ZhaolaiCommented:
Try this:
Change tblName to your table name.

        Dim strSQL As String = "SET NOCOUNT ON INSERT INTO tblName(Name) VALUES('xxxxx') SELECT @@IDENTITY AS NewID SET NOCOUNT OFF"
        oConnection.Open(sConnectString)
        oRecordset = oConnection.Execute(strSQL)
        Dim varNewID As Integer = oRecordset("NewID").Value
        oConnection.Close()

Open in new window

0
 
Jess31Author Commented:
Zhaolai
When it hits the Execute(strSQL) I get this error

[QODBC] Expected lexical element not found: CREATE, DELETE, DROP, INSERT, SELECT, SP_BATCHCLEAR, SP_BATCHSTART, SP_BATCHUPDATE, SP_CATEGORIES, SP_CATEGORYTABLES, SP_CATEGORYREPORTS, SP_CLEARLASTINSERTID, SP_COLUMNS, SP_FOREIGNKEYS, SP_FQSAVETOCACHEROLLBACK, SP_LASTINSERTID, SP_LASTINSERTIDRETURN, SP_OPTIMIZEFULLSYNC, SP_OPTIMIZEUPDATESYNC, SP_PARAMETERS, SP_PRIMARYKEYS, SP_QBFILENAME, SP_REPORT, SP_REPORTCOLUMNS, SP_REPORTS, SP_SPECIALCOLUMNS, SP_STATISTICS, SP_TABLES, SP_VOID or UPDATE
0
 
joriszwaenepoelCommented:
Have a look at the following article:

HOW TO: Retrieve the Identity Value While Inserting Records into Access Database By Using Visual Basic .NET
http://support.microsoft.com/kb/815629

0
 
Jess31Author Commented:
These all seem quite laborious since in MS Access when connecting to SQL Server I was able to just read the key right after the recordset.Update. Maybe this has something do with with this driver/database which is different... I do know that this key is not a numeric. But it for sure does get produced as soon as I do the .Update cause I am able to retrieve it by calling another function that will query that record, but this is slow and I'm doing many records so I would like to avoid that.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now