VB数据库如何用text文本框插入数据和读取显示数据
我在窗体上建立了Adodc1和DataGrid1,并已经建好他们之间的连接。数据库里面内容如下:ABCB1小明二年级100我想建立4个文本框,第一个文本text1里面如果...
我在窗体上建立了Adodc1和DataGrid1,并已经建好他们之间的连接。
数据库里面内容如下:
A B C B
1 小明 二年级 100
我想建立4个文本框,第一个文本text1里面如果输入1,则在text2.text里面显示“小明”,在text3.text里面显示“二年级”,在text3.text里面显示“100”
同时界面还有几个操作按钮,分别是“保存”,“修改”,“删除”
请问如何实现? 展开
数据库里面内容如下:
A B C B
1 小明 二年级 100
我想建立4个文本框,第一个文本text1里面如果输入1,则在text2.text里面显示“小明”,在text3.text里面显示“二年级”,在text3.text里面显示“100”
同时界面还有几个操作按钮,分别是“保存”,“修改”,“删除”
请问如何实现? 展开
展开全部
一 查阅
在Text1的KeyPress事件中加入代码,查阅时,在Text1里输入数据后,按Enter键作结束
Private Sub Text1_KeyPress(KeyAscii AsInteger)
If KeyAscii = 13 Then
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open "Provider=microsoft.jet.oledb.4.0;data source="& App.Path & "\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset, adLockOptimistic
if not rst.EOF then
rst.Movefirst
end if
For X = 1 To rst.RecordCount
If Val(Me.Text1) = rst("A") Then ‘如果字段A是文本格式,Val()函数改为Trim()
Me.Text1 = rst("A")
Me.Text2 = rst("B")
Me.text3 = rst("C")
Me.text4 = rst("D")
Exit For
EndIf
rst.MoveNext
Next X
rst.Close
Set CCN = Nothing
End If
End Sub
二保存
Private Sub 保存_Click()
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open"Provider=microsoft.jet.oledb.4.0;data source=" & App.Path &"\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset,adLockOptimistic
rst.AddNew
rst("A")= Me.Text1
rst("B")= Me.Text2 =
rst("C")=Me.Text3
rst("D")= Me.Text4
rst.update
rst.Close
Set CCN = Nothing
End Sub
三修改 删除
在DataGrid的RowColChange事件里加入
Private Sub DataGrid_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
me.text1=Datagrid.columns(0) '如果字段A是第一列,
end sub
点击修改或 删除 前,需在DataGrid表里点选需要修改或删除的记录,让该记录的第一列出现在textbox1里
Private Sub 修改_Click()
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open"Provider=microsoft.jet.oledb.4.0;data source=" & App.Path &"\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset,adLockOptimistic
if not rst.EOF then
rst.Movefirst
end if
For X = 1 To rst.RecordCount
If Val(Me.Text1) = rst("A") Then '如果字段A是文本格式,Val()函数改为Trim()
rst("A")= Me.Text1
rst("B")= Me.Text2 =
rst("C")=Me.Text3
rst("D")= Me.Text4
rst.update
ExitFor
EndIf
rst.MoveNext
Next X
rst.Close
Set CCN = Nothing
End Sub
Private Sub 删除_Click()
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open"Provider=microsoft.jet.oledb.4.0;data source=" & App.Path &"\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset,adLockOptimistic
if not rst.EOF then
rst.Movefirst
end if
For X = 1 To rst.RecordCount
If Val(Me.Text1) = rst("A") Then '如果字段A是文本格式,Val()函数改为Trim()
rst.delete
Exit For
EndIf
rst.MoveNext
Next X
rst.Close
Set CCN = Nothing
End Sub
在Text1的KeyPress事件中加入代码,查阅时,在Text1里输入数据后,按Enter键作结束
Private Sub Text1_KeyPress(KeyAscii AsInteger)
If KeyAscii = 13 Then
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open "Provider=microsoft.jet.oledb.4.0;data source="& App.Path & "\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset, adLockOptimistic
if not rst.EOF then
rst.Movefirst
end if
For X = 1 To rst.RecordCount
If Val(Me.Text1) = rst("A") Then ‘如果字段A是文本格式,Val()函数改为Trim()
Me.Text1 = rst("A")
Me.Text2 = rst("B")
Me.text3 = rst("C")
Me.text4 = rst("D")
Exit For
EndIf
rst.MoveNext
Next X
rst.Close
Set CCN = Nothing
End If
End Sub
二保存
Private Sub 保存_Click()
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open"Provider=microsoft.jet.oledb.4.0;data source=" & App.Path &"\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset,adLockOptimistic
rst.AddNew
rst("A")= Me.Text1
rst("B")= Me.Text2 =
rst("C")=Me.Text3
rst("D")= Me.Text4
rst.update
rst.Close
Set CCN = Nothing
End Sub
三修改 删除
在DataGrid的RowColChange事件里加入
Private Sub DataGrid_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
me.text1=Datagrid.columns(0) '如果字段A是第一列,
end sub
点击修改或 删除 前,需在DataGrid表里点选需要修改或删除的记录,让该记录的第一列出现在textbox1里
Private Sub 修改_Click()
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open"Provider=microsoft.jet.oledb.4.0;data source=" & App.Path &"\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset,adLockOptimistic
if not rst.EOF then
rst.Movefirst
end if
For X = 1 To rst.RecordCount
If Val(Me.Text1) = rst("A") Then '如果字段A是文本格式,Val()函数改为Trim()
rst("A")= Me.Text1
rst("B")= Me.Text2 =
rst("C")=Me.Text3
rst("D")= Me.Text4
rst.update
ExitFor
EndIf
rst.MoveNext
Next X
rst.Close
Set CCN = Nothing
End Sub
Private Sub 删除_Click()
Dim X As Integer
Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQ2 As String
CNN.Open"Provider=microsoft.jet.oledb.4.0;data source=" & App.Path &"\数据库名.mdb"
SQ2 = "select * from 表名"
rst.Open SQ2, CNN, adOpenKeyset,adLockOptimistic
if not rst.EOF then
rst.Movefirst
end if
For X = 1 To rst.RecordCount
If Val(Me.Text1) = rst("A") Then '如果字段A是文本格式,Val()函数改为Trim()
rst.delete
Exit For
EndIf
rst.MoveNext
Next X
rst.Close
Set CCN = Nothing
End Sub
展开全部
文本框本来就具有这个功能,称为数据绑定控件:
在设计时,在它的属性窗口设置它的绑定对象就可以了:
DataSource 属性 选ADODC1
DataField 属性 选对应的字段名
然后加一个按钮:
按钮代码如下:
如果字段A是数值类型
Private Sub Command1_Click()
Adodc1.RecordSource = "Select * from 数据表名 where A=" & Text1.Text
Adodc1.Refresh
End Sub
如果字段A是文本类型
Private Sub Command1_Click()
Adodc1.RecordSource = "Select * from 数据表名 where A='" & Text1.Text & "'"
Adodc1.Refresh
End Sub
在设计时,在它的属性窗口设置它的绑定对象就可以了:
DataSource 属性 选ADODC1
DataField 属性 选对应的字段名
然后加一个按钮:
按钮代码如下:
如果字段A是数值类型
Private Sub Command1_Click()
Adodc1.RecordSource = "Select * from 数据表名 where A=" & Text1.Text
Adodc1.Refresh
End Sub
如果字段A是文本类型
Private Sub Command1_Click()
Adodc1.RecordSource = "Select * from 数据表名 where A='" & Text1.Text & "'"
Adodc1.Refresh
End Sub
追问
能不能详细点,按照我说的那样如何实现,谢谢了
追答
给你做一个,你自己看好了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询