![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
Over writing text in a .txt file?
hey, could someone please tell me how i can overwrite the text.
i have some points code and it reads and writes from/to a textfile i need to make the write function so it finds the old customer id and points and replaces it with a new one ^^;; heres the first part that hasnt been tested...cuz obviously i dont have that function xP feel free to tell me what else is wrong or could be better ^^ Dim cId As Integer Private Sub Combo12_Change() 'Customer Id Combo Combo12.SetFocus cId = Combo12.Text Text15.Text = LoadScore(cId) End Sub Private Sub Command17_Click()
'Increase Button
Text15.SetFocus
If (cId = "") Then
MsgBox ("Specify a customer ID first")
GoTo SubEnd
ElseIf (Len(cId) > 0) Then
Dim tScore As String
tScore = LoadScore(cId)
Text15.Text = tScore
If (tScore < 500) Then
Randomize (Timer)
Dim Rand1 As String
Rand1 = Rnd(1) * 5
Text15.Text = Rand1 + tScore
WriteScore cId, tScore, Rand1
ElseIf (tScore < 1000) And (tScore >= 500) Then
Dim Rand2 As String
Rand2 = Rnd(1) * 10
Text15.Text = Rand2 + tScore
WriteScore cId, tScore, Rand2
Else
Dim Rand1 As String
Rand3 = Rnd(1) * 15
Text15.Text = Rand3 + tScore
WriteScore cId, tScore, Rand3
End If
End If
SubEnd:
End SubPrivate Sub Command18_Click()
'Load Button
If (cId = "") Then
MsgBox ("Specify a customer ID first")
GoTo SubEnd
ElseIf (Len(cId) > 0) Then
Text15.SetFocus
Text15 = LoadScore(cId)
End If
SubEnd:
End SubFunction LoadScore(CustomerID As Integer) As Integer
'Function That Returns The Current Score Of The ID Specified
On Error GoTo ReturnNull
Dim fFile As Integer
fFile = FreeFile
Open "D:\CustomerScore.txt" For Input As #fFile
Dim Lines As Integer, cLine As Integer, lString As String
Lines = LOF(fFile)
cLine = 1
While (cLine <= Lines)
Line Input #fFile, lString
If (lString Like CustomerID & "*") Then
LoadScore = Replace$(lString, Left$(lString, 3), "", 1, -1, vbTextCompare)
GoTo FuncEnd
End If
Wend
ReturnNull:
LoadScore = 0
FuncEnd:
End FunctionPlease help thanks alot - cloud |
|
|
|
|
|
#2 |
|
Expert Programmer
|
I think you can just use Print: depending how you've got it set up it will either append or overwrite the data at the current position in the file: there doesn't seem to be any equivalent of line input to overwrite a specific line.
Random access, or binary mode would work, which would be more flexible if a little more complicated (remember to use StrConv(Data, vbUnicode) for the latter) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|