|
-
Dec 31st, 2011, 09:35 AM
#1
Thread Starter
Member
[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
-
Dec 31st, 2011, 10:09 AM
#2
Frenzied Member
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.
-
Dec 31st, 2011, 10:37 AM
#3
Frenzied Member
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
-
Dec 31st, 2011, 04:24 PM
#4
Thread Starter
Member
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?
-
Dec 31st, 2011, 04:27 PM
#5
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.
-
Jan 1st, 2012, 02:40 AM
#6
Thread Starter
Member
Re: A problem when saving the data
I couldn't find a solution that's why I copy the code
vb Code:
If category2 = "" And category3 = "" And category4 = "" Then
Open "D:\\test.txt" For Input As #1
Do While Not (EOF(1))
Input #1, read
a = Mid(read, 1, M)
M = Len(read)
If InStr(1, read, category, 1) <= 0 Or InStr(1, read, equip_number) <= 0 Then
MsgBox "There is no record like this, please check the number and the name of category"
Exit Sub
ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 1, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
w = Right(read, 1)
Exit Do
ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 2, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
w = Right(read, 2)
Exit Do
ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 3, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
w = Right(read, 3)
Exit Do
ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 4, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
w = Right(read, 4)
Exit Do
ElseIf InStr(1, read, category, 1) > 0 And Mid(read, M - 5, 1) = " " And InStr(1, read, equip_number, 1) > 0 Then
w = Right(read, 5)
Exit Do
End If
Loop
Close #1
a = " "
Text_number_of_equipment.Text = w + 1
End If
Open "D:\\test.txt" For Append As #1
Print #1, category, equip_number, w
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?
-
Jan 1st, 2012, 04:10 AM
#7
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% 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.
-
Jan 1st, 2012, 08:53 AM
#8
Frenzied Member
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.
-
Jan 3rd, 2012, 04:10 AM
#9
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|