VB中利用数据库做查询功能,在text1中输入姓名,点击查询后,其他信息如职称工资等依次在text2text3中显示
4个回答
展开全部
一段完整代码:
private sub command1_click
if text1 = "" then msgbox "请输入姓名!", 48: exit sub
text2 = ""
text3 = ""
dim mycon1 as new adodb.connection, rs1 as new adodb.recordset
mycon1.open "provider=microsoft.jet.oledb.4.0;data source=" & app.path & "\人事管理.mbd"
set rs1.activeconnection = mycon1
rs1.open "select * from 人事管理表 where 姓名='" & replace(trim(text1), "'", "") & "'"
if not rs1.eof then
text2 = rs1.fields("职称")
text3 = rs1.fields("工资")
end if
rs1.close
set rs1 = nothing
mycon1.close
set mycon1 = nothing
end sub
以上代码需要添加DAO引用,如果不引用,将:
dim mycon1 as new adodb.connection, rs1 as new adodb.recordset
修改为:
Set mycon1 = CreateObject("adodb.connection")
Set rs1 = CreateObject("adodb.recordset")
private sub command1_click
if text1 = "" then msgbox "请输入姓名!", 48: exit sub
text2 = ""
text3 = ""
dim mycon1 as new adodb.connection, rs1 as new adodb.recordset
mycon1.open "provider=microsoft.jet.oledb.4.0;data source=" & app.path & "\人事管理.mbd"
set rs1.activeconnection = mycon1
rs1.open "select * from 人事管理表 where 姓名='" & replace(trim(text1), "'", "") & "'"
if not rs1.eof then
text2 = rs1.fields("职称")
text3 = rs1.fields("工资")
end if
rs1.close
set rs1 = nothing
mycon1.close
set mycon1 = nothing
end sub
以上代码需要添加DAO引用,如果不引用,将:
dim mycon1 as new adodb.connection, rs1 as new adodb.recordset
修改为:
Set mycon1 = CreateObject("adodb.connection")
Set rs1 = CreateObject("adodb.recordset")
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要现在vb引用中添加Microsoft ActiveX Data Objects,2.0的就可以当然高版本的也行。
Public Function GetCnAccess(DbFile As String, Password As String) As ADODB.Connection
On Error GoTo Errcl
Set GetCnAccess = New ADODB.Connection
GetCnAccess.ConnectionTimeout = 30
GetCnAccess.CommandTimeout = 30
GetCnAccess.CursorLocation = adUseClient
GetCnAccess.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database password=" & Password & ";Data Source=" & DbFile & "; Persist Security Info=False"
GetCnAccess.Open
Errcl:
Select Case Err
Case 0
Case -2147217843
Set GetCnAccess = Nothing
MsgBox "密码错误,请重新录入密码!", vbExclamation, "提示"
Case Else
Set GetCnAccess = Nothing
MsgBox Error, vbExclamation, "提示"
End Select
End Function
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim rst As New ADODB.Recordset
Set cn = GetCnAccess(App.Path & "\admin.mdb", "")
If cn Is Nothing Then Exit Sub
rst.Open "select * from aaa where 姓名='" & Trim(Text1) & "'", cn, 3, 1
If rst.BOF And rst.EOF Then
rst.Close
cn.Close
Set cn = Nothing
MsgBox "该姓名不存在!", vbExclamation, "提示"
Exit Sub
End If
If rst.RecordCount > 1 Then
MsgBox "姓名有重复,载入前面的记录!", vbExclamation, "提示"
End If
Text2 = rst!电话
Text3 = rst!地址
rst.Close
cn.Close
Set cn = Nothing
End Sub
Public Function GetCnAccess(DbFile As String, Password As String) As ADODB.Connection
On Error GoTo Errcl
Set GetCnAccess = New ADODB.Connection
GetCnAccess.ConnectionTimeout = 30
GetCnAccess.CommandTimeout = 30
GetCnAccess.CursorLocation = adUseClient
GetCnAccess.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database password=" & Password & ";Data Source=" & DbFile & "; Persist Security Info=False"
GetCnAccess.Open
Errcl:
Select Case Err
Case 0
Case -2147217843
Set GetCnAccess = Nothing
MsgBox "密码错误,请重新录入密码!", vbExclamation, "提示"
Case Else
Set GetCnAccess = Nothing
MsgBox Error, vbExclamation, "提示"
End Select
End Function
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim rst As New ADODB.Recordset
Set cn = GetCnAccess(App.Path & "\admin.mdb", "")
If cn Is Nothing Then Exit Sub
rst.Open "select * from aaa where 姓名='" & Trim(Text1) & "'", cn, 3, 1
If rst.BOF And rst.EOF Then
rst.Close
cn.Close
Set cn = Nothing
MsgBox "该姓名不存在!", vbExclamation, "提示"
Exit Sub
End If
If rst.RecordCount > 1 Then
MsgBox "姓名有重复,载入前面的记录!", vbExclamation, "提示"
End If
Text2 = rst!电话
Text3 = rst!地址
rst.Close
cn.Close
Set cn = Nothing
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1 首先你有数据库连接
2 dim sqlstr as string
Sqlstr = "select * from 人事管理表 where 姓名=“&text1.text
Set Rec_Query = DE.DataConnect.Execute(Sqlstr)
With Rec_Query
text2.text=Trim(.Fields("职称") & "")
text2.text=Trim(.Fields("工资") & "")
end with
2 dim sqlstr as string
Sqlstr = "select * from 人事管理表 where 姓名=“&text1.text
Set Rec_Query = DE.DataConnect.Execute(Sqlstr)
With Rec_Query
text2.text=Trim(.Fields("职称") & "")
text2.text=Trim(.Fields("工资") & "")
end with
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加个adodc控件
Dim adocn As New ADODB.Connection
Private Sub Command1_Click()
If Text1.Text = "" Then
Else
Dim rs As New ADODB.Recordset
Dim strSql As String
strSql = "select * from 人事管理表 where 姓名=" & Trim(Text1.Text)
adocn.Open
rs.Open strSql, adocn, 3, 3
If rs.EOF And rs.BOF Then
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Set rs = Nothing
adocn.Close
MsgBox "没有查询到符合您要求的信息!", vbCritical + vbOKOnly, "信息"
Exit Sub
Else
Text2.Text = rs.Fields("职称")
Text3.Text = rs.Fields("工资")
Set rs = Nothing
adocn.Close
End If
End If
End Sub
Private Sub Form_Load()
adocn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\人事管理.mdb;Persist Security Info=False"
End Sub
Dim adocn As New ADODB.Connection
Private Sub Command1_Click()
If Text1.Text = "" Then
Else
Dim rs As New ADODB.Recordset
Dim strSql As String
strSql = "select * from 人事管理表 where 姓名=" & Trim(Text1.Text)
adocn.Open
rs.Open strSql, adocn, 3, 3
If rs.EOF And rs.BOF Then
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Set rs = Nothing
adocn.Close
MsgBox "没有查询到符合您要求的信息!", vbCritical + vbOKOnly, "信息"
Exit Sub
Else
Text2.Text = rs.Fields("职称")
Text3.Text = rs.Fields("工资")
Set rs = Nothing
adocn.Close
End If
End If
End Sub
Private Sub Form_Load()
adocn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\人事管理.mdb;Persist Security Info=False"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询