Soyez le premier à donner votre avis sur cette source.
Vue 8 162 fois - Téléchargée 564 fois
'****************************************************************************************** '****************************************************************************************** ' ' Classe créée par sibi14 : sibi14@skynet.be ' users.skynet.be/sibi14 ' ' Classe permettant de gerer une base de donnée ' ' Propriété : ' ' Additem : Permet d'ajouter une entrée ' ' Changed : Indique si la base de donnée a été modifié ' depuis la dernière sauvegarde ' ' Clear : Vide la base de donnée ' ' Count : Indique le nombre d'entrée dans la base de donnée ' ' DB : Permet de lire/écrire dans la base de donnée ' ' DefineColumn : Définit le nom des colonnes ' ' List : Rafraichit la liste associé à la base de donnée ' ' ListBox : Affecte/lit le ListBox attribué à la base de donnée ' ' ListFormat : Définit/lit le format d'affichage dans la base de ' donnée.Usage: ' ' - %n : Signifie que l'élément de la Nème colonne ' est afficher ici ' ' - %r : Signifie que le reste de la ligne est aligner ' à droite ' ' - %% : Affiche le caractère "%" ' ' NumberOfColumn : ' ' OpenFile : ' ' RemoveItem : ' ' SaveFile : ' ' SearchColumnNumber : ' ' SearchLine : ' ' SearchListIndex : ' ' TypeOfColumn : ' ' TypeOfColumnNumber : ' '****************************************************************************************** '****************************************************************************************** Enum Search Faux = -&H1 Column1 = &H0 Column2 = &H1 Column3 = &H2 Column4 = &H3 Column5 = &H4 Column6 = &H5 Column7 = &H6 Column8 = &H7 Column9 = &H8 Column10 = &H9 Column11 = &HA Column12 = &HB Column13 = &HC Column14 = &HD Column15 = &HE Column16 = &HF FormatList = &H1000 End Enum Dim FileName As String Dim DBSibi() As Variant Dim Vide As Boolean Dim Nom() As String Dim TID() As VbVarType Public Changed As Boolean Public Count As Long Event Format(Valeur As String, ByVal Index As Long) Event Vide() Event PlusVide() Event Change(ByVal Column As Integer, ByVal Line As Integer, ByRef Valeur As Variant) 'variables locales de stockage des valeurs de propriétés Private mvarListBox As Object 'copie locale 'variables locales de stockage des valeurs de propriétés Private mvarListFormat As String 'copie locale Public Property Let ListFormat(ByVal vData As String) 'utilisé lors de l'affectation d'une valeur à la propriété, du coté gauche de l'affectation. 'Syntax: X.ListFormat = 5 mvarListFormat = vData If Trim(mvarListFormat) = "" Then mvarListFormat = "%1" List End Property Public Property Get ListFormat() As String 'utilisé lors de la lecture de la valeur de la propriété, du coté droit de l'instruction. 'Syntax: Debug.Print X.ListFormat ListFormat = mvarListFormat End Property Public Property Let ListBox(ByVal vData As Object) 'utilisé lors de l'affectation d'une valeur à la propriété, du coté gauche de l'affectation. 'Syntax: X.ListBox = 5 mvarListBox = vData List End Property Public Property Set ListBox(ByVal vData As Object) 'utilisé lors de l'affectation d'un Object à la propriété, du coté gauche de l'instruction Set 'Syntax: Set x.ListBox = Form1 Set mvarListBox = vData List End Property Public Property Get NumberOfColumn() As Integer 'utilisé lors de la lecture de la valeur de la propriété, du coté droit de l'instruction. 'Syntax: Debug.Print X.ListBox NumberOfColumn = UBound(DBSibi, 1) + 1 'List End Property Public Property Get ListBox() As Object 'utilisé lors de la lecture de la valeur de la propriété, du coté droit de l'instruction. 'Syntax: Debug.Print X.ListBox If IsObject(mvarListBox) Then Set ListBox = mvarListBox Else ListBox = mvarListBox End If 'List End Property Property Get Db(ByVal Column As Variant, Optional ByVal Line As Variant, Optional ByVal Search As Search = Faux) If IsNumeric(Column) Then x = Column - 1 If x < LBound(DBSibi, 1) Then x = LBound(DBSibi, 1) If x > UBound(DBSibi, 1) Then x = UBound(DBSibi, 1) Else x = SearchColumnNumber(CStr(Column)) - 1 End If If x = -1 Then Exit Property y = 0 If Search > -1 Then y = SearchLine(Line, Search) ElseIf IsMissing(Line) Then If mvarListBox Is Nothing Then Exit Property If mvarListBox.Sorted Then y = SearchLine(mvarListBox.Text, FormatList) Else y = mvarListBox.ListIndex + 1 End If If y < LBound(DBSibi, 2) Then y = LBound(DBSibi, 2) If y > UBound(DBSibi, 2) Then y = UBound(DBSibi, 2) ElseIf IsNumeric(Line) Then y = Line If y < LBound(DBSibi, 2) Then y = LBound(DBSibi, 2) If y > UBound(DBSibi, 2) Then y = UBound(DBSibi, 2) End If If y = 0 Then Exit Property On Error Resume Next Db = Convert(DBSibi(x, y), TID(x)) End Property Property Let Db(ByVal Column As Variant, Optional ByVal Line As Variant, Optional ByVal Search As Search = Faux, Valeur As Variant) Dim x As Long, y As Long Dim ListIndex As Integer If IsNumeric(Column) Then x = Column - 1 If x < LBound(DBSibi, 1) Then x = LBound(DBSibi, 1) If x > UBound(DBSibi, 1) Then x = UBound(DBSibi, 1) Else x = SearchColumnNumber(CStr(Column)) - 1 End If If x = -1 Then Exit Property y = 0 If Search > -1 Then y = SearchLine(Line, Search) ElseIf IsMissing(Line) Then If mvarListBox Is Nothing Then Exit Property If mvarListBox.Sorted Then y = SearchLine(mvarListBox.Text, FormatList) Else y = mvarListBox.ListIndex + 1 End If If y < LBound(DBSibi, 2) Then y = LBound(DBSibi, 2) If y > UBound(DBSibi, 2) Then y = UBound(DBSibi, 2) ElseIf IsNumeric(Line) Then y = Line If y < LBound(DBSibi, 2) Then y = LBound(DBSibi, 2) If y > UBound(DBSibi, 2) Then y = UBound(DBSibi, 2) End If If y = 0 Then Exit Property On Error Resume Next 'If DBSibi(X, Y) <> Convert(Valeur, TID(X)) Then Changed = True 'End If ListIndex = SearchListIndex(y) DBSibi(x, y) = Convert(Valeur, TID(x)) RaiseEvent Change(x + 1, y, DBSibi(x, y)) List y, ListIndex End Property Public Sub OpenFile(Optional FName As String) If Not FName = "" Then FileName = FName Dim a() 'ReDim a(0 To NbreColonne) If Dir(FileName) = "" Then GoTo Error On Error GoTo Error Frf = FreeFile Open FileName For Random As #Frf 'Len = 40 * UBound(DBSibi) Get #Frf, 1, Nom() Close #Frf Open FileName For Random As #Frf Len = 40 * UBound(Nom) Get #Frf, 1, Nom() Get #Frf, 2, TID() ReDim DBSibi(0 To 0, 1 To 1) Do Until LOF(Frf) <= (Loc(Frf)) * 40 * UBound(Nom) Or EOF(Frf) j = j + 1 Get #Frf, 2 + j, a() If UBound(a, 1) = UBound(DBSibi, 1) Then ReDim Preserve DBSibi(0 To UBound(a, 1), 1 To j) Else ReDim DBSibi(0 To UBound(a, 1), 1 To j) For i = 0 To UBound(a, 1) DBSibi(i, j) = a(i) Next i Loop Close #Frf Changed = False Vide = (UBound(DBSibi, 2) = 0) Count = UBound(DBSibi, 2) List Exit Sub Error: Close #1 Class_Initialize List 'If UBound(DBSibi, 2) = 0 Then Vide = False Else Vide = True End Sub Public Sub SaveFile(Optional FName As String) If Not FName = "" Then FileName = FName If Dir(FileName) <> "" Then Kill FileName fr = FreeFile Dim a() As Variant ReDim a(0 To UBound(DBSibi, 1)) As Variant Open FileName For Random As #fr Len = 40 * UBound(DBSibi, 1) Put #fr, 1, Nom() Put #fr, 2, TID() For i = 1 To UBound(DBSibi, 2) For j = 0 To UBound(DBSibi, 1) a(j) = Convert(DBSibi(j, i), TID(j)) Next j Put #fr, i + 2, a() Next i Close #fr Changed = False End Sub Public Sub DefineColumn(ParamArray Name() As Variant) a = UBound(Name, 1) If a <> UBound(DBSibi, 1) Then ReDim DBSibi(0 To a, 1 To UBound(DBSibi, 2)) End If If a <> UBound(Nom, 1) Then ReDim Nom(0 To a) End If If a <> UBound(TID, 1) Then ReDim Preserve TID(0 To a) End If For i = 0 To a Nom(i) = Name(i) If TID(i) = 0 Then TID(i) = vbVariant Next i List End Sub Public Sub TypeOfColumn(ParamArray TypeID() As Variant) a = UBound(TypeID, 1) If a <> UBound(DBSibi, 1) Then ReDim DBSibi(0 To a, 1 To UBound(DBSibi, 2)) End If If a <> UBound(Nom, 1) Then ReDim Preserve Nom(0 To a) End If If a <> UBound(TID, 1) Then ReDim TID(0 To a) End If For i = 0 To a TID(i) = TypeID(i) If TID(i) = 0 Then TID(i) = vbVariant Next i List End Sub Public Sub Clear() ReDim DBSibi(0 To UBound(DBSibi, 1), 1 To 1) Count = 0 Vide = True List RaiseEvent Vide End Sub Public Sub RemoveItem(Optional Index As Long) If Vide Then Exit Sub If UBound(DBSibi, 2) = 1 Then Clear: Exit Sub If Index = 0 Then If mvarListBox Is Nothing Then Exit Sub Index = SearchLine(mvarListBox.Text, FormatList) ListIndex = mvarListBox.ListIndex If Index = -1 Then Exit Sub End If If Index < LBound(DBSibi, 2) Then Index = LBound(DBSibi, 2) If Index > UBound(DBSibi, 2) Then Index = UBound(DBSibi, 2) For i = Index + 1 To UBound(DBSibi, 2) For j = 0 To UBound(DBSibi, 1) DBSibi(j, i - 1) = DBSibi(j, i) Next j Next i If UBound(DBSibi, 2) - 1 = 0 Then Vide = True: ReDim Preserve DBSibi(0 To UBound(DBSibi, 1), 1 To UBound(DBSibi, 2)) Else ReDim Preserve DBSibi(0 To UBound(DBSibi, 1), 1 To (UBound(DBSibi, 2) - 1)) Count = UBound(DBSibi, 2) If mvarListBox Is Nothing Then Exit Sub mvarListBox.RemoveItem CInt(ListIndex) End Sub Public Sub AddItem(ParamArray Valeur() As Variant) If UBound(Valeur) >= LBound(Valeur) Then tmp = Valeur(0) Else tmp = "" a = SearchLine(tmp, Column1) If a = -1 Then GoTo suite For i = 1 To UBound(DBSibi, 1) If i > 0 Then If DBSibi(i, a) <> Valeur(i) And a <> "" Then GoTo suite Next i Exit Sub suite: If Vide = False Then ReDim Preserve DBSibi(0 To UBound(DBSibi, 1), 1 To UBound(DBSibi, 2) + 1) Else RaiseEvent PlusVide End If For i = 0 To UBound(DBSibi, 1) If i >= LBound(Valeur, 1) And i <= UBound(Valeur, 1) Then DBSibi(i, UBound(DBSibi, 2)) = Convert(Valeur(i), TID(i)) Next i Count = UBound(DBSibi, 2) Vide = False If mvarListBox Is Nothing Then Exit Sub mvarListBox.AddItem "", mvarListBox.ListCount List Count, mvarListBox.ListCount - 1 mvarListBox.Refresh mvarListBox.ListIndex = mvarListBox.ListCount - 1 End Sub Private Sub List(Optional Line As Long = -1, Optional ListIndex As Integer = -1) Attribute List.VB_UserMemId = 0 Dim Valeur As String If mvarListBox Is Nothing Then Exit Sub 'If mvarListBox.Container.ActiveControl Is mvarListBox Then b = True 'mvarListBox.Visible = False If Line = -1 Then a = mvarListBox.ListIndex If Count = 0 Then a = 0 mvarListBox.Clear If Vide Then GoTo suite Set frm.Font = mvarListBox.Font For i = 1 To UBound(DBSibi, 2) tmp = False Item = "" c = 1 boucle: Texte = InStr(c, mvarListFormat, "%") If Texte = 0 Then Item = Item & Right(mvarListFormat, Len(mvarListFormat) - c + 1): GoTo FinBoucle If Texte <> c Then Item = Item & Mid(mvarListFormat, c, Texte - c) If Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "%" Then Item = Item & "%" ElseIf UBound(DBSibi, 1) >= Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) - 1 And Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) > 0 Then If TID(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1) = vbDate Then Item = Item & Strings.Format(DBSibi(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1, i), "dd/mm/yy") Else Item = Item & DBSibi(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1, i) End If ElseIf Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "r" Then tmp = True ItemG = Item Item = "" Else Valeur = Mid(mvarListFormat, InStr(c, mvarListFormat, "%"), 2) RaiseEvent Format(Valeur, i) Item = Item & Valeur End If c = InStr(c, mvarListFormat, "%") + 2 GoTo boucle FinBoucle: If tmp Then Item = ItemG & Space(((mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400) + Abs(mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400)) / 2 / frm.TextWidth(" ")) & Item 'Call RemoveItem(162) mvarListBox.AddItem Item & " " Next i If UBound(DBSibi, 2) - 1 < a Then a = UBound(DBSibi, 2) - 1 If UBound(DBSibi, 2) <> 0 Then mvarListBox.ListIndex = a suite: Else i = Line tmp = False Item = "" c = 1 Boucle2: Texte = InStr(c, mvarListFormat, "%") If Texte = 0 Then Item = Item & Right(mvarListFormat, Len(mvarListFormat) - c + 1): GoTo FinBoucle2 If Texte <> c Then Item = Item & Mid(mvarListFormat, c, Texte - c) If Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "%" Then Item = Item & "%" ElseIf UBound(DBSibi, 1) >= Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) - 1 And Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) > 0 Then If TID(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1) = vbDate Then Item = Item & Strings.Format(DBSibi(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1, i), "dd/mm/yy") Else Item = Item & DBSibi(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1, i) End If ElseIf Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "r" Then tmp = True ItemG = Item Item = "" Else Valeur = Mid(mvarListFormat, InStr(c, mvarListFormat, "%"), 2) RaiseEvent Format(Valeur, i) Item = Item & Valeur End If c = InStr(c, mvarListFormat, "%") + 2 GoTo Boucle2 FinBoucle2: If tmp Then Item = ItemG & Space(((mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400) + Abs(mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400)) / 2 / frm.TextWidth(" ")) & Item 'mvarListBox.ListIndex = -1 If ListIndex = -1 Then ListIndex = SearchListIndex(Line) mvarListBox.List(ListIndex) = Item & " " 'mvarListBox.ListIndex = i - 1 End If 'mvarListBox.Visible = True 'If b = True Then mvarListBox.SetFocus End Sub Private Sub Class_Initialize() Changed = False ReDim DBSibi(0 To 0, 1 To 1) ReDim Nom(0) ReDim TID(0) mvarListFormat = "%1" Count = 0 Vide = True End Sub Private Function Convert(Var As Variant, TypeID As VbVarType) If IsMissing(Var) Then Exit Function Select Case TypeID Case vbString a = CStr(Var) Case vbInteger If Var = "" Then Var = "0" a = CInt(Val(Var)) Case vbBoolean a = CBool(Var) Case vbByte If Var = "" Then Var = "0" a = CByte(Val(Var)) Case vbCurrency If Var = "" Then Var = "0" a = CCur(Var) Case vbDate a = CDate(Var) Case vbDouble If Var = "" Then Var = "0" a = CDbl(Var) Case vbDecimal If Var = "" Then Var = 0 If InStr(1, Var, ".") <> 0 Then Mid(Var, InStr(1, Var, "."), 1) = "," a = CDec(Var) Case vbLong If Var = "" Then Var = "0" a = CLng(Val(Var)) Case vbSingle If Var = "" Then Var = "0" a = CSng(Var) Case Else a = CVar(Var) End Select Convert = a End Function Public Function Column(ByVal n As Long) As String Column = Nom(n - 1) End Function Public Function TypeOfColumnNumber(ByVal n As Integer) As VbVarType On Error Resume Next TypeOfColumnNumber = -1 TypeOfColumnNumber = TID(n - 1) End Function Public Function SearchLine(Valeur As Variant, Search As Search) As Integer Dim Text As String y = -1 If Search = FormatList Then If Vide Then GoTo suite For i = 1 To UBound(DBSibi, 2) tmp = False Item = "" c = 1 boucle: Texte = InStr(c, mvarListFormat, "%") If Texte = 0 Then Item = Item & Right(mvarListFormat, Len(mvarListFormat) - c + 1): GoTo FinBoucle If Texte <> c Then Item = Item & Mid(mvarListFormat, c, Texte - c) If Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "%" Then Item = Item & "%" ElseIf UBound(DBSibi, 1) >= Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) - 1 And Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) > 0 Then Item = Item & DBSibi(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1, i) ElseIf Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "r" Then tmp = True ItemG = Item Item = "" Else Text = Mid(mvarListFormat, InStr(c, mvarListFormat, "%"), 2) RaiseEvent Format(Text, i) Item = Item & Text End If c = InStr(c, mvarListFormat, "%") + 2 GoTo boucle FinBoucle: If tmp Then Item = ItemG & Space(((mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400) + Abs(mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400)) / 2 / frm.TextWidth(" ")) & Item If Trim(Item) = Trim(Valeur) Then y = i: GoTo suite Next i suite: ElseIf Search > -1 And Valeur <> "" Then For i = 1 To UBound(DBSibi, 2) If DBSibi(Search, i) = Valeur Then y = i: Exit For Next i Else y = -1 End If SearchLine = y End Function Function SearchListIndex(Valeur As Long) If mvarListBox.Sorted = False Then SearchListIndex = Valeur - 1: Exit Function Dim Text As String y = -1 If Vide Then GoTo suite i = Valeur tmp = False Item = "" c = 1 boucle: Texte = InStr(c, mvarListFormat, "%") If Texte = 0 Then Item = Item & Right(mvarListFormat, Len(mvarListFormat) - c + 1): GoTo FinBoucle If Texte <> c Then Item = Item & Mid(mvarListFormat, c, Texte - c) If Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "%" Then Item = Item & "%" ElseIf UBound(DBSibi, 1) >= Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) - 1 And Val(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1)) > 0 Then Item = Item & DBSibi(Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) - 1, i) ElseIf Mid(mvarListFormat, InStr(c, mvarListFormat, "%") + 1, 1) = "r" Then tmp = True ItemG = Item Item = "" Else Text = Mid(mvarListFormat, InStr(c, mvarListFormat, "%"), 2) RaiseEvent Format(Text, i) Item = Item & Text End If c = InStr(c, mvarListFormat, "%") + 2 GoTo boucle FinBoucle: If tmp Then Item = ItemG & Space(((mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400) + Abs(mvarListBox.Width - frm.TextWidth(ItemG & Item) - 400)) / 2 / frm.TextWidth(" ")) & Item For i = 1 To UBound(DBSibi, 2) If Trim(Item) = Trim(mvarListBox.List(i - 1)) Then y = i - 1: GoTo suite Next i suite: SearchListIndex = y End Function Function SearchColumnNumber(Column As String) As Integer x = -1 For i = 0 To UBound(Nom, 1) If Nom(i) = Column Then x = i + 1: Exit For Next i SearchColumnNumber = x End Function
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.