Aléger du code

cs_jujujulien Messages postés 18 Date d'inscription mardi 6 février 2007 Statut Membre Dernière intervention 19 mai 2007 - 13 mai 2007 à 16:21
cs_jujujulien Messages postés 18 Date d'inscription mardi 6 février 2007 Statut Membre Dernière intervention 19 mai 2007 - 14 mai 2007 à 23:55
Voila j'ai copier le code d'un autre programme, qui sert a lire les enregistrement d'un champ, et je doit utiliser ce code quatre fois mais je ne comprend pas entièrement le code, malgrés tout je l'ai modifié mais cela reste très grossier. Je me demandais quelqu'un pouvait d'alléger mon code. Je remercie d'avance toute réponse éventuel.

@+ un débutant

Voici le code en question :

Dim oADOConstructeur   As ADODB.Connection
Dim oRSConstructeur    As ADODB.Recordset
Dim strSQLConstructeur As String
Set oADOConstructeur = New ADODB.Connection
With oADOConstructeur
.Provider = "Microsoft.jet.oledb.4.0"
.Mode = adModeShareDenyNone
.ConnectionString = ".\MagPc.mdb"
.Open
End With
    Set oRSConstructeur = New ADODB.Recordset
    strSQLConstructeur = "SELECT fldConstructeur FROM tblCarteMere ORDER BY fldConstructeur"
    With oRSConstructeur
        .CursorLocation = adUseClient
        .Open strSQLConstructeur, oADOConstructeur, adOpenForwardOnly, adLockOptimistic
        .Properties.Refresh
        .MoveLast
        .MoveFirst
        Do While Not .EOF
            ListConstructeur.AddItem .Fields("fldConstructeur").Value
           .MoveNext
        Loop
        .Close
    End With
    oADOConstructeur.Close
    Set oRSConstructeur = Nothing
    Set oADOConstructeur = Nothing


Dim oADOSocket   As ADODB.Connection
Dim oRSSocket    As ADODB.Recordset
Dim strSQLSocket As String
Set oADOSocket = New ADODB.Connection
With oADOSocket
.Provider = "Microsoft.jet.oledb.4.0"
.Mode = adModeShareDenyNone
.ConnectionString = ".\MagPc.mdb"
.Open
End With
    Set oRSSocket = New ADODB.Recordset
    strSQLSocket = "SELECT fldSocket FROM tblCarteMere ORDER BY fldSocket"
    With oRSSocket
        .CursorLocation = adUseClient
        .Open strSQLSocket, oADOSocket, adOpenForwardOnly, adLockOptimistic
        .Properties.Refresh
        .MoveLast
        .MoveFirst
        Do While Not .EOF
            ListSocket.AddItem .Fields("fldSocket").Value
           .MoveNext
        Loop
        .Close
    End With
    oADOSocket.Close
    Set oRSSocket = Nothing
    Set oADOSocket = Nothing


Dim oADOFormatRam   As ADODB.Connection
Dim oRSFormatRam    As ADODB.Recordset
Dim strSQLFormatRam As String
Set oADOFormatRam = New ADODB.Connection
With oADOFormatRam
.Provider = "Microsoft.jet.oledb.4.0"
.Mode = adModeShareDenyNone
.ConnectionString = ".\MagPc.mdb"
.Open
End With
    Set oRSFormatRam = New ADODB.Recordset
    strSQLFormatRam = "SELECT fldFormatRam FROM tblCarteMere ORDER BY fldFormatRam"
    With oRSFormatRam
        .CursorLocation = adUseClient
        .Open strSQLFormatRam, oADOFormatRam, adOpenForwardOnly, adLockOptimistic
        .Properties.Refresh
        .MoveLast
        .MoveFirst
        Do While Not .EOF
            ListFormatRam.AddItem .Fields("fldFormatRam").Value
           .MoveNext
        Loop
        .Close
    End With
    oADOFormatRam.Close
    Set oRSFormatRam = Nothing
    Set oADOFormatRam = Nothing


Dim oADOPortGraphique   As ADODB.Connection
Dim oRSPortGraphique    As ADODB.Recordset
Dim strSQLPortGraphique As String
Set oADOPortGraphique = New ADODB.Connection
With oADOPortGraphique
.Provider = "Microsoft.jet.oledb.4.0"
.Mode = adModeShareDenyNone
.ConnectionString = ".\MagPc.mdb"
.Open
End With
    Set oRSPortGraphique = New ADODB.Recordset
    strSQLPortGraphique = "SELECT fldPortGraphique FROM tblCarteMere ORDER BY fldPortGraphique"
    With oRSPortGraphique
        .CursorLocation = adUseClient
        .Open strSQLPortGraphique, oADOPortGraphique, adOpenForwardOnly, adLockOptimistic
        .Properties.Refresh
        .MoveLast
        .MoveFirst
        Do While Not .EOF
            ListPortGraphique.AddItem .Fields("fldPortGraphique").Value
            .MoveNext
        Loop
        .Close
    End With
    oADOPortGraphique.Close
    Set oRSPortGraphique = Nothing
    Set oADOPortGraphique = Nothing

3 réponses

cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
13 mai 2007 à 16:54
Salut
Je n'ai pas lu toutyes les lignes, mais ce qui ressort, c'est que tu as une requète (texte) et une ListBox qui change dans chacun des appels. Il suffit donc de paramétrer ces éléments :

Private Sub maSub (byVal sSQL As String, maListBox As ListBox)
   Dim oADOConstructeur   As ADODB.Connection
   Dim oRSConstructeur    As ADODB.Recordset
   Dim strSQLConstructeur As String
   Set oADOConstructeur = New ADODB.Connection
   With oADOConstructeur
      .Provider = "Microsoft.jet.oledb.4.0"
      .Mode = adModeShareDenyNone
      .ConnectionString = ".\MagPc.mdb"
      .Open
   End With
   Set oRSConstructeur = New ADODB.Recordset
   strSQLConstructeur = sSQL
   With oRSConstructeur
      .CursorLocation = adUseClient
      .Open strSQLConstructeur, oADOConstructeur, adOpenForwardOnly, adLockOptimistic
      .Properties.Refresh
      .MoveLast
      .MoveFirst
      Do While Not .EOF
         maListBox.AddItem .Fields(0).Value   ' L'index du Field est tjrs le 1er champ
         .MoveNext
      Loop
      .Close
   End With
   oADOConstructeur.Close
   Set oRSConstructeur = Nothing
   Set oADOConstructeur = Nothing
End Sub
Et pour la lancer (exemple du premier appel) :
Call maSub ("SELECT fldConstructeur FROM tblCarteMere ORDER BY fldConstructeur", _
             ListConstructeur)

Vala
Jack, MVP VB
NB : Je ne répondrai pas aux messages privés

Champion du monde de boule de cristal - 2005
Le savoir est la seule matière qui s'accroit quand on la partage (Socrate)
0
cs_jujujulien Messages postés 18 Date d'inscription mardi 6 février 2007 Statut Membre Dernière intervention 19 mai 2007
13 mai 2007 à 18:57
Grand merci a toi!
0
cs_jujujulien Messages postés 18 Date d'inscription mardi 6 février 2007 Statut Membre Dernière intervention 19 mai 2007
14 mai 2007 à 23:55
j'ai un petit problème quand je met cette ligen dans le form load :
Call maSub ("SELECT fldConstructeur FROM tblCarteMere ORDER BY fldConstructeur", _
             ListConstructeur)
vb me di erreur cette argument n'est pas facultatif et cv entoure Call maSub
0
Rejoignez-nous