📄 9-5.frm
字号:
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1
Caption = "ADO 数据访问"
ClientHeight = 3870
ClientLeft = 60
ClientTop = 345
ClientWidth = 5550
LinkTopic = "Form1"
ScaleHeight = 3870
ScaleWidth = 5550
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Find
Caption = "查询"
Height = 495
Left = 120
TabIndex = 4
Top = 3240
Width = 1215
End
Begin VB.CommandButton Add
Caption = "增加"
Height = 495
Left = 1440
TabIndex = 2
Top = 3240
Width = 1215
End
Begin VB.CommandButton Del
Caption = "删除"
Height = 495
Left = 2760
TabIndex = 1
Top = 3240
Width = 1215
End
Begin VB.CommandButton Exit
Caption = "退出"
Height = 495
Left = 4080
TabIndex = 0
Top = 3240
Width = 1215
End
Begin MSFlexGridLib.MSFlexGrid Grid1
Height = 2895
Left = 120
TabIndex = 3
Top = 120
Width = 5295
_ExtentX = 9340
_ExtentY = 5106
_Version = 393216
Rows = 1
Cols = 4
FixedCols = 0
AllowUserResizing= 1
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Cn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Private Sub Add_Click()
Dim Name As String
Dim Sex As String
Dim Age As Integer
Dim S As String
'输入姓名
'注意:字符串长度不能大于数据库字段长度
Name = Left(InputBox("输入姓名", "输入", , 1000, 300), 20)
'姓名不能为空
If Len(Name) = 0 Then
Exit Sub
End If
'输入性别
Sex = Left(InputBox("输入性别", "输入", , 1000, 300), 2)
'输入年龄
Age = Val(InputBox("输入年龄", "输入", , 1000, 300))
'以下直接执行SQL语句增加数据
'定义SQL语法串
S = "Insert into Demo values('" & Name & "', '" & Sex & "', " & Str(Age) & ")"
'执行该SQL语句
Cn.Execute S '执行SQL语句
End Sub
Private Sub Del_Click()
Dim Name As String
Name = InputBox("输入要删除的姓名", "输入", , 1000, 300)
If Not Len(Name) = 0 Then
Cn.Execute "delete * from Demo where 姓名 = '" & Name & "'"
End If
End Sub
Private Sub Exit_Click()
Unload Me
End Sub
Private Sub Find_Click()
Dim i As Long
On Error Resume Next
'将记录集指针移到开始位置
Rs.MoveFirst
If Rs.BOF And Rs.EOF Then
'无数据
Grid1.Rows = 1 '清除原来的显示
MsgBox "数据库中无可用记录", , "提示"
Exit Sub
End If
i = 1
Do Until Rs.EOF '
Grid1.Rows = i + 1 '增加一行记录
Grid1.Row = i '设置行位置
Grid1.Col = 0 '设置为第一列
Grid1.Text = " " & Str(i)
Grid1.Col = 1 '设置为第二列
Grid1.Text = " " & Rs!姓名
Grid1.Col = 2 '设置为第三列
Grid1.Text = " " & Rs!性别
Grid1.Col = 3 '设置为第四列
Grid1.Text = " " & Str(Rs!年龄)
Rs.MoveNext
i = i + 1
Loop
End Sub
Private Sub Form_Load()
Dim Source As String
Dim ActiveConnection As String
'打开连接
Cn.ConnectionString = "uid=admin;pwd=;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & App.Path & "\9.MDB"
Cn.Open
'设置连接属性
Source = "select * from demo"
Rs.Open Source, Cn '该参数也可是打开的Connection对象
'设置网格宽度
Grid1.ColWidth(0) = 600
Grid1.ColWidth(1) = 2500
Grid1.ColWidth(2) = 1000
Grid1.ColWidth(3) = 1000
'设置字段名显示
Grid1.Row = 0
Grid1.Col = 0
Grid1.Text = " 序号"
Grid1.Col = 1
Grid1.Text = " 姓名"
Grid1.Col = 2
Grid1.Text = " 性别"
Grid1.Col = 3
Grid1.Text = " 年龄"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Rs.Close
Cn.Close
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -