实在是没办法 才来找大家帮忙 是关于VB+ACCESS 查询的问题~~~
实在是没办法 才来找大家帮忙 是关于VB+ACCESS 查询的问题~~~
这个是我的第一贴~
我刚学VB没多久,现在在联系做小的模型 来实现 查询功能
具体是我用了2个combo1实现在两个combo1中选择不同的值(为简化 就用了“姓名”而且输入的是纯数字) 而搜索出相关信息
数据库中有 id 姓名 等 id是自动编号 其他都是文本
Private con As ADODB.Connection
Private Res As ADODB.Recordset
`````````````````````````
Private Sub Form_Load()
Set con = New ADODB.Connection
Set Res = New ADODB.Recordset
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data Source= " & App.Path & "\db8.mdb;Persist Security Info=False "
With Res
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open "select id,名称,单位,单价 from 备件基本信息 ", con, , , adCmdText
End With
For i = 1 To Res.RecordCount
Combo1(0).AddItem Res.Fields(1).Value
Combo1(1).AddItem Res.Fields(1).Value
Res.MoveNext
Next
End Sub
``````````````````
Private Sub Command1_Click()
Dim Mccon As ADODB.Connection
Dim Mcres As ADODB.Recordset
Set Mccon = New ADODB.Connection
Set Mcres = New ADODB.Recordset Mccon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data Source= " & App.Path & "\db8.mdb;Persist Security Info=False "
With Mcres
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open "select * from 备件基本信息 where 姓名 between ' " & Trim(Combo1(0).Text) & " ' " & " and " & " ' " & Trim(Combo1(1).Text) & " ' ", Mccon, , , adCmdText
End With
Set DataGrid1.DataSource = Mcres
End Sub
结果在.Open "select * from 备件基本信息 where 姓名 between ' " & Trim(Combo1(0).Text) & " ' " & " and " & " ' " & Trim(Combo1(1).Text) & " ' ", Mccon, , , adCmdText出现错误 错误是“至少有个参数没有被指定值”,想了好久也不知道为什么 请大家帮我找出来 谢谢 顺便帮我完成编写的目的
[解决办法]
.Open "select * from 备件基本信息 where 姓名 between ' " & Trim(Combo1(0).Text) & " ' " & " and " & " ' " & Trim(Combo1(1).Text) & " ' ", Mccon, , , adCmdText
改为
.Open "select * from 备件基本信息 where 姓名 between ' " & Trim(Combo1(0).Text) & " ' " & " and " & " ' " & Trim(Combo1(1).Text) & " ' ", Mccon, adOpenDynamic, adLockOptimistic
试验一下
[解决办法]
我很久以前的一段程序,找了半天,希望对你有帮助。程序是用一个SQL语句从数据库取得数据,显示在表格控件上
strSql = "select distinct * from 检测模版 where 编号 = ' " & sQuery & " ' or 检测项目 like '% " & sQuery & "% ' order by 1 "
setVSFG strSql, MSHFG, sConnect
Public Sub setVSFG(strSql As String, myVSFG As MSHFlexGrid, sCon As String)
'将SQl结果显示在GRID上
Dim rs As New ADODB.Recordset
Dim con As New ADODB.Connection
con.ConnectionString = sCon
con.Open
rs.Open strSql, con, adOpenStatic
Set myVSFG.DataSource = rs
Set rs = Nothing
con.Close
End Sub