Age As String * 3 End Type
Private Sub Command1_Click() '\添加记录\命令按钮
Dim s As Student Dim I As Integer
Open \打开文件 I = LOF(1)/Len(s) '计算记录号 s.Name = Text1.Text s.Age = Text2.Text
Put #1, I + 1,s '写入数据,记录号为I+1
Text1.Text =\
Text2.Text =\清空Text1和Text2 Text1.SetFocus '置Text1为焦点。 Close '关闭文件 End Sub
Private Sub Command2_Click() '退出命令按钮 End End Sub 【例10-4】
Private Sub Command1_Click()
Dim fso As New FileSystemObject, drv As Drive, s As String ‘定义一个FSO对象
Set drv = fso.GetDrive(fso.GetDriveName(\ ‘取出C盘当前路径 s = \
s = s & drv.VolumeName & vbCrLf ‘取出C盘的卷标 s = s & \
‘取出C盘总共的磁盘空间,并换算成KB s = s & \
s = s & \
‘取出C盘剩余的磁盘空间,并换算成KB s = s & \ MsgBox sEnd
Sub
【例10-5】
Private Sub Command1_Click()
Set fso = CreateObject(\
'Set f1 = fso.CreateTextFile(\使用CreateTextFile创建文件
'Set f1 = fso.OpenTextFile(\使用OpenTextFile创建文件
fso.CreateTextFile (\
36
Set ft = fso.GetFile(\
Set f1 = ft.OpenAsTextStream(ForWriting, True) '以上三条语句使用
OpenAsTextStream创建文件
f1.WriteLine (\写入一行带有换行符的文本 f1.WriteBlankLines (3) '向文件中写入三个换行符。 f1.Write (3+2) '写入表达式3+2的值5 f1.Close
End Sub
【例10-6】
Private Sub Command2_Click()
Set fso = CreateObject(\‘定义FSO对象 Set f1 = fso.OpenTextFile(\‘打开文件用于读 s1 = f1.ReadAll ‘将文件中内容一次性全部
读出
Text1.Text = s1 f1.Close End Sub
也可使用ReadLine方法,程序如下: Private Sub Command2_Click()
Set fso = CreateObject(\Set f1 = fso.OpenTextFile(\s1 = \
Do While Not f1.AtEndOfStream s1 = s1 & f1.ReadLine + vbCrLf Loop
Text1.Text = s1 f1.Close End Sub 实训
Dim sn(25) As Integer
Private Sub Command1_Click() Open \Do While Not EOF(1) s1 = Input$(1, #1) For i = 0 To 25
If UCase$(s1) = Chr$(65 + i) Then sn(i) = sn(i) + 1 End If Next i Loop Close #1
37
For i = 0 To 25
Text1.Text = Text1.Text + \字母\+ Chr$(65 + i) + \的个数为:\+ Str(sn(i)) +
vbCrLf
Next i End Sub
Private Sub Command2_Click()
Open \For i = 0 To 25
Print #1, vbCrLf + \字母\的个数为:\Next i Close #1
End Sub
第11章 课前体验
在窗体的Form_Load()事件、5个命令按钮的Click()事件中分别输入以下程序:
(1)Form_Load() 事件 Private Sub Form_Load()
cmdUpdata.Enabled = False '使更新按钮不可用 End Sub (2)“更新”按钮
Private Sub cmdAdd_Click()
Data1.Recordset.AddNew '添加新记录
cmdUpdata.Enabled = True '激活更新按钮 End Sub (3)“删除”按钮
Private Sub cmdDel_Click()
Data1.Recordset.Delete '删除当前记录 End Sub (4)“修改”按钮
Private Sub cmdEdit_Click()
Data1.Recordset.Edit '开始修改记录
cmdUpdata.Enabled = True '激活更新按钮 End Sub (5)“退出”按钮
Private Sub cmdExit_Click()
End '退出程序 End Sub (6)“添加”按钮
Private Sub cmdUpdata_Click()
Data1.Recordset.Update '更新记录
cmdUpdata.Enabled = False '使更新按钮不可用
38
End Sub 实训
(3)编写程序:对各命令按钮的Click()事件编程如下 ①“增加职工”按钮的Click()事件 Private Sub Command1_Click()
If Command1.Caption = \增加职工\ Data1.Recordset.AddNew Command1.Caption = \确定\ Command2.Caption = \取消\ Command3.Enabled = False
ElseIf Command1.Caption = \确定\
Text9.Text = Val(Text2) + Val(Text5) + Val(Text8) - Val(Text3) - Val(Text6) Data1.Recordset.Update
Command1.Caption = \增加职工\ Command2.Caption = \删除职工\ Command3.Enabled = True End If End Sub
②“删除职工”按钮的Click()事件: Private Sub Command2_Click()
If Command2.Caption = \删除职工\ Data1.Recordset.Delete
If Not Data1.Recordset.EOF Then Data1.Recordset.MoveNext
ElseIf Not Data1.Recordset.BOF Then Data1.Recordset.MovePrevious Else
MsgBox (\这是最后一条记录\ End If
ElseIf Command2.Caption = \取消\ Data1.Recordset.CancelUpdate End If End Sub
③“修改数据”按钮的Click()事件 Private Sub Command3_Click() Data1.Recordset.Edit
Command1.Caption = \确定\ Command2.Caption = \取消\ Command3.Enabled = False End Sub
④“退出”按钮的Click()事件 Private Sub Command4_Click() End End Sub
39
第12章 12.4.2
Dim trytimes As Integer '尝试登陆次数 ‘命令按钮“确定”的Click事件 Private Sub cmdCancel_Click()
If MsgBox(\你选择了退出登陆,是否退出?\
vbYesNo + vbInformation, \用户登陆\ End Else
Exit Sub End If End Sub
‘命令按钮“确定”的Click事件 Private Sub cmdOK_Click()
Dim sName As String, sPas As String
Dim mrs As ADODB.Recordset, strSQL As String
sName = Trim(txtUserName.Text): sPas = Trim(txtPassword.Text) '数据有效性检查 If sName = \
MsgBox \请输入用户名!\用户登陆验证\ txtUserName.SetFocus
‘若用户名为空,则提示输入用户名,并将定位在用户名文本框 Exit Sub End If
If sPas = \
MsgBox \请输入密码!\用户登陆验证\ txtPassword.SetFocus
‘若用户密码为空,则提示输入密码,并将定位在密码文本框 Exit Sub End If
'检查用户名是否正确
strSQL = \用户表 where 用户名='\ Set mrs = conn.Execute(strSQL) If mrs.EOF = True Then
MsgBox \用户名不存在!\用户登陆验证\ try_times = try_times + 1 If try_times >= 3 Then
MsgBox \您已经三次尝试进入本系统,均不成功,系统将关闭!\ vbCritical, \用户登陆验证\ End Else
txtUserName.SetFocus txtUserName.SelStart = 0
txtUserName.SelLength = Len(txtUserName.Text)
40