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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| Public Function GenericOpenRecordset(strSQL As String, _
Optional intType As DAO.RecordsetTypeEnum = dbOpenDynaset, _
Optional intOptions As DAO.RecordsetOptionEnum, _
Optional intLock As DAO.LockTypeEnum, _
Optional pdb As DAO.Database) As DAO.Recordset
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim prm As DAO.Parameter
Dim sExpr As String
If Not pdb Is Nothing Then
Set db = pdb
Else
Set db = CurrentDb
End If
On Error Resume Next
Set qdf = db.QueryDefs(strSQL)
If Err = 3265 Then
Set qdf = db.CreateQueryDef("", strSQL)
End If
For Each prm In qdf.Parameters
Err.Clear
sExpr = prm.Name
sExpr = Replace(sExpr, "[Formulaires]!", "Forms!")
sExpr = Replace(sExpr, "Formulaires!", "Forms!")
prm.Value = Eval(sExpr)
Select Case Err.Number
Case 2482, 2450
' 2482 = Paramètre non évaluable.
' 2450 = Impossible de trouver le formulaire auquel il est fait référence
' dans une expression de macro ou un code Visual Basic.
' Demande la saisie du paramètre dans une inputbox
prm.Value = InputBox(prm.Name)
End Select
Next
On Error GoTo 0
If intOptions = 0 And intLock = 0 Then
Set rst = qdf.OpenRecordset(intType)
ElseIf intOptions > 0 And intLock = 0 Then
Set rst = qdf.OpenRecordset(intType, intOptions)
ElseIf intOptions = 0 And intLock > 0 Then
Set rst = qdf.OpenRecordset(intType, intLock)
ElseIf intOptions > 0 And intLock > 0 Then
Set rst = qdf.OpenRecordset(intType, intOptions, intLock)
End If
Set GenericOpenRecordset = rst
Set prm = Nothing
Set rst = Nothing
Set qdf = Nothing
Set db = Nothing
End Function |
Partager