各位VB高手帮个忙,关于查询调用的。。
各个高手:碰到一个程序题。利用DAO链接数据库的。
假设在D盘驱动器上已建立了一个ACCESS数据库tx.mdb,该库中包含一张数据库表通讯录表,通讯录表中包含了编号、用户名、电话号码和地址等4个字段。试编写一个程序,实现能按用户名查找,并在文本框中显示查找结果。如图所示。设“查找”按钮的名称定义为cmdfindname,查找输入名称和结果文本框的名称分别定义为txtName和txtCount,记录条数标签的名称定义为lblCount。 Dim rst as recordset
Dim db as database
Private sub form_load
Use database d:\tx.mdb
Set db=dbengine.workspaces(0).dbtabases(0)
Set rst=db.openrecordset(“通讯录表”)
End sub
Private sub cmdfindname_click( )
Dim strinput as string
Dim strsql as string
Strinput=input(“请输入需查找用户名”)
Txtname.text=strinput
Strsql=”select *from 通讯录表 where 用户名 like & strinput & “ “
Set.rst=db.openrecordset(strsqr)
If not rst.eof then
Do while not rst.eof
Txt1.value=rst(“用户名”)
Txt2.value=rst(“编号”)
Txt3.value=rst(“电话号码”)
Txt4.value=rst(“地址”)
X=msgbox(“查找是否正确?”,vbyesno,”查找提示”)
If x=vbyes then
Exit sub
Else
Rst.movenext
Lblcount=strsql+1
Lblcount.caption=”一共找到” & (strsql+1)
Endif
Loop
Else
msgbox “用户[”& rinput & “]不存在!”,vbokonly, ”查找提示”
end if
rst.close
end sub
麻烦各位改下到VB中能调试出来。。谢谢奥 Strsql = "select * from 通讯录表 where 用户名 like " & Chr(39) & "%" & strinput & "%" & Chr(39)
Dim rst As Recordset各种数据类型字段查询方法如下:
Dim db As Database
Private Sub Form_load()
Set db = DBEngine.Workspaces(0).OpenDatabase("d:\tx.mdb", False, False, "Provider=microsoft.jet.oledb.4.0;PWD=;")
End Sub
Private Sub cmdfindname_click()
Dim strinput As String
Dim strSQL As String, i As Long
strinput = InputBox("请输入需查找用户名")
Txtname.Text = strinput
strSQL = "select * from 通讯录表 where 用户名 like '*" & strinput & "*' "
Set rst = db.OpenRecordset(strsqr)
If Not rst.EOF Then
Do While Not rst.EOF
Txt1.Value = rst("用户名")
Txt2.Value = rst("编号")
Txt3.Value = rst("电话号码")
Txt4.Value = rst("地址")
x = MsgBox("查找是否正确?", vbYesNo, "查找提示")
If x = vbYes Then
Exit Sub
Else
rst.MoveNext
Lblcount = i + 1
Lblcount.Caption = "一共找到" & (i + 1)
End If
Loop
Else
MsgBox "用户[" & strinput & "]不存在!", vbOKOnly, "查找提示"
End If
rst.Close
Set rst = Nothing
End Sub
sql = "select * from sjb where ID = " & Val(Text1.Text) 或
sql = "select * from sjb where ID = " & trim(Text1.Text) ’数据类型——自动编号
sql = "select * from sjb where 文本 = '" & Text1.Text & "'" ’数据类型——文本
sql = "select * from sjb where 数字 = " & Val(Text1.Text) ’数据类型——数字
sql = "select * from sjb where 货币 = " & val(Text1.Text) ’数据类型——货币
sql = "select * from sjb where 日期 = #" & trim(Text1.Text) & "#" ’数据类型——日期
数字字段不用双单引号,字符字段使用双单(')引号.日期字段对ACCESS数据库则使用双#号.
以下是实例
’输入ID查询代码如下:
Private Sub Form_Load()
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=gjr#data.mdb;Persist Security Info=False;"
Conn.Open Connstr
End Sub
Private Sub Command1_Click()
sql = "select * from sjb where ID = " & Val(Text1.Text)
Rs.Open sql, Conn, 1, 1
Text2.Text = "文本是 " & Rs.Fields(1) & vbCrLf & vbCrLf
Text3.Text = Rs.Fields(2)
Text4.Text = Rs.Fields(3)
Text5.Text = Rs.Fields("日期")
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
Private Sub Command2_Click()
Form1.Show
Unload Me
End Sub
’输入文本查询代码如下:
Private Sub Form_Load()
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=gjr#data.mdb;Persist Security Info=False;"
Conn.Open Connstr
End Sub
Private Sub Command1_Click()
sql = "select * from sjb where 文本 = '" & Text1.Text & "'"
Rs.Open sql, Conn, 1, 1
Text2.Text = "数字是 " & Rs.Fields(2) & vbCrLf & vbCrLf
Text3.Text = Rs.Fields(3)
Text4.Text = Rs.Fields("日期")
Text5.Text = Rs.Fields(0)
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
Private Sub Command2_Click()
Form1.Show
Unload Me
End Sub
’输入数字查询代码如下:
Private Sub Form_Load()
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=gjr#data.mdb;Persist Security Info=False;"
Conn.Open Connstr
End Sub
Private Sub Command1_Click()
sql = "select * from sjb where 数字 = " & Val(Text1.Text)
Rs.Open sql, Conn, 1, 1
Text2.Text = "文本是 " & Rs.Fields(1) & vbCrLf & vbCrLf
Text3.Text = Rs.Fields(2)
Text4.Text = Rs.Fields("日期")
Text5.Text = Rs.Fields(5)
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
Private Sub Command2_Click()
Form1.Show
Unload Me
End Sub
’输入货币查询代码如下:
Private Sub Form_Load()
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=gjr#data.mdb;Persist Security Info=False;"
Conn.Open Connstr
End Sub
Private Sub Command1_Click()
sql = "select * from sjb where 货币 = " & val(Text1.Text)
Rs.Open sql, Conn, 1, 1
Text2.Text = "文本是 " & Rs.Fields(1) & vbCrLf & vbCrLf
Text3.Text = Rs.Fields(2)
Text4.Text = Rs.Fields("日期")
Text5.Text = Rs.Fields(5)
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
Private Sub Command2_Click()
Form1.Show
Unload Me
End Sub
’输入日期查询代码如下:
Private Sub Form_Load()
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=gjr#data.mdb;Persist Security Info=False;"
Conn.Open Connstr
End Sub
Private Sub Command1_Click()
sql = "select * from sjb where 日期 = #" & trim(Text1.Text) & "#"
Rs.Open sql, Conn, 1, 1
Text2.Text = "文本是 " & Rs.Fields(1) & vbCrLf & vbCrLf
Text3.Text = Rs.Fields(2)
Text4.Text = Rs.Fields("货币")
Text5.Text = Rs.Fields(5)
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
Private Sub Command2_Click()
Form1.Show
Unload Me
End Sub
如需要制作好的实例,请留邮箱。 好几天 没登录了 谢谢各位大牛。。。。痛苦流泪般的。。
补充:VB , 数据库(包含打印,安装,报表)