|
- Private Sub 添加记录_Click()
- Dim rst As ADODB.Recordset
- Dim i&, SQL$, temp$
- For Each c In Array(1, 2, 7, 12) '工号,姓名,部门,身份证号,否则提示!
- If Me.Controls("TextBox" & c).Text = "" Then
- MsgBox rs.Fields(c - 1).Name & "不能为空!", vbCritical
- Me.Controls("TextBox" & c).SetFocus
- Exit Sub
- End If
- Next
- temp = " 工号='" & TextBox1.Text & "' and 姓名='" & TextBox2.Text & "'" '工号和姓名都相同的视为同一个人
- On Error Resume Next
- Set rst = New ADODB.Recordset
- SQL = "select * from B2A员工档案 where " & temp
- rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic
- If rst.RecordCount > 0 Then
- MsgBox "B2A员工档案中已经存在该记录!", vbInformation, "添加失败"
- Set rst = Nothing
- Exit Sub
- End If
- Set rst = New ADODB.Recordset
- SQL = "select * from B2A员工档案"
- rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic
- '开始添加数据
- With rst
- .AddNew '添加各个字段的数据
- For i = 0 To rst.Fields.Count - 2 'ID号 在数据库中自动增加
- .Fields(i) = Me.Controls("TextBox" & i + 1).Text
- Next i
- .Update '更新数据表
- End With
- Set rst = Nothing
- If 模糊查询.Text = "" Then Call 显示数据(SQL) Else 模糊查询.Text = "" '刷新ListView1数据
- MsgBox "已经将 新人员数据添加到 B2A员工档案!", vbInformation, "添加记录"
- End Sub
复制代码 |
|