[RESOLVED] Combo and arrays of txt controls - prob with saving to a txt file
Welcome
I have something like this
First the code it's to saving the txt file - how see I used CommonDialog control, and second code it's to a reading - I used also CommonDialog control.
Code:
Option Explicit
Dim IntLic As Long
Dim iCount As Long
Dim intNumPlik As Long
Dim strBufor As String
Dim strKatalog As String
Dim data() As String
Dim i As Long
Dim lgCoKas As Long
''''''''''''''''''''''''''''''''' ZAPIS (eng: save of data) danych do pliku txt '''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdZapis_Click()
Dim strPlikZapisz As String
With cdlMyDialog
.CancelError = True
On Error GoTo BladPoCancel ' << rozmyślił się User i klik na Cancel
.InitDir = strKatalog
.Flags = cdlOFNOverwritePrompt + cdlOFNPathMustExist
.FileName = ""
.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
.ShowSave
On Error GoTo CapBlad ' << a to gdy inny błąd
strPlikZapisz = .FileName
End With
intNumPlik = FreeFile
Open strPlikZapisz For Output As #intNumPlik
For IntLic = 0 To 6 ' << tablica txt siup do pliku
Print #intNumPlik, txtRaz(IntLic)
Next
If Combo1.ListCount > 0 Then
For i = 0 To Combo1.ListCount - 1 ' << zawartość Combo siup do pliku
Print #intNumPlik, Combo1.List(i)
Next
End If
Close #intNumPlik
Exit Sub
CapBlad:
MsgBox "Nieoczekiwany błąd:" & vbNewLine _
& "Err # " & Err.Number & " - " & Err.Description, _
vbCritical, _
"Błędne działanie"
BladPoCancel:
End Sub
''''''''' eng: READ and LOAD data to a objects (the array txt and ComboBox)''''''''
'''''''''''''''''''''' ODCZYT i ZAŁADOWANIE danych do obiektów '''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdOdczyt_Click()
Dim strOtworzPlik As String
With cdlMyDialog
.CancelError = True
On Error GoTo CancelError ' << user wcisnął Cancel
.InitDir = strKatalog
.Flags = cdlOFNHideReadOnly
.FileName = ""
.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
.ShowOpen
On Error GoTo TrapError ' << inny błąd
strOtworzPlik = .FileName
End With
intNumPlik = FreeFile
Open strOtworzPlik For Input As #intNumPlik
strBufor = Input$(LOF(intNumPlik), intNumPlik)
Close #intNumPlik
data = Split(strBufor, vbNewLine) '<<< strBufor - teraz podziel dane na oddzielne linie
For IntLic = 0 To 6
txtRaz(IntLic) = data(IntLic) '<<< przekaż część do tablicy kontrolek txt
Next
For IntLic = 7 To UBound(data) '<<< resztę - do końca - dodaj do Combo1
Combo1.AddItem data(IntLic) '<<< I TUTAJ jest coś źle
Next '<<< bo dodaje pustą linię na końcu
lgCoKas = 4 '<<< licznik, jego wartość steruje blokiem kasowania
Combo1.ListIndex = 0 '<<< ustaw w Combo 1 pozycję
''''''''''''ustaw w belce okna informację jaki plik otworzyłeś''''''''''''''
Me.Caption = vbNullString: Me.Caption = "Otwarto plik" & " " & strOtworzPlik
Exit Sub
TrapError:
MsgBox "Nieoczekiwany błąd:" & vbNewLine _
& "Err # " & Err.Number & " - " & Err.Description, _
vbCritical, _
"Błędne działanie"
CancelError:
End Sub
I note, my code to saving has a some the error, because it write in addition for me a blank line on the end of this file. What is wrong in my code? Someone it know?
Thanks in advance
Re: Combo and arrays of txt controls - prob with saving to a txt file
The blank line at the end is not a real blank line, the problem is occurs when you use the Split() statement. The problem is because you use the Print() method, to save the values, so the last line will be the last value and an CrLF. The Split() just recognizes this last CrLF as a new value, so it will read it as empty, because nothing is left.
Its better to skip the last line, by decreasing the for...next by 1.
Code:
For IntLic = 7 To UBound(data) - 1
Or you can format the last line in your saving routine
Code:
For i = 0 To Combo1.ListCount - 2
Print #intNumPlik, Combo1.List(i)
Next
Print #intNumPlik, Combo1.List(Combo1.ListCount - 1);
The ; closing in the Print method prevents to placing a closing crlf at the end of the value.
Re: Combo and arrays of txt controls - prob with saving to a txt file
In cmdZapis_Click add Dim strArray() As String and then
Code:
If Combo1.ListCount Then
ReDim strArray(Combo1.ListCount - 1)
For i = 0 To UBound(strArray)
strArray(i) = Combo1.List(i)
Next i
Print #intNumPlik, Join(strArray, vbNewLine);
End If
This way you save a joined string array separated by line changes and the ; character will prevent writing an additional line change to the end.
You could also improve your reading code to ignore empty lines, simply check for length with Len (or LenB).
Re: Combo and arrays of txt controls - prob with saving to a txt file
Welcome
Many thanks :thumb: :thumb: :thumb: :thumb: for you both, it's working correctly now
I greet :wave: :bigyello: