Private Sub cmdok_Click()
If txtusername.Text = "" Then
MsgBox "请输入用户名称!", 48, "警告"
Exit Sub
txtusername.SetFocus
Else
If Adodc1.Recordset.Fields(0) = txtusername.Text Then
MsgBox "用户名以存在,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtusername.SetFocus
txtusername.Text = ""
password1.Text = ""
password2.Text = ""
Exit Sub
Else
Adodc1.Recordset.MoveNext End If End If
If txtpassword1.Text <> txtpassword2.Text Then
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbExclamation, "警告"
txtpassword1.SetFocus
txtpassword1.Text = ""
txtpassword2.Text = ""
Exit Sub
Else
Adodc1.Recordset.AddNew
Adodc1.Refresh
Unload Me MsgBox "添加用户成功!", vbOKOnly + vbExclamation, "添加用户"
End If
End Sub
为什么添加不到库里面?
If txtusername.Text = "" Then
MsgBox "请输入用户名称!", 48, "警告"
Exit Sub
txtusername.SetFocus
Else
If Adodc1.Recordset.Fields(0) = txtusername.Text Then
MsgBox "用户名以存在,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtusername.SetFocus
txtusername.Text = ""
password1.Text = ""
password2.Text = ""
Exit Sub
Else
Adodc1.Recordset.MoveNext End If End If
If txtpassword1.Text <> txtpassword2.Text Then
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbExclamation, "警告"
txtpassword1.SetFocus
txtpassword1.Text = ""
txtpassword2.Text = ""
Exit Sub
Else
Adodc1.Recordset.AddNew
Adodc1.Refresh
Unload Me MsgBox "添加用户成功!", vbOKOnly + vbExclamation, "添加用户"
End If
End Sub
为什么添加不到库里面?
Adodc1.Recordset.AddNew位置呢??????Adodc1.Refresh
2.调用addnew方法后要用update方法更新才能添加到数据库
Adodc1.Recordset("username")=Trim(txtusername.text)
Adodc1.Recordset.Update还有你的if嵌套有问题你自己再Debug下
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbExclamation, "警告"
txtpassword1.SetFocus
txtpassword1.Text = ""
txtpassword2.Text = ""
Exit Sub
Else
Adodc1.Recordset.AddNew
Adodc1.Recordset.fields("用户名") = trim$(txtUser.text)
Adodc1.Recordset.fields("用户密码") = trim$(txtPassword1.text)
Adodc1.Recordset.update
Adodc1.Refresh MsgBox "添加用户成功!", vbOKOnly + vbExclamation, "添加用户"
Unload Me End If
End Sub
Adodc1.Recordset.update
Adodc1.Refresh
不能少呀。
If txtusername.Text = "" Then
MsgBox "请输入用户名称!", 48, "警告"
Exit Sub
txtusername.SetFocus
Else
If Adodc1.Recordset.Fields(0) = txtusername.Text Then 'Fields(0)少了.Value
MsgBox "用户名以存在,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtusername.SetFocus
txtusername.Text = ""
password1.Text = ""
password2.Text = ""
Exit Sub
Else
Adodc1.Recordset.MoveNext
End If
End If
If txtpassword1.Text <> txtpassword2.Text Then
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbExclamation, "警告"
txtpassword1.SetFocus
txtpassword1.Text = ""
txtpassword2.Text = ""
Exit Sub
Else
Adodc1.Recordset.AddNew '这里没有给字段赋值。
Adodc1.Recordset.Fields(0).Value=txtusername.Text '我加入,为用户名字段赋值
Adodc1.Recordset.Fields(1).Value=txtpassword1.Text '我加入,为密码字段赋值
Adodc1.Recordset.Update '这行不能少,用来更新数据库
Adodc1.Refresh
Unload Me MsgBox "添加用户成功!", vbOKOnly + vbExclamation, "添加用户"
End If
End Sub另:你尚须养成良好的编程习惯,代码组织显得很乱。注意缩行!