Link to home
Start Free TrialLog in
Avatar of NUPE2006
NUPE2006

asked on

Remove item from Combobox list and Text file

Hello All,

I'm creating a program in VB6 which the user can add/remove regions to be modified via combobox. The problem I'm running into is that when I go to remove a region, the code I currently have adds blank spaces and doesn't remove the region from the combobox or the text file. Could someone please let me know where I'm going wrong?
Public Sub RemoveRegion(ByVal strFileName As String, _
    ByVal strSearchString As String)
    'Search for text if found Delete Line
    Dim strTxt As String
    Dim strLines() As String
    Dim I As Integer
 
    'strFileName = "C:\regions.txt"
    'strSearchString = me.cboSelectRegion.Text
 
    'Open the text file
    Open strFileName For Input As #1
    'load the text file in strTxt variable
    strTxt = Input(LOF(1), 1)
    'close the text file
    Close #1
    'Split the text file
    strLines() = Split(strTxt, vbCrLf)
 
    'Clear existing text
    strTxt = ""
 
    For I = LBound(strLines) To UBound(strLines)
        'search the string
        If InStr(1, strLines(I), strSearchString) = 0 Then
            strTxt = strTxt & vbCrLf & strLines(I)
        End If
    Next I
 
    'open the text file to write the content of strTxt
    Open strFileName For Output As #1
    Print #1, strTxt
    'Close the text file
    Close #1
End Sub

Open in new window

Avatar of JonMny
JonMny

Are you sure that each line has a CRLF?
Avatar of NUPE2006

ASKER

the text file only contains the regions for the textbox
example:
1. gugbuq12
2.gughwun9

Should I remove that portion of the code ?
SOLUTION
Avatar of JonMny
JonMny

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
O...that was a real dumb mistake. I didn't even notice. I'll fix those errors and run it to see if that fixes the problem. I'm new to this so thanks for taking the time to help.
I ran it and got the same results. Should I search for a whole other method or is the one I'm using correct and I just need to change some things?
Avatar of GrahamSkan
It seems to work, but try this alternative. Note that neither this nor your code attempt to affect the combo.
Public Sub RemoveRegion(ByVal strFileName As String, _
    ByVal strSearchString As String)
    'Search for text if found Delete Line
    Dim strText As String
    Dim strLines() As String
    Dim i As Integer
    Dim j As Integer
    Dim f As Integer
 
    f = FreeFile
    Open strFileName For Input As #f
    Do Until EOF(f)
        Line Input #f, strText
        If strSearchString <> strText Then
            ReDim Preserve strLines(i)
             strLines(i) = strText
             i = i + 1
        End If
    Loop
    Close #f
    f = FreeFile
    Open strFileName For Output As #f
        For j = 0 To i - 1
            Print #f, strLines(j)
        Next j
    Close #f
End Sub

Open in new window

I've attached the code for my command button below. Also how do I update my combobox after changes? Is what I have correct?
Private Sub cmdDelRegion_Click()
    Call RemoveRegion("C:\Documents and Settings\hbc1636\Desktop\regions.txt", Me.cboSelectRegion.Text)
   'Load updated combobox list
    Dim the_file As String
the_file = "C:\Documents and Settings\hbc1636\Desktop\regions.txt"
 
Open the_file For Input As #1 'open the file
While Not EOF(1)
    Line Input #1, temp 'read in a line
    cboSelectRegion.AddItem temp 'add that line to the combobox
Wend
'close the file
Close #1
    MsgBox "Region Removed"
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think your original problem is coming from this:

 strLines() = Split(strTxt, vbCrLf)
 in that there is no gaurantee of a good line parse which may add additional entries to the split array.  Will have much better results using Line input as in Skan's code above.
The leading numbers are actually in the file due to using UltaEdit 32. In the suggested code should I replace freefile with the path to my text file?
No. Freefile is a function to find the first spare VB internal file number that can be used.  In a simple program it will probably be 1, so the #1 that you have used is unlikely to give an problem, but this makes sure.
Your file name should go into strFileName.
Next, I'm going to find out how to make it affect the combobox Thanks for everything, I know you all must be pretty busy and I appreciate you taking your time to help.
Thank you everybody for your time and not getting frustrated with me. To make it take affect on the combobox i have to save the file and then reload it, right?
No.You can look for and delete an individual item.

    For i = i To Combo1.ListCount - 1
        If Combo1.List(i) = strSearchString Then
            Combo1.RemoveItem i
        End If
    Next i

Open in new window

Sorry. The 'No' may have been misleading. Yes, of course you can reload the combo if you prefer. You can use the .Clear method to remove all the items.
Ok Thanks.