views:

277

answers:

2

I have this code:

With shtControleblad
    Dim strsql_basis As String
        strsql_basis = "INSERT INTO is_calculatie (offerte_id) VALUES ('" & Sheets("controleblad").Range("D1").Value & "')"

        rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic

        Dim last_id As String
        last_id = "select last_insert_id()"
End With

The string last_id is not filled. What is wrong? I need to find te last_insert_id so I can use it in an other query.

A: 

you have to add rs.movelast after you open the recordset, that should help

Marga Keuvelaar
How would that apply to my situation? And what about when more people are using the db?
Muiter
What is it you're really trying to do? You'd like to select the last inserted id, based on an insert statement? Maybe you should run a select statement first, then move to the last record, read from it, and then with that data, do the insert statement.
Marga Keuvelaar
+1  A: 
last_id = "select last_insert_id()"

You have set the sql statement to be executed, but have not executed it.
Call rs.Open with the above statement to get the 'last_insert_id` instead.

If mysql supports multiple sql statements on single line, you could do

strsql_basis = "INSERT INTO is_calculatie (offerte_id)  
VALUES ('" & Sheets("controleblad").Range("D1").Value & "')
; select last_insert_id()"

rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic
shahkalpesh
It does not support multiple statements.I have tried Dim last_id As String last_id = "select last_insert_id()" rs.Open last_id, oConn, adOpenDynamic, adLockOptimisticBut not the result wanted.
Muiter