-
2 Attachment(s)
[RESOLVED] vb6 CRUD in Text File
Hi guys, I'm developing a CRUD in VB6, but I didn't find much on google. I would like to ask for your help.
I need your help just to diplay the items in listbox without '|' , I tried 'split' but I didn't get success.
also I would like to select items in listbox and fill the textbox as expected.
and I need your help because my searches getting errors.
See img:
Attachment 186270
my project in attached.
Thanks in advance. Attachment 186271
-
Re: vb6 CRUD in Text File
-
Re: vb6 CRUD in Text File
Instead of a ListBox use the MS(H)FlexGrid.
You can specify the numbers of rows and columns for the grid and put each value in it's own cell.
-
Re: vb6 CRUD in Text File
Code:
Option Explicit
Dim way As Variant
Dim a, X
Public szValidPath, sName As String
Public Sub Pathway(way As Variant) 'SET DATABASE FOLDER
way = App.Path & "\data"
End Sub
Private Sub Form_Load()
Call Create_Folder
Call Add_Counter
Call PopulateListbox
Call Load_Combobox1
End Sub
Private Sub Command4_Click()
Dim i As Long, Lindex As Long
If IsArray(a) Then
Me.List1.List = a
Else
Me.List1.Clear
End If
Lindex = Me.Combo1.ListIndex + 1
If Lindex = 0 Then Exit Sub
With Me.List1
If .ListCount Then
For i = .ListCount - 1 To 0 Step -1
If Val(Split(.List(i, 3), "/")(1)) <> Lindex Then .RemoveItem i
Next
End If
End With
End Sub
Private Sub Load_Combobox1()
With Combo1
.AddItem "01 - January"
.AddItem "02 - February"
.AddItem "03 - March"
.AddItem "04 - April"
.AddItem "05 - May"
.AddItem "06 - June"
.AddItem "07 - July"
.AddItem "08 - August"
.AddItem "09 - September"
.AddItem "10 - October"
.AddItem "11 - November"
.AddItem "12 - December"
End With
End Sub
Private Sub Command3_Click() 'DELETE BUTTON
Call DeleteData
Call PopulateListbox
End Sub
Private Sub DeleteData() ' DELETE
Call Pathway(way) 'SET FOLDER WAY
Dim i As Long, ii As Long, n As Long, txt As String
If Me.List1.ListIndex = -1 Then Exit Sub
X(Me.List1.ListIndex) = Chr(1)
X = Filter(X, Chr(2), 0)
If UBound(X) > -1 Then
txt = Join(X, vbNewLine)
Else
X = Empty
End If
Open (way & "\users.txt") For Output As #1
Print #1, txt;
Close #1
Call PopulateListbox
End Sub
Private Sub List1_Click() 'LOAD INFORMATIONS IN TEXTBOX ABOUT THE REMITTEE
Text1.Text = List1.List(0)
Text2.Text = List1.List(1)
Text3.Text = List1.List(2)
Text4.Text = List1.List(3)
End Sub
Private Sub PopulateListbox()
Call Pathway(way) 'SET FOLDER WAY
List1.Clear
Dim fNum As Integer
Dim sInput As String
Dim iInd As Integer
'To load the ListBox from file:
fNum = FreeFile
'List1.Clear
Open way & "\users.txt" For Input As #fNum
Do Until EOF(fNum)
Input #fNum, sInput
List1.AddItem sInput
Loop
Close #fNum
End Sub
Private Sub Create_Folder() 'CREATE FOLDER
Call Pathway(way) 'SET FOLDER WAY
Dim CheckFolder As String
CheckFolder = way
If Dir(CheckFolder, vbDirectory) = "" Then MkDir CheckFolder
Call Check_UsersTXT
End Sub
Private Sub Check_UsersTXT() 'CHECK FILE USERS
Call Pathway(way) 'SET FOLDER WAY
Dim strPath As Variant
Dim strCheck As String
'Arquivo que verificaremos
strPath = way & "\users.txt"
If Dir(strPath) = vbNullString Then
strCheck = False
Call Create_UserTXT 'if the file NO exist do it.
Else
strCheck = True
'if the file exist do it. Nothing
End If
End Sub
Private Sub Create_UserTXT() 'CREATE FILE USER
Call Pathway(way) 'SET FOLDER WAY
Dim f As Integer
f = FreeFile
Open way & "\users.txt" For Output As #f
Close #f
End Sub
Private Sub Command1_Click()
Call Pathway(way) 'SET FOLDER WAY
'Open way & "\users.txt" For Output As #1
'Print #1, Text1.Text & "," & Text2.Text & "," & Text3.Text
'Close #1
'Call PopulateListbox
Call Add_Counter
SaveInfo VBA.Trim(Text1.Text) & "|" & VBA.Trim(Text2.Text) & "|" & VBA.Trim(Text3.Text) & "|" & VBA.Trim(Text4.Text)
Call PopulateListbox
Call Clear_Fields
Call Add_Counter
End Sub
Private Sub SaveInfo(LogMessage As String) 'SAVE DATA
Dim LogFileName As String
Dim FileNum As Integer
Call Pathway(way) ''''SET FOLDER WAY
LogFileName = way & "\users.txt" 'Set file name
If Dir(LogFileName, vbNormal) <> "" Then 'If the file does not
If FileLen(LogFileName) Then LogMessage = vbNewLine & LogMessage 'exist, the same file is
End If 'created
FileNum = FreeFile
Open LogFileName For Append As #FileNum
Print #FileNum, LogMessage;
Close #FileNum
End Sub
Private Sub Add_Counter() 'ADD COUNTER
Call Check_UsersTXT
Dim FileName As String
Dim FileNum As Integer
Dim Arr() As Variant
Dim r As Long
Dim data As String
'' *** Change path and file name to suit ***
Call Pathway(way) 'SET FOLDER WAY
FileName = way & "\users.txt"
FileNum = FreeFile
r = 1
Open FileName For Input As #FileNum
Do While Not EOF(FileNum)
Line Input #FileNum, data
ReDim Preserve Arr(1 To r)
Arr(r) = data
r = r + 1
Loop
Close #FileNum
If FileLen(FileName) <> 0 Then 'TEST IF BLANK TXT FILE
Text1.Text = Split(Arr(UBound(Arr)), "|")(0) + 1
Else
Text1.Text = 1
End If
End Sub
Private Sub Command2_Click()
Call TextFile_FindReplace
Call PopulateListbox
End Sub
Private Sub TextFile_FindReplace() 'REPLACE DATA
Dim valores() As String, linha As String, X As String, ID As String, NewName As String, NewAddress As String, NewBirthday As String
ID = Text1.Text
NewName = Text2.Text
NewAddress = Text3.Text
NewBirthday = Text4.Text
Call Pathway(way) 'SET FOLDER WAY
Open (way & "\users.txt") For Input As #1
Do While EOF(1) = False
Line Input #1, linha
If linha <> "" Then
valores = Split(linha, "|")
If valores(0) = UCase(ID) Then
valores(1) = NewName
valores(2) = NewAddress
valores(3) = NewBirthday
linha = Join(valores, "|")
End If
If X <> "" Then
X = X + vbNewLine + linha
Else
X = X + linha
End If
End If
Loop
Close #1
Open (way & "\users.txt") For Output As #1
Print #1, X;
Close #1
End Sub
Private Sub Clear_Fields()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
Private Sub Text4_KeyPress(KeyAscii As Integer) 'TEXTBOX FOR DATE
Text4.MaxLength = 10 '10/10/2017
Select Case KeyAscii
Case 8 'Accept o BACK SPACE
Case 13: SendKeys "{TAB}" 'Emula o TAB
Case 48 To 57
If Text4.SelStart = 2 Then Text4.SelText = "/"
If Text4.SelStart = 5 Then Text4.SelText = "/"
Case Else: KeyAscii = 0 'Ignore others caracters
End Select
End Sub
Private Sub Text5_Change() 'TEXTBOX FOR SEARCH IN LISTBOX
Dim i As Long
Dim sFind As String
sFind = Me.Text5.Text
If Text5.Text = "" Then Exit Sub
If Len(sFind) = 0 Then
Me.List1.ListIndex = -1
Me.List1.TopIndex = 0
Else
For i = 0 To Me.List1.ListCount - 1
If InStr(UCase(List1.List(i, 1)), UCase(sFind)) > 0 Then
Me.List1.TopIndex = i
Me.List1.ListIndex = i
Exit For
End If
Next i
End If
End Sub
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
Arnoutdv
Instead of a ListBox use the MS(H)FlexGrid.
You can specify the numbers of rows and columns for the grid and put each value in it's own cell.
I will try with Listview, to see
-
Re: vb6 CRUD in Text File
If he‘s using Textfiles (why not a Database?), why in blazes isn‘t he using one of those gazillion CSV-Classes/Parsers you can find on the net?
EDIT: or use ADO (or was it DAO?) with Textdriver.
-
Re: vb6 CRUD in Text File
is your textfile in the zip file?
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
SamOscarBrown
is your textfile in the zip file?
yes. it is
-
1 Attachment(s)
Re: vb6 CRUD in Text File
Modified project attached, should do what you need. New code is commented. Hope this helps
EDIT: I didn't fix the search, I'm at work and out of time for now.....
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
SomeYguy
Modified project attached, should do what you need. New code is commented. Hope this helps
EDIT: I didn't fix the search, I'm at work and out of time for now.....
great job. more people like you in the world :wave:
do you know how can I separate in columns ?
Code:
'- Add to listbox
List1.AddItem strList 'sInput
'-======================
because the Delete button got message 'Mismatch' I think it's because it expecting a number and I m passing a text.
thanks in advance
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
willianrc
great job. more people like you in the world :wave:
do you know how can I separate in columns ?
thanks in advance
For columns, probably best to use a good grid control rather than a listbox such as this one by LeandroA here:
https://www.vbforums.com/showthread....530-ucGridPlus
I'll look at the delete code when I get a chance.
-
2 Attachment(s)
Re: vb6 CRUD in Text File
Here is AN example using a database...simple to implement.
(NOTE---I did not include a number of checks that you should to make sure users enter data correctly....!)
Attachment 186307
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
SamOscarBrown
Here is AN example using a database...simple to implement.
(NOTE---I did not include a number of checks that you should to make sure users enter data correctly....!)
Attachment 186307
I couldn't run your example, maybe because I don't have any references. :blush:
'note:
'Added Reference to Microsoft ActiveX Data Objects 2.8 Library
'Added MSFlexGrid Control
'Added DTPicker Control
-
Re: vb6 CRUD in Text File
You know how to add References and Controls?
If not, For References:
(Main Menu) Select Project, then References. Then FIND the one I added...add it.
For Controls, Right-Mouse on the Toolbox (If not visible, go to View, then ToolBox);
Select "Components".
From the popup list select "Microsoft FlexGrid Control 6.0"
From the same list, select "Microsoft Windows Common Controls-2.60"
Click OK
You will now see the icons in the ToolBox for the Flexgrid control and the DTPicker Control.
Add them to the form if not already there. (If you are using MY example, they should be there.---and, I set the FlexGrid Control's name to "Grid1", and set number of rows to 0 and number of columns to 4. In addition, I set FixedCols and FixedRows to 0.
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
willianrc
I couldn't run your example, maybe because I don't have any references. :blush:
Sam made the mistake, to base the whole thing on a DBFile in "AccDB"-Format -
(which - other than the Jet-Engines *.mdbs - are not supported "out-of-the-box" on current Win-OS')
I've re-worked Sams example-code a bit (changing to the Jet-Engine and Krools VBFlexGrid) -
and uploaded it into the CodeBank: https://www.vbforums.com/showthread....gine-CRUD-Demo
HTH
Olaf
-
Re: vb6 CRUD in Text File
@Olaf...
Sam made the mistake, to base the whole thing on a DBFile in "AccDB"-Format -
(which - other than the Jet-Engines *.mdbs - are not supported "out-of-the-box" on current Win-OS')
What do you mean? (I'm a little slow on the uptake.)
Are you saying that the little sample I provided will not run on Win11? (Meaning, unless Access is installed, it won't work?)
Sam
-
Re: vb6 CRUD in Text File
-
2 Attachment(s)
Re: vb6 CRUD in Text File
My attempt :D
Wish this project will be useful to you, willianrc.
See ya :wave:
-
Re: vb6 CRUD in Text File
Hey Urbin,
-I opened your project, and it's AWESOME . but I noticed that the add routine is not adding to the txt file only in listbox.
-I also noticed that you add values to the txt file on a line by line basis for each value.
-and this my project i have to follow pattern: [value][delimiter][value][delimiter][value] in txt file. because the other software reads this pattern.
but i really appreciate your help. more people like you in the world. :)
-
1 Attachment(s)
Re: vb6 CRUD in Text File
Hi guys,
I changed my lisbox for Listview . My routines are working except Delete and Search , I got some errors. Can anyone help me with routines Delete and Search ?
Code:
Private Sub Command4_Click() 'SEARCH BUTTON
Dim i As Long, Lindex As Long
If IsArray(a) Then
Me.ListView1.ListItems = a
Else
Me.ListView1.ListItems.Clear
End If
Lindex = Me.Combo1.ListIndex + 1
If Lindex = 0 Then Exit Sub
With Me.ListView1
If .ListItems Then
For i = .ListItems - 1 To 0 Step -1
If Val(Split(.List(i, 3), "/")(1)) <> Lindex Then .RemoveItem i
Next
End If
End With
End Sub
Code:
Private Sub DeleteData() ' DELETE DATA
Call Pathway(way) 'SET FOLDER WAY
Dim i As Long, ii As Long, n As Long, txt As String
If Me.ListView1.ListIndex = -1 Then Exit Sub
X(Me.ListView1.ListIndex) = Chr(1)
X = Filter(X, Chr(2), 0)
If UBound(X) > -1 Then
txt = Join(X, vbNewLine)
Else
X = Empty
End If
Open (way & "\users.txt") For Output As #1
Print #1, txt;
Close #1
Call PopulateListview
End Sub
See my project attached.
Attachment 186333
-
1 Attachment(s)
Re: vb6 CRUD in Text File
you didn't get this error when running your program?????
See what it is telling you.....
Attachment 186334
And why are you insisting on using flat files???? A database (of some sort) would make a WHOLE LOT MORE sense.
Sammi
-
Re: vb6 CRUD in Text File
OH, and another thing...you are not using Option Explicit (at top of form code)... if you were, you would see a WHOLE BUNCH OF errors that you must/should fix.
EDIT: You REALLY ought to look at Olaf Schmidt's link to the codebank...his demo is great!
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
SamOscarBrown
OH, and another thing...you are not using Option Explicit (at top of form code)... if you were, you would see a WHOLE BUNCH OF errors that you must/should fix.
EDIT: You REALLY ought to look at Olaf Schmidt's link to the codebank...his demo is great!
hi SamOscarBrown,
I don't know if you read my message above but I will tell you:
this my project i have to follow pattern: [value][delimiter][value][delimiter][value] in txt file. BECAUSE the OTHER SOFTWARE reads this pattern.
I got some errors that means I dont have enough knowlodge to fix it , otherwise if I knew how to fix it I would not open this post. also I'm still learning , I m not professional coder. please dont judge me and try to help me otherwise you gonna get me frustrated.
do not get me wrong please.
-
Re: vb6 CRUD in Text File
not judging you at all...we all started anew once. And ONE thing you really should do is use Option Explicit. To make it DEFAULT in a new project, go to Tools, Options, Check the option to "Require Variable Declaration" under the Editor Tab. Restart your program. That will place the words Option Explicit (meaning, Option Explicit ON) at the top of every new form/module/etc. This allows VB6 to identify several errors if you don't declare variables (etc) before you use them. Go ahead on your project, and do that, then run the project and see if you can fix most of your errors before you repost your project. We are here to help, but if you want to ignore our advice (like the good advice Olaf provided), then so be it...but you won't learn anywhere near as fast if you simply continue to 'to things your way'.
Sam
PS--buy and read a book about VB6 programming...Also, extensively use your MSDN (that is the "HELP" for VB).
-
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
willianrc
...i have to follow pattern:
[value][delimiter][value][delimiter][value] in txt file.
BECAUSE the OTHER SOFTWARE reads this pattern.
Well, from the Jet/ADO-Demo I've posted into the Codebank,
you can export such a String-Format at any time with a one-liner:
Rs.Clone.GetString(, , ",") '<- free choice of the Column-Delimiter
Just add the following into the Demo-Form to check it out:
Code:
Private Sub Form_Click()
MsgBox Rs.Clone.GetString(, , ",")
End Sub
All that remains is (I leave that as "homework"), to replace the MsgBox-term above with your own:
WriteStringToTextFile Rs.Clone.GetString(, , ",") , YourFileName
...routine.
Olaf
-
1 Attachment(s)
Re: vb6 CRUD in Text File
okay...so you insist on that funky textfile .... try this convoluted (but works) program (I left the DB stuff in there but commented it out---well, most of it).
Attachment 186336
Sam
-
Re: vb6 CRUD in Text File
BUT...if you really want a much, much better solution (and you still want to use that file of yours), use what Olaf posted. All you would have to do is load that file into the table in the database...do all the stuff you want with the grid (CRUD), and after each 'event', simply save what is in the database (or the grid) back to a file...I THINK you were able to import your file to a listview or listbox, and export it from there as well...you can simply do the same into and out of a database table.
Anyway...think I am out of here...been playing around with this WAY to long...Good Luck.
-
2 Attachment(s)
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
willianrc
Hey Urbin,
-I opened your project, and it's AWESOME . but I noticed that the add routine is not adding to the txt file only in listbox.
Hi, willianrc. :wave:
That's because it's something weird to be saving a file each time a new item it's added. It's not good for an SSD to be writing small pieces of data all the time.
A better way it's clicking the File Menu > Save option when it's necessary.
Quote:
Originally Posted by
willianrc
-I also noticed that you add values to the txt file on a line by line basis for each value.
Yeah! Because this is a better way to save the information of each Item and later read it back. In that way, you wouldn't need to worry about the Delimiters of each String of data.
Quote:
Originally Posted by
willianrc
-and this my project i have to follow pattern: [value][delimiter][value][delimiter][value] in txt file. because the other software reads this pattern.
Oh! I'm sorry. I'm going to pay more attention next time.
Look; this is my second attempt. I think now it's more closely to what you needed.
Quote:
Originally Posted by
willianrc
but i really appreciate your help. more people like you in the world. :)
Thank you! :cry: You make me happy.
See ya! :wave:
-
2 Attachment(s)
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
Urbin
Hi, willianrc. :wave:
That's because it's something weird to be saving a file each time a new item it's added. It's not good for an SSD to be writing small pieces of data all the time.
A better way it's clicking the File Menu > Save option when it's necessary.
Yeah! Because this is a better way to save the information of each Item and later read it back. In that way, you wouldn't need to worry about the Delimiters of each String of data.
Oh! I'm sorry. I'm going to pay more attention next time.
Look; this is my second attempt. I think now it's more closely to what you needed.
Thank you! :cry: You make me happy.
See ya! :wave:
Hey Urbin , you're master man!!!
This is more than I expected. hahaha
I m really happy now, but would share mine as well, Of course yours turned out much better than mine.
See attached.
Thanks for all cooperations
Attachment 186343
Attachment 186342
-
1 Attachment(s)
Re: vb6 CRUD in Text File
Quote:
Originally Posted by
Urbin
Hi, willianrc. :wave:
That's because it's something weird to be saving a file each time a new item it's added. It's not good for an SSD to be writing small pieces of data all the time.
A better way it's clicking the File Menu > Save option when it's necessary.
Yeah! Because this is a better way to save the information of each Item and later read it back. In that way, you wouldn't need to worry about the Delimiters of each String of data.
Oh! I'm sorry. I'm going to pay more attention next time.
Look; this is my second attempt. I think now it's more closely to what you needed.
Thank you! :cry: You make me happy.
See ya! :wave:
Hiii Supreme Urbin,
do you know how to separate your project in Columns like this? :
Attachment 186345
reference link : https://stackoverflow.com/questions/...istbox-vba-vb6
Cheers