📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "添加新的记录"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 5460
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 5460
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton Command1
Caption = "添加新的记录"
Height = 615
Left = 1560
TabIndex = 0
Top = 960
Width = 2415
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public Sub AddNewX()
Dim cnn1 As ADODB.Connection
Dim rstEmployees As ADODB.Recordset
Dim strCnn As String
Dim strID As String
Dim strFirstName As String
Dim strLastName As String
Dim booRecordAdded As Boolean
'打开连接
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;Data Source=mynetserver;Initial Catalog=pubs;User Id=sa;Password=12345678;"
cnn1.Open strCnn
'打开雇员表,生成记录集
Set rstEmployees = New ADODB.Recordset
rstEmployees.CursorType = adOpenKeyset
rstEmployees.LockType = adLockOptimistic
rstEmployees.Open "employee", cnn1, , , adCmdTable
' 从用户获取数据,雇员 ID 的格式必须为:名、中间名和姓的三个首字母,五位数字,以及性别标识 M 或 F
'例如,Bill Sornsin 的雇员 ID 为B-S55555M
strID = Trim(InputBox("输入雇员ID:"))
strFirstName = Trim(InputBox("输入首名称:"))
strLastName = Trim(InputBox("输入尾名称:"))
'判断是否正确输入
If (strID <> "") And (strFirstName <> "") _
And (strLastName <> "") Then
'调用AddNew方法向记录集中添加新记录
rstEmployees.AddNew
rstEmployees!emp_id = strID
rstEmployees!fname = strFirstName
rstEmployees!lname = strLastName
'调用Update方法向数据库中提交新记录
rstEmployees.Update
booRecordAdded = True
'显示新添加的数据
MsgBox "数据库中的新记录: " & rstEmployees!emp_id & " " & _
rstEmployees!fname & " " & rstEmployees!lname
Else
MsgBox "请输入雇员ID,首名称,尾名称."
End If
rstEmployees.Close
cnn1.Close
End Sub
Private Sub Command1_Click()
Call AddNewX
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -