代码如下(仅供参考):
在写如之前,首先要确定输入数据库中的各个数据不能为空。
Option Explicit '———————————————————————— '窗体:添加用户窗体 '说明:查询每个文本框不为空,两次密码相同,最后把添加信息写入USER—info 表 中 '———————————————————————— Private Sub cmdOk_Click() Dim mrc As ADODB.Recordset Dim MsgText As String Dim txtsql As String Dim time1 As String Dim date1 As String '各个文本框不能空着,且密码和确认密码值要想同 If Not Testtxt(Text1.Text) Then MsgBox "请输入用户名!",vbOKOnly + vbExclamation,"警告" Text1.SetFocus Exit Sub End If If Not Testtxt(Text2.Text) Then MsgBox "请返回上一个界面选择用户级别!","警告" frmadduser.Show Unload Me Exit Sub End If If Not Testtxt(Text3.Text) Then MsgBox "请输入用户名!","警告" Text3.SetFocus Exit Sub End If If Not Testtxt(Text4.Text) Then MsgBox "密码不能为空!","警告" Text3.SetFocus Exit Sub End If If Not Testtxt(Text5.Text) Then MsgBox "请确认密码!","警告" Text5.SetFocus Exit Sub End If If Text4.Text <> Text5.Text Then MsgBox "两次输入密码不一致,请重新输入!","警告" End If Set mrc = Executesql(txtsql,MsgText) txtsql = "select * from User_Info" Set mrc = Executesql(txtsql,MsgText) mrc.AddNew '在表中建立新的列,写入添加数据 mrc.Fields(0) = Trim(Text1.Text) mrc.Fields(1) = Trim(Text4.Text) mrc.Fields(2) = Trim(Text2.Text) mrc.Fields(3) = Trim(Text3.Text) mrc.Fields(4) = UserName mrc.Update MsgBox "添加用户成功成功!","警告" mrc.Close Me.Hide End Sub Private Sub Form_Load() Text2.Text = level '在上一个窗体中设置用户级别 End Sub