Visual Basic Problem?

2007-03-14 6:03 pm
What is the code of Save and Load file?

FOR save ... i want to save as text file...

For load .....i want to load the text file ....

回答 (2)

2007-03-15 12:56 am
✔ 最佳答案
舉一個例子,譬如你想做一個銀行戶口記錄;
資料包括Account Number、date、time、credit/debit、balance。
假設你己經在Form Set好哂D輸入介面,輸入controls包括txtAcc、dtpDate、dtpTime、optCr、optDr、txtBal。

寫入(Save)Subroutine係:
Private Sub cmdEnter_Click()
Open "c:\temp\acc.dat" For Append As #1
Write #1, txtAcc.Text, dtpDate.Value, dtpTime.Value, optCr.Value, txtBal.Text
Close #1
End Sub
只要你Click cmdEnter,個D介面control的data就寫入acc.dat檔案,如果你用notepad都開到;

讀出(Load)Subroutine係:
Private Sub cmdDisplay_Click()
Dim strAcc As String
Dim datDate As Date
Dim datTime As Time
Dim boolCredit As Boolean
Dim dblBalance As Double
Dim counter As Integer
counter = 1
Open "c:\temp\acc.dat" For Input As #1
Do While Not EOF(1)
If counter = 3 Then
'read file into valuable
Input #1, strAcc, datDate, datTime, boolCredit, dblBalance
Else
counter = counter +1
End If
Loop
Close #1
If strAcc <> "" Then
txtAcc.Text = strAcc
dtpDate.Value = datDate
dtpTime.Value = datTime
If boolCredit Then
optCr.Value = True
Else
optCr.Value = False
End If
txtBal.Text = dblBalance
End If
End Sub
上面係擷取第三個記錄,如果想擷取其他記錄,你可以改成counter = 其他數字,或者一些變數,待run time才輸入等等。
你可以跟住試試
參考: vb experience but NOT COPY AND PASTE
2007-03-17 12:09 am
TEXT file 是一種順序文件格式, 我們可以通過
open "xxxx.txt" for output as #1 '把開文件, 分配文件代號為1號
print #1,L '把字串L的內容寫入1號文件中
close #1
只要你在中間那句加入循環, 而L為陣列, 那麼就可以寫入多行, 當部寫入後, 關閉文件
寫入時如果有須要可以在後面加上 & VBLF & VBCR
如果文字是存放在textbox中, 可以更方便處理:
(如果Text1.Multiline設為True, 則可以一次過多行文章)
Open "xxxx.txt" For Output As #1
Print #1, Text1.Text;
Close #1

讀取:
如果用textbox 的話, 可以:
Open "xxxx.txt" For Input As #1
Text1.Text = Input(LOF(1), #1)
Close #1
用陣列或變量的話:
Open "xxxx.txt" For Input As #1
input #1,L '讀入一行資料
Close #1

2007-03-16 16:14:02 補充:
唔知點解我的 單引號及 雙引號變晒 " 及 &#39 , 所以使用時自己改改方法就冇錯

2007-03-30 21:25:47 補充:
根本不是發問者所問的答案, 佢要的是如何存入和讀出Text File!


收錄日期: 2021-04-12 21:12:13
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070314000051KK00672

檢視 Wayback Machine 備份