Bonjour,
J'ai une fonction de calcul que j'ai réalisée en parcourant un recordset. Pas de problème, cela fonctionne.
Ayant lu que les requêtes SQL sont plus rapides, j'essaie de créer une requête de mise à jour
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Public Function Calcul() Dim oDb As DAO.Database, oRst As DAO.Recordset, sSQL As String Set oDb = CurrentDb sSQL = "SELECT tAUT.*, tFOR.* FROM tFOR INNER JOIN tAUT ON tFOR.forid = tAUT.autforid;" Set oRst = oDb.OpenRecordset(sSQL, dbOpenDynaset) If oRst.BOF Then MsgBox "Il n'y a pas d'enregistrement à recalculer", vbExclamation + vbOKOnly, "Exécution impossible" Else While Not oRst.EOF oRst.Edit If IsNull(oRst.Fields("autfor").Value) Then oRst.Fields("autala").Value = Null oRst.Fields("autlim").Value = Null ElseIf Not IsNull(oRst.Fields("autsus").Value) Then oRst.Fields("autala").Value = Null oRst.Fields("autlim").Value = Null ElseIf (oRst.Fields("foratt").Value = True And IsNull(oRst.Fields("autaut").Value)) Then oRst.Fields("autala").Value = Null oRst.Fields("autlim").Value = Null ElseIf (oRst.Fields("fordur").Value > 0 And Date > DateSerial(Year(oRst.Fields("autfor").Value), Month(oRst.Fields("autfor").Value) + oRst.Fields("fordur").Value, Day(oRst.Fields("autfor").Value))) Then oRst.Fields("autala").Value = Null oRst.Fields("autlim").Value = Null ElseIf oRst.Fields("fordur").Value = 0 Then oRst.Fields("autala").Value = #12/31/2099# oRst.Fields("autlim").Value = #12/31/2099# Else oRst.Fields("autala").Value = DateSerial(Year(oRst.Fields("autfor").Value), Month(oRst.Fields("autfor").Value) + oRst.Fields("fordur").Value - 6, Day(oRst.Fields("autfor").Value)) oRst.Fields("autlim").Value = DateSerial(Year(oRst.Fields("autfor").Value), Month(oRst.Fields("autfor").Value) + oRst.Fields("fordur").Value, Day(oRst.Fields("autfor").Value)) End If oRst.Update oRst.MoveNext Wend End If MsgBox Combien(sSQL) & " enregistrements ont été recalculés", vbExclamation + vbOKOnly, "Recalcul" oRst.Close: oDb.Close: Set oRst = Nothing: Set oDb = Nothing If CurrentProject.AllForms("fAGEsfAUT").IsLoaded Then Forms!fAGEsfAUT.Requery End Function
Vous avez deviné ? La requête semble s'exécuter plus lentement, qui plus est les calculs sont erronés. Je l'ai pourtant déduite de la première fonction.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 Public Function Calcul_bis() Dim sAlarme As String, sLimite As String, sSQL As String sAlarme = "IIf(IsNull(tAUT.autfor) = True, Null, " & _ "IIf(IsNull(tAUT.autsus) = False, Null, " & _ "IIf(tFOR.foratt = True And IsNull(tAUT.autaut) = True, Null, " & _ "IIf(tFOR.fordur > 0 And Date() > DateSerial(Year(tAUT.autfor), Month(tAUT.autfor) + tFOR.fordur, Day(tAUT.autfor)), " & _ "IIf(tFOR.fordur = 0, #12/31/2099#, DateSerial(Year(tAUT.autfor), Month(tAUT.autfor) + tFOR.fordur - 6, Day(tAUT.autfor)))))))" sLimite = "IIf(IsNull(tAUT.autfor) = True, Null, " & _ "IIf(IsNull(tAUT.autsus) = False, Null, " & _ "IIf(tFOR.foratt = True And IsNull(tAUT.autaut) = True, Null, " & _ "IIf(tFOR.fordur > 0 And Date() > DateSerial(Year(tAUT.autfor), Month(tAUT.autfor) + tFOR.fordur, Day(tAUT.autfor)), " & _ "IIf(tFOR.fordur = 0, #12/31/2099#, DateSerial(Year(tAUT.autfor), Month(tAUT.autfor) + tFOR.fordur, Day(tAUT.autfor)))))))" sSQL = "UPDATE tFOR INNER JOIN tAUT ON tFOR.forid = tAUT.autforid SET tAUT.autala = " & sAlarme & ", tAUT.autlim = " & sLimite & ";" DoCmd.SetWarnings True DoCmd.RunSQL sSQL DoCmd.SetWarnings True If CurrentProject.AllForms("fAGEsfAUT").IsLoaded Then Forms!fAGEsfAUT.Requery End Function
Avez-vous une idée ?







Répondre avec citation




Partager