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 63
| Imports System.Data.OleDbPublic Class Form1
Const MyRep As String = "C:\Myrep\Fruits", MyFichier = "reference.txt"
Dim Shema As String = $"[{MyFichier}]
Format= Delimited(;)"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With Me.cbRef
.SelectedIndex = -1
.DataSource = OpenFtxt(MyRep, $"Select Distinct [reference] From [{MyFichier}] order By [reference]", Shema, True)
.ValueMember = "reference"
.DisplayMember = "reference"
End With
MessageBox.Show(Reperoire("Chemin du fichier légumes :C:test\\test1.TXT"))
End Sub
Private Sub cbRef_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbRef.SelectedIndexChanged
If cbRef.SelectedIndex > -1 Then
With cbCoul
.DataSource = OpenFtxt(MyRep, $"Select Distinct [couleur] From [{MyFichier}] WHERE [reference]={cbRef.Text} order By [couleur]", Shema, True)
.ValueMember = "couleur"
.DisplayMember = "couleur"
End With
Dim td As DataTable = OpenFtxt(MyRep, $"Select Distinct [designation],[conditionnement] From [{MyFichier}] WHERE [reference]={cbRef.Text}", Shema, True)
If td IsNot Nothing Then
With td
If .Rows.Count > 0 Then
With .Rows(0)
designation.Text = .Item("designation")
conditionement.Text = .Item("conditionnement")
End With
End If
End With
End If
End If
End Sub
Function Reperoire(ByVal txt As String) As String
Dim pose As Long = txt.IndexOf(":") + 1
txt = txt.Substring(pose, txt.Length - pose).Replace("\", ":").Replace(":", ":\").Replace("\\", "\")
If txt.Substring(0, 1) = "\" Then txt = "\" + txt
Return txt
End Function
Private Function OpenFtxt(folder As String, SQL As String, Shemat As String, Titre As Boolean) As DataTable
Dim objWriter As New System.IO.StreamWriter($"{folder}\schema.ini", False)
objWriter.WriteLine(Shemat)
objWriter.Close()
Dim con As String = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={folder};Extended Properties='text;HDR={If(Titre, "Yes", "No")};FMT=Delimited';"
Dim dt As New DataTable
Using Adp As New OleDbDataAdapter(SQL, con)
Try
Adp.Fill(dt)
Return dt
Catch ex As Exception
Return Nothing
End Try
End Using
End Function
End Class |
Partager