我是这个visual basic 6.0的新手,你能告诉我这个会发生什么吗?
我的表单上有1个Microsoft ADO Data Control 6.0(OLEDB),我将其连接到我的数据库(* .mdb),并且我的adodc1属性的记录源使用以下连接:SELECT * FROM UsersDB' This是我的命令文本sql
这是代码:
Dim str As String
Private Sub Form_Load()
LoadEmployee
End Sub
Private Sub btnSave_Click()
If btnSave.Caption = "Save" Then
Adodc1.RecordSource = "SELECT * FROM UsersDB Where Name='" & txtName.Text & "'"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
MsgBox "This name already exists. Please chose another."
Exit Sub
End If
On Error Resume Next
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("Name") = txtName.Text
Adodc1.Recordset.Fields("Date_of_Birth") = txtDOB.Text
Adodc1.Recordset.Fields("Age") = txtAge.Text
Adodc1.Recordset.Fields("Gender") = cmbGender.Text
Adodc1.Recordset.Fields("Address") = txtAddress.Text
Adodc1.Recordset.Fields("Father's_Name") = txtFathersname.Text
Adodc1.Recordset.Fields("Mother's_Name") = txtMothersname.Text
Adodc1.Recordset.Fields("Contact_Number") = txtContactNumber.Text
Adodc1.Recordset.Fields("Hobbies") = cmbHobbies.Text
Adodc1.Recordset.Fields("Picture") = str
Adodc1.Recordset.Fields("Usertype") = cmbUsertype.Text
Adodc1.Recordset.Fields("Username") = txtUsername.Text
Adodc1.Recordset.Fields("Password") = txtPassword.Text
Adodc1.Recordset.Update
MsgBox "Successfully added to the database", vbInformation + vbOKOnly, "Success"
btnSave.Caption = "Add New"
ElseIf btnSave.Caption = "Add New" Then
txtName.Text = ""
txtDOB.Text = ""
txtAge.Text = ""
cmbGender.Text = ""
txtAddress.Text = ""
txtFathersname.Text = ""
txtMothersname.Text = ""
txtContactNumber.Text = ""
cmbHobbies.Text = ""
cmbUsertype.Text = ""
txtUsername.Text = ""
txtPassword.Text = ""
Image2.Picture = Nothing
btnSave.Caption = "Save"
End If
End Sub
这里发生的是,在运行程序时,我加载了当前连接到数据库的所有信息(通过表单load())。在我保存之后,数据网格中的所有数据都消失了,唯一剩下的就是我当前保存的新保存数据。如何在datagrid上检索所有数据以及我新保存的数据?
这是我的工作:
答案 0 :(得分:0)
Private Sub btnSave_Click()
If btnSave.Caption = "Save" Then
Set DataGrid1.DataSource = Nothing
Adodc1.RecordSource = "SELECT * FROM UsersDB Where Name='" & txtName.Text & "'"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
MsgBox "This name already exists. Please chose another."
Exit Sub
End If
On Error Resume Next
Adodc1.RecordSource = "SELECT * FROM UsersDB"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
DataGrid1.Refresh
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("Name") = txtName.Text
Adodc1.Recordset.Fields("Date_of_Birth") = txtDOB.Text
Adodc1.Recordset.Fields("Age") = txtAge.Text
Adodc1.Recordset.Fields("Gender") = cmbGender.Text
Adodc1.Recordset.Fields("Address") = txtAddress.Text
Adodc1.Recordset.Fields("Father's_Name") = txtFathersname.Text
Adodc1.Recordset.Fields("Mother's_Name") = txtMothersname.Text
Adodc1.Recordset.Fields("Contact_Number") = txtContactNumber.Text
Adodc1.Recordset.Fields("Hobbies") = cmbHobbies.Text
Adodc1.Recordset.Fields("Picture") = str
Adodc1.Recordset.Fields("Usertype") = cmbUsertype.Text
Adodc1.Recordset.Fields("Username") = txtUsername.Text
Adodc1.Recordset.Fields("Password") = txtPassword.Text
Adodc1.Recordset.Update
MsgBox "Successfully added to the database", vbInformation + vbOKOnly, "Success"
btnSave.Caption = "Add New"
ElseIf btnSave.Caption = "Add New" Then
txtName.Text = ""
txtDOB.Text = ""
txtAge.Text = ""
cmbGender.Text = ""
txtAddress.Text = ""
txtFathersname.Text = ""
txtMothersname.Text = ""
txtContactNumber.Text = ""
cmbHobbies.Text = ""
cmbUsertype.Text = ""
txtUsername.Text = ""
txtPassword.Text = ""
Image2.Picture = Nothing
btnSave.Caption = "Save"
End If
End Sub