Results 1 to 9 of 9

Thread: [RESOLVED] A problem when saving the data

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    53

    Resolved [RESOLVED] A problem when saving the data

    I will have a program which will save the equipments into the text file and I have a button which increases the number of the equipment. That button works now, but I want the program to save the new number. For example,
    First,

    Resistors 15
    Control cards 22

    I increased the the number of resistors. It's 16 now. I want to save it after deleting the old data

    Then,
    It should be

    Resistors 16
    Control cards 22


    when I try to do this, it will be

    Resistors 15
    Control cards 22
    Resistors 16


    How can I solve this problem?

    Thanks

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: A problem when saving the data

    here is a theoretical solution:
    1. read both lines
    2. make the necessary changes to the data
    3. empty the file (delete and recreate/open to overwrite)
    4. write the data

    hope this helps.

  3. #3
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: A problem when saving the data

    it depends on how you handled the file in the first place as we can see what you did we are shooting in the dark

    more info needed
    here to talk

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    53

    Re: A problem when saving the data

    I have a command buttons called save and increase and I have combobox and text box.

    I select resistor from combobox and I write 10 in textbox and I click the save button. The program writes into the file

    Resistor 10


    After that, I select resistor from combobox and I click increase button. It writes

    Resistor 10
    Resistor 11

    I want it to write

    Resistor 11


    What should I do?

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: A problem when saving the data

    You cannot simply change text within a file unless what you are changing results in the exact same number of characters. Even so, doing so is a bit of a pain. There are 2 common methods, among others, that people generally use

    1) Read the entire file into memory. Change what is needed. Re-write the entire file. Similar to what mebhas mentioned in reply #2

    2) Open 1 file for reading, another for writing. Read a line from 1st file, modify it if needed, write the line to the 2nd file. Close both files when done. Delete 1st file & rename 2nd file to 1st file's name.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    53

    Re: A problem when saving the data

    I couldn't find a solution that's why I copy the code

    vb Code:
    1. If category2 = "" And category3 = "" And category4 = "" Then
    2.    
    3.         Open "D:\\test.txt" For Input As #1
    4.         Do While Not (EOF(1))
    5.             Input #1, read
    6.             a = Mid(read, 1, M)
    7.             M = Len(read)
    8.             If InStr(1, read, category, 1) <= 0 Or InStr(1, read, equip_number) <= 0 Then
    9.                 MsgBox "There is no record like this, please check the number and the name of category"
    10.                 Exit Sub
    11.            
    12.             ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 1, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
    13.        w = Right(read, 1)
    14.  
    15.        
    16.         Exit Do
    17.        
    18.     ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 2, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
    19. w = Right(read, 2)
    20.  
    21.         Exit Do
    22.        
    23.     ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 3, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
    24. w = Right(read, 3)
    25.  
    26.         Exit Do
    27.        
    28.     ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 4, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
    29. w = Right(read, 4)
    30.  
    31.         Exit Do
    32.        
    33.     ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 5, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
    34. w = Right(read, 5)
    35.  
    36.     Exit Do
    37.      
    38.     End If
    39.     Loop
    40.     Close #1
    41.  
    42.     a = " "
    43.     Text_number_of_equipment.Text = w + 1
    44.     End If
    45.  
    46.     Open "D:\\test.txt" For Append As #1
    47.         Print #1, category, equip_number, w
    48.         Close #1


    category = the name of equipment
    equip_number = the number of equipment to distinguish the other products which have the same name
    number_of_equipment = it means how many product I have

    Firstly, I tried to check the file to decide if it's written in the text file. After that I find the number of equipment and I increase it and I change the line ( because I will write new ) and I write the new line

    Where is the mistake?

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: A problem when saving the data

    This seems to be very complicated for a simple problem. It appears that you are searching the record for the last space in order to isolate the Count. InStrRev is useful for such things. (It searches a String in reverse) Also, there's no need for the 'complex' If statement if you reverse the logic. As has been previously stated, you can't selectively update records, in-situ, in a sequential file, you have to replace the entire file. Opening a file for 'Append' just adds new records to the file.

    You could re-work the file format into fixed length records and use Random Access where you can select records by record number and update in-situ, but for some reason, that's not very popular these days. I guess the advent of sophisticated (and free) Databases is one of the main reasons coupled with the 'housekeeping' you'd have to do to keep the file 'tidy'. If your file is getting, or is likely to get, large, then the Database option may be the way forward.

    Here's something you can play with, which goes along the lines of previous suggestions.

    It's untested but hopefully you can follow the logic by reading the comments. I think there's more lines of comments than code
    Code:
    Dim boUpdate As Boolean
    Dim boErr As Boolean
    Dim intFile As Integer
    Dim intI As Integer
    Dim strData As String
    Dim strT As String
    Dim intN As Integer
    Dim intP As Integer
    Dim strRec() As String
    '
    ' Whatever your preamble code is
    '
    If category2 = "" And category3 = "" And category4 = "" Then
        '
        ' Allocate a File Number
        ' Open, read the entire contents of the file and close it
        ' Split the data into records
        '
        ' It is assumed that:
        '   (a) Each Record is unique
        '   (b) The Count is the last item of data in the record preceded by a space
        '       eg "Resistor 1.2K 0.5W 0.1&#37; High Stability ENR1.2.5.1.H 17"
        '       Where: 17 is the current Count
        '       and, for example, 'Resistor' is the Category, and 'ENR1.2.5.1.H' is the equipment number
        '
        intFile = FreeFile
        Open "D:\Test.txt" For Input As intFile
        strData = Input(LOF(intFile), intFile)
        Close intFile
        strRec = Split(strData, vbNewLine)
        Do
            '
            ' If this record's category and equipment number match
            ' the ones being searched for:
            ' Isolate the count, increment it and replace the record to include
            ' the new updated count
            ' Set the flag to exit the loop
            '
            If InStr(strRec(intI), category) > 0 And InStr(strRec(intI), equip_number) > 0 Then
                '
                ' Locate the last space character in the record
                ' which will be just in front of the Count
                '
                intP = InStrRev(strRec(intI), " ")
                If intP > 0 Then
                    '
                    ' Check that the Count is numeric
                    ' and increment it if it is
                    '
                    If IsNumeric(Mid$(strRec(intI), intP + 1)) Then
                        intN = CInt(Mid$(strRec(intI), intP + 1))
                        intN = intN + 1
                        '
                        ' Copy the first part of this record to a temporary string
                        ' add the new count and replace the existing Record
                        ' in the array. Display the new Count in the TextBox
                        '
                        strT = Mid$(strRec(intI), 1, intP) & CStr(intN)
                        strRec(intI) = strT
                        Text_number_of_equipment.Text = CStr(intN)
                        boUpdate = True
                    Else
                        '
                        ' The existing value is not numeric or is not present
                        ' report the fact and exit the loop
                        '
                        MsgBox "Invalid Count in Record or Count Not Found" & CStr(intI + 1)
                        boErr = True
                    End If
                Else
                    '
                    ' This record did not have an existing count
                    ' report the fact and exit the loop
                    '
                    MsgBox "Did not find a Count for Record " & CStr(intI + 1)
                    boErr = True
                End If
            End If
            intI = intI + 1
        Loop Until intI > UBound(strRec) Or boUpdate = True Or boErr = True
        '
        ' If no match was found, report the fact and finish
        '
        If intI > UBound(strRec) Then
            MsgBox "No matching record found for " & category & " " & equip_number
        Else
            '
            ' If an update was made, re-write the
            ' entire file. Tell the user it's been done
            '
            If boUpdate Then
                intFile = FreeFile
                Open "D:\test.txt" For Output As intFile
                For intI = 0 To UBound(strRec)
                    Print #intFile, strRec(intI)
                Next intI
                Close intFile
                MsgBox "File has been Updated"
            End If
        End If
    End If
    Last edited by Doogle; Jan 1st, 2012 at 04:25 AM.

  8. #8
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: A problem when saving the data

    oops

    the test for category emtyness

    if category1&category2&category3="" then ' ""&""&"" = ""

    the complex

    if
    elseif
    elseif
    end if

    should really be

    select case
    case
    case
    end case

    the latter escapes when tasks are done and is a flatter structure easier to read

    here to help
    Last edited by incidentals; Jan 1st, 2012 at 09:01 AM.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    53

    Re: A problem when saving the data

    it was about category1, category2, category3. They must have been combo1.text, combo2.text, combo3.text.

    Thanks for replies

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width