各位大虾,请问如何在VB里通过text文本框进行数据库的删除与添加?小弟在此拜谢
这是我写的程序,但是却无法实现我想得到的结果,请各位不吝赐教!帮我完善并修改程序。PrivateSubCommand2_Click()Adodc1.Recordset.A...
这是我写的程序,但是却无法实现我想得到的结果,请各位不吝赐教!帮我完善并修改程序。
Private Sub Command2_Click()
Adodc1.Recordset.AddNew
End Sub
Private Sub Command3_Click()
Dim ask As Integer
ask = MsgBox("删除否?", vbYesNo)
If ask = 3 Then
Adodc1.Recordset.Delete
Adodc1.Recordset.MoveNext
If Adodc1.Recordset.EOF Then Adodc1.Recordset.MoveLast
End If
Adodc1.Refresh
End Sub 展开
Private Sub Command2_Click()
Adodc1.Recordset.AddNew
End Sub
Private Sub Command3_Click()
Dim ask As Integer
ask = MsgBox("删除否?", vbYesNo)
If ask = 3 Then
Adodc1.Recordset.Delete
Adodc1.Recordset.MoveNext
If Adodc1.Recordset.EOF Then Adodc1.Recordset.MoveLast
End If
Adodc1.Refresh
End Sub 展开
2个回答
展开全部
首先,添加一个公共模块(public recs as integer
public flag as integer )
command1添加
command2修改
command3删除Private Sub Form_Load()
recs = Adodc1.Recordset.RecordCount '求出当前记录数,recs是模块变量
Text4.Text = recs
If recs > 0 Then '已有教师时
Adodc1.Recordset.MoveLast '移到最后一个记录
Adodc1.Recordset.MoveFirst '移到开头记录
End If
End Sub
Private Sub Form_Activate()
DataGrid1.SetFocus '焦点移向DataGrid1控件
Call encomm '调用本窗体自定义的encomm过程
End Sub
Private Sub command3_Click() '添加教师记录
flag = 1 'flag=1表示添加操作
Form2.Show vbModal '调用edt1窗体
End Sub
Private Sub command2_Click() '修改教师记录
flag = 2 'flag=2表示修改操作
Form2.Show vbModal '调用edt1窗体修改当前教师记录
End Sub
Private Sub Command3_Click()
If MsgBox("确认要删除吗?", vbOKCancel + vbQuestion, "信息提示") = vbOK Then
Adodc1.Recordset.Delete
If Adodc1.Recordset.EOF Then
Adodc1.Recordset.MoveLast
End If
End If
End Sub
Private Sub command5_Click() '重置,将设置条件框架中的所有输入清空
Text1.Text = "": Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub encomm()
'自定义子过程:判断Adodc1中是否存在记录,使相应命令按钮无效或有效
If recs = 0 Then
Command2.Enabled = False: Command3.Enabled = False
Else
Command2.Enabled = True: Command3.Enabled = True
End If
End Sub
Private Sub Form_Activate()
If flag = 2 Then '修改操作
Text1.Text = Trim(Form1.Adodc1.Recordset.Fields("wno")) & ""
Text2.Text = Trim(Form1.Adodc1.Recordset.Fields("wname")) & ""
Text3.Text = Trim(Form1.Adodc1.Recordset.Fields("wyears")) & ""
End If
Else
Text1 = "": Text2 = "": Text3 = ""
End Sub
Private Sub command4_Click() '取消操作
Unload Me
End Sub
Private Sub command1_Click()
If Trim(Text1.Text) = "" Then
MsgBox "加*数据项不能为空,请重新设置", vbOKOnly, "信息提示"
Text1.SetFocus
Exit Sub
End If
If flag = 1 Then '添加操作
recs = recs + 1
Form1.Adodc1.Recordset.AddNew '添加一个记录
End If
Form1.Adodc1.Recordset.Fields("wno") = Trim(Text1.Text)
Form1.Adodc1.Recordset.Fields("wname") = Trim(Text2.Text)
'End If
' If Trim(Text1(2)).Text <> "" Then
' edt.Adodc1.Recordset.Fields("tbirthday") = Format(Trim(Text1(2).Text), "yy-mm-dd")
' End If
Form1.Adodc1.Recordset.Fields("wyears") = Trim(Text3.Text)
Form1.Adodc1.Recordset.Update '保存记录
Unload Me '释放窗体
End Sub
public flag as integer )
command1添加
command2修改
command3删除Private Sub Form_Load()
recs = Adodc1.Recordset.RecordCount '求出当前记录数,recs是模块变量
Text4.Text = recs
If recs > 0 Then '已有教师时
Adodc1.Recordset.MoveLast '移到最后一个记录
Adodc1.Recordset.MoveFirst '移到开头记录
End If
End Sub
Private Sub Form_Activate()
DataGrid1.SetFocus '焦点移向DataGrid1控件
Call encomm '调用本窗体自定义的encomm过程
End Sub
Private Sub command3_Click() '添加教师记录
flag = 1 'flag=1表示添加操作
Form2.Show vbModal '调用edt1窗体
End Sub
Private Sub command2_Click() '修改教师记录
flag = 2 'flag=2表示修改操作
Form2.Show vbModal '调用edt1窗体修改当前教师记录
End Sub
Private Sub Command3_Click()
If MsgBox("确认要删除吗?", vbOKCancel + vbQuestion, "信息提示") = vbOK Then
Adodc1.Recordset.Delete
If Adodc1.Recordset.EOF Then
Adodc1.Recordset.MoveLast
End If
End If
End Sub
Private Sub command5_Click() '重置,将设置条件框架中的所有输入清空
Text1.Text = "": Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub encomm()
'自定义子过程:判断Adodc1中是否存在记录,使相应命令按钮无效或有效
If recs = 0 Then
Command2.Enabled = False: Command3.Enabled = False
Else
Command2.Enabled = True: Command3.Enabled = True
End If
End Sub
Private Sub Form_Activate()
If flag = 2 Then '修改操作
Text1.Text = Trim(Form1.Adodc1.Recordset.Fields("wno")) & ""
Text2.Text = Trim(Form1.Adodc1.Recordset.Fields("wname")) & ""
Text3.Text = Trim(Form1.Adodc1.Recordset.Fields("wyears")) & ""
End If
Else
Text1 = "": Text2 = "": Text3 = ""
End Sub
Private Sub command4_Click() '取消操作
Unload Me
End Sub
Private Sub command1_Click()
If Trim(Text1.Text) = "" Then
MsgBox "加*数据项不能为空,请重新设置", vbOKOnly, "信息提示"
Text1.SetFocus
Exit Sub
End If
If flag = 1 Then '添加操作
recs = recs + 1
Form1.Adodc1.Recordset.AddNew '添加一个记录
End If
Form1.Adodc1.Recordset.Fields("wno") = Trim(Text1.Text)
Form1.Adodc1.Recordset.Fields("wname") = Trim(Text2.Text)
'End If
' If Trim(Text1(2)).Text <> "" Then
' edt.Adodc1.Recordset.Fields("tbirthday") = Format(Trim(Text1(2).Text), "yy-mm-dd")
' End If
Form1.Adodc1.Recordset.Fields("wyears") = Trim(Text3.Text)
Form1.Adodc1.Recordset.Update '保存记录
Unload Me '释放窗体
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询