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.
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
Last edited by willianrc; Nov 21st, 2022 at 10:28 AM.
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
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....!)
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....!)
(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.
I couldn't run your example, maybe because I don't have any references.
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')
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?)
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.
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
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!
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.
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).
...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.
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).
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.
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.
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.
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.
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.
Originally Posted by willianrc
but i really appreciate your help. more people like you in the world.
Hi, willianrc.
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! You make me happy.
See ya!
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.
Hi, willianrc.
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! You make me happy.
See ya!
Hiii Supreme Urbin,
do you know how to separate your project in Columns like this? :