大伙帮我看看吧,我用了参数查询,就老有错误哦.不用参数,我已经搞定了,但是用了command的parameters后就不对了,请各位帮我看看吧 !先谢过了!
Dim cn As New ADODB.Connection
Dim cmd As New Command
Private Sub Command1_Click()
Dim objrs As Recordset
Dim objsource As Recordset
cmd("sno") = "%" & Text1.Text & "%"
cmd("sn") = "%" & Text2.Text & "%"
cmd("age") = "%" & Text3.Text & "%"
cmd("sex") = "%" & Text4.Text & "%"
cmd("dept") = "%" & Text5.Text & "%"
cmd("dn") = "%" & Text6.Text & "%"
Set objrs = cmd.Execute()
Set objsource = New Recordset
For i = o To objrs.Fields.Count - 1
objsource.Fields.Append objrs.Fields(i).Name, adVarChar, objrs.Fields(i).DefinedSize
Next
objsource.Open
While Not objrs.EOF
objsource.AddNew
objsource!sno = objrs!sno
objsource!sn = objrs!sn
objsource!age = objrs!age
objsource!sex = objrs!sex
objsource!dept = objrs!dept
objsource!dn = objrs!dn
objrs.MoveNext
Wend
If objsource.RecordCount > 0 Then objsource.MoveFirst
Set DataGrid1.DataSource = objsource
End Sub
Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.ConnectionString = "provider=sqloledb.1;data source=(local);user id=sa;pwd=623058;initial catalog=sdb"
cn.Open
Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
With cmd
.CommandText = "select * from s where sno like ? and sn like ? and age like ? and sex like? and dept like ? and dn like ?"
.CommandType = adCmdText
End With
Dim param As New Parameter
Set param = cmd.CreateParameter("sno", adChar, adParamInput, 3)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("sn", adChar, adParamInput, 8)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("age", adInteger, adParamInput, 4)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("sex", adChar, adParamInput, 2)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("dept", adChar, adParamInput, 1)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("dn", adChar, adParamInput, 15)
cmd.Parameters.Append param
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set cmd = Nothing
Set cn = Nothing
End Sub