@Villeroy,
I took a look at the other filters of the various forms and indeed the possibilities of filtering via SQL are rather impressive. I’m sure it will come in handy eventually.
.
Even though the filtering system is probably faster with SQL rather than using macros, I managed to solve my problem and create a macro that acts as a sort of “jump to” function. As expected, the macro is executed via a drop-down list or the user selects a letter and the procedure finds the first recurrence matching that letter and accesses it.
.
Here is the basic code:
.
Sub SetNameID(oEvent As Object)
Dim oForm As Object, oResultSet As Object, oModel As Object
Dim sSQL As String, sFormName As String, sLetter As String
Dim aNoms() As String ' Création d'un tableau pour stocker les noms des acteurs
Dim aIDs() As Integer ' Création d'un tableau pour stocker les IDs des acteurs
Dim i As Integer, j As Integer, iID As Integer
oModel = oEvent.Source.Model
oForm = oModel.Parent ' Cueillette du formulaire
sFormName = oForm.Name ' Cueillette du nom du formulaire
sLetter = oEvent.Source.SelectedItem ' Cueillette de la lettre sélectionnée dans la liste déroulante
' Construction de la requête SQL pour extraire les données de la table selon le formulaire
If sFormName = "frm-FilmsSeries" Then
sSQL = "SELECT ""TitreAnglais1"", ""FilmSerieID"" FROM ""TFilmsSeries"" ORDER BY ""TitreAnglais1"" ASC"
ElseIf sFormName = "frm-Acteurs" Then
sSQL = "SELECT ""NomActeur1"", ""ActeurID"" FROM ""TActeurs"" ORDER BY ""NomActeur1"" ASC"
ElseIf sFormName = "frm-Actrices" Then
sSQL = "SELECT ""NomActrice1"", ""ActriceID"" FROM ""TActrices"" ORDER BY ""NomActrice1"" ASC"
End If
oResultSet = oForm.ActiveConnection.createStatement().executeQuery(sSQL) ' Exécutio de la requête SQL
' Itération sur les enregistrements résultants
i = 0
Do While oResultSet.next()
ReDim Preserve aNoms(i) ' Redimensionner le tableau et ajoute le nom
ReDim Preserve aIDs(i) ' Redimensionner le tableau et ajoute l'ID
aNoms(i) = oResultSet.getString(1) ' Champ correspondant aux "Noms"
aIDs(i) = oResultSet.getInt(2) ' Champ correspondant Aux "IDs"
i = i + 1
Loop
oResultSet.close() ' Fermeture du résultat de la requête
' Boucle permettant de rechercher un nom qui commence par la lettre sélectionnée (sLetter) dans le tableau (aNoms).
' Lorsque le nom est trouvé, l'ID correspondant est stocké dans iID, et la boucle est interrompue.
For j = LBound(aNoms) To UBound(aNoms)
If Left(aNoms(j), 1) = sLetter Then
iID = aIDs(j)
Exit For
End If
Next j
' Si aucun nom ne correspond à la lettre sélectionnée, iID reste à 0, aucun nom ne commence par cette lettre.
If iID = 0 Then
Beep
MsgBox("Aucun nom dans le formulaire ne commence par la lettre " & sLetter & ".", 48, "Sélection")
Exit Sub
End If
FindRecord(oForm, iID) ' Permet d'afficher un enregistrement selon une recherche effectuée.
End Sub
.
This macro is used in others procedures.
Sub FindRecord(oForm As Object, iTarget As Integer)
Dim iRowNumber As Integer
Dim oColumn As Object, oResultSet As Object
Dim sFormName As String
sFormName = oForm.Name
If sFormName = "frm-Acteurs" Then
oResultSet = oForm.createResultSet
oColumn = oResultSet.columns.getByName("ActeurID")
ElseIf sFormName = "frm-Actrices" Then
oResultSet = oForm.createResultSet
oColumn = oResultSet.columns.getByName("ActriceID")
Else
oResultSet = oForm.createResultSet
oColumn = oResultSet.columns.getByName("FilmSerieID")
End If
oResultSet.Absolute(1)
Do
If oColumn.getInt = iTarget Then
iRowNumber = oResultSet.Row
Exit Do
End If
oResultSet.Next
Loop
oForm.Reload()
oForm.Absolute(iRowNumber)
End Sub
.
It may not be the most elegant of macros, but it works perfectly.
.
Thank you for giving me your time, it’s always appreciated.