VB文本框内容保存?怎样录入数据保存后下次登陆时不丢失?
发布网友
发布时间:2022-10-17 02:20
我来回答
共1个回答
热心网友
时间:2023-11-03 23:31
1、把文本框的数据存入文本文件,登录后从文本文件读入数据再放入文本框即可;
2、如果数据较多,或者需要多个不同客户的数据,建议使用数据库;
3、保存和修改可以放到同一个按钮,因为实现的功能其实是一样的(文本文件中没数据就是保存,已经有数据就是修改了)。
下面是例子:
如果40个文本框分别是Text1、Text2……Text40,则:
Private Sub Command1_Click() '保存或修改
Dim fn As String
fn = App.Path & "\123.txt"
If Dir(fn) <> "" Then Kill fn
Open fn For Output As #1
Print #1, Text1.Text
Print #1, Text2.Text
'.....
Print #1, Text40.Text
Close #1
End Sub
Private Sub Form_Load() '读入数据
'登录验证的过程略
Dim fn As String, s() As String
fn = App.Path & "\123.txt"
Open fn For Binary As #1
s = Split(Input(LOF(1), #1), vbCrLf)
Close #1
ReDim Preserve s(39)
Text1.Text = s(0)
Text2.Text = s(1)
'.....
Text40.Text = s(39)
End Sub
如果40个文本框是控件数组Text1(0)到Text1(39),则这样:
Private Sub Command1_Click() '保存或修改
Dim fn As String, i As Integer
fn = App.Path & "\123.txt"
If Dir(fn) <> "" Then Kill fn
Open fn For Output As #1
For i = 0 To 39
Print #1, Text1(i).Text
Next
Close #1
End Sub
Private Sub Form_Load() '读入数据
'登录验证的过程略
Dim fn As String, s() As String, i As Integer
fn = App.Path & "\123.txt"
Open fn For Binary As #1
s = Split(Input(LOF(1), #1), vbCrLf)
Close #1
ReDim Preserve s(39)
For i = 0 To 39
Text1(i).Text = s(i)
Next
End Sub