VBA Last insert id gebruiken in een sql statement

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
In onderstaande code (uit Excel/VBA) worden 2 querys uitgevoerd. In de eerste wordt serverside een id gegeneerd. Deze id wil ik in de 2e query gebruiken. Hoe moet ik dat aanpakken?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    Set wsCtcts = Worksheets("controleblad")
    Set rs = New ADODB.Recordset
    
    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
    End With
    
    Set wsCtcts = Worksheets("materialen")
    Set rs = New ADODB.Recordset
    
    With shtMaterialen
        Dim row_pl As Integer
        Dim strsql_pl As String
        row_pl = 2
        While Trim(.Cells(row_pl, 2)) <> ""
            strsql_pl = "INSERT INTO is_platen (cnc, lengte_genest, lengte_nodig, breedte_genest, breedte_nodig, dikte, materiaal, aantal, snijtijd, machine) " & _
            "VALUES ('" & esc(Trim(.Cells(row_pl, 1).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 2).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 3).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 4).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 5).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 6).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 7).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 8).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 9).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 10).Value)) & "')"

            rs.Open strsql_pl, oConn, adOpenDynamic, adLockOptimistic
            row_pl = row_pl + 1
        Wend
    End With


Ik heb het zonder succes geprobeerd met:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    Set wsCtcts = Worksheets("controleblad")
    Set rs = New ADODB.Recordset
    
    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
    
    Set wsCtcts = Worksheets("materialen")
    Set rs = New ADODB.Recordset
    
    With shtMaterialen
        Dim row_pl As Integer
        Dim strsql_pl As String
        row_pl = 2
        While Trim(.Cells(row_pl, 2)) <> ""
            strsql_pl = "INSERT INTO is_platen (id_calculatie, cnc, lengte_genest, lengte_nodig, breedte_genest, breedte_nodig, dikte, materiaal, aantal, snijtijd, machine) " & _
            "VALUES ('" & last_id & "','" & _
            esc(Trim(.Cells(row_pl, 1).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 2).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 3).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 4).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 5).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 6).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 7).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 8).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 9).Value)) & "', '" & _
            esc(Trim(.Cells(row_pl, 10).Value)) & "')"

            rs.Open strsql_pl, oConn, adOpenDynamic, adLockOptimistic
            row_pl = row_pl + 1
        Wend
    End With


Het gaat fout bij last_id = "select last_insert_id()"
Deze wordt niet gevuld, als ik hier zelf een waarde invoeg werkt de rest wel naar behoren.

Acties:
  • 0 Henk 'm!

  • pedorus
  • Registratie: Januari 2008
  • Niet online
Met deze syntax gaat dat bij mijn weten niet lukken. Waarom gebruik je niet AddNew() en Update() op een table recordset? Dan kun je na afloop de Id gewoon opvragen, en waarschijnlijk gaat dit nog sneller ook, en je hoeft niet moeilijk te doen met escapen/quoten. :p

Vitamine D tekorten in Nederland | Dodelijk coronaforum gesloten