Private Sub Text1_Change() dim rs as new adodb.recordset rs.open "select col2,col3,col4,col5 from table1 where col1='" & text1.text & "'",cn if not rs.eof then text2.text=rs!col2 text3.text=rs!col3 text4.text=rs!col4 text5.text=rs!col5 end if rs.close set rs=nothingEnd Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) dim rs as new adodb.recordset If KeyAscii = vbKeyReturn Then rs.open "select c2,c3,c4,c5 from Table1 where c1='" & trim(text1.text) & "'",cn if rs.recordcount>0 then text2.text=rs!c2 text3.text=rs!c3 text4.text=rs!c4 text5.text=rs!c5 end if rs.close set rs=nothing end if End Sub
'你得自己梢作改动才成呀,如cn你要定义及连接'引用Microsoft Activex Data Object2.x Library Dim cn As New ADODB.Connection cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.MDB;Persist Security Info=False" 建议你不要用绑定的方法,那样太不灵活了
Private Sub Text1_LostFocus() Dim StrSql As String Dim Rs As New ADODB.Recordset
If Len(Trim$(Text1.Text)) > 0 Then'检查TEXT1中的内容是否为空 StrSql = "SELECT C2,C3,C4 FROM [TABLENAME] WHERE ID='" & Text1.Text & "'" Rs.Open StrSql, P_Cnn '打开记录集. If Not (Rs.EOF And Rs.BOF) Then '检查是否有记录返回 Rs.MoveFirst '移到记录首条记录 Text2.Text = "" & Rs.Fields("C2") Text3.Text = "" & Rs.Fields("C3") Text4.Text = "" & Rs.Fields("C4") Else Text2.Text = "" Text3.Text = "" Text4.Text = "" End If Else Text2.Text = "" Text3.Text = "" Text4.Text = "" End If Set Rs = Nothing End Sub
Private Sub Text1_LostFocus() Dim StrSql As String Dim Rs As New ADODB.Recordset Dim P_Cnn As New ADODB.Connection P_Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\d\My Documents\db1.mdb;Persist Security Info=False" If Len(Trim$(Text1.Text)) > 0 Then '检查TEXT1中的内容是否为空 StrSql = "SELECT col2,col3,col4 FROM sqx WHERE col1='" & text1.Text& "'" Rs.Open StrSql, P_Cnn '打开记录集. If Not (Rs.EOF And Rs.BOF) Then '检查是否有记录返回 Rs.MoveFirst '移到记录首条记录 Text2.Text = "" & Rs.Fields("col2") Text3.Text = "" & Rs.Fields("col3") Text4.Text = "" & Rs.Fields("col4") Else Text2.Text = "" Text3.Text = "" Text4.Text = "" End If Else Text2.Text = "" Text3.Text = "" Text4.Text = "" End If Set Rs = Nothing End Sub
错误出在哪一行?可能你的字段col1不是字符类型, 改成这个试试: StrSql = "SELECT col2,col3,col4 FROM sqx WHERE col1=" & text1.Text debug.print strsql 然后你再把调试窗口中的strsql贴出来看看
dim rs as new adodb.recordset
rs.open "select col2,col3,col4,col5 from table1 where col1='" & text1.text & "'",cn
if not rs.eof then
text2.text=rs!col2
text3.text=rs!col3
text4.text=rs!col4
text5.text=rs!col5
end if
rs.close
set rs=nothingEnd Sub
dim rs as new adodb.recordset
If KeyAscii = vbKeyReturn Then
rs.open "select c2,c3,c4,c5 from Table1 where c1='" & trim(text1.text) & "'",cn
if rs.recordcount>0 then
text2.text=rs!c2
text3.text=rs!c3
text4.text=rs!c4
text5.text=rs!c5
end if
rs.close
set rs=nothing
end if
End Sub
不知道你看懂上面兩位兄弟帖的代碼沒有???
Dim cn As New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.MDB;Persist Security Info=False"
建议你不要用绑定的方法,那样太不灵活了
Private Sub Text1_LostFocus()
Dim StrSql As String
Dim Rs As New ADODB.Recordset
If Len(Trim$(Text1.Text)) > 0 Then'检查TEXT1中的内容是否为空
StrSql = "SELECT C2,C3,C4 FROM [TABLENAME] WHERE ID='" & Text1.Text & "'"
Rs.Open StrSql, P_Cnn '打开记录集.
If Not (Rs.EOF And Rs.BOF) Then '检查是否有记录返回
Rs.MoveFirst '移到记录首条记录
Text2.Text = "" & Rs.Fields("C2")
Text3.Text = "" & Rs.Fields("C3")
Text4.Text = "" & Rs.Fields("C4")
Else
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End If
Else
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End If
Set Rs = Nothing
End Sub
-------------------
连接到具体的数据库。
Dim StrSql As String
Dim Rs As New ADODB.Recordset
Dim P_Cnn As New ADODB.Connection
P_Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\d\My Documents\db1.mdb;Persist Security Info=False"
If Len(Trim$(Text1.Text)) > 0 Then '检查TEXT1中的内容是否为空
StrSql = "SELECT col2,col3,col4 FROM sqx WHERE col1='" & text1.Text& "'"
Rs.Open StrSql, P_Cnn '打开记录集.
If Not (Rs.EOF And Rs.BOF) Then '检查是否有记录返回
Rs.MoveFirst '移到记录首条记录
Text2.Text = "" & Rs.Fields("col2")
Text3.Text = "" & Rs.Fields("col3")
Text4.Text = "" & Rs.Fields("col4")
Else
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End If
Else
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End If
Set Rs = Nothing
End Sub
改成这个试试:
StrSql = "SELECT col2,col3,col4 FROM sqx WHERE col1=" & text1.Text
debug.print strsql
然后你再把调试窗口中的strsql贴出来看看