用access生成一个表,每个记录有5列,在vb中添加5个text控件,如何能在第一个text控件写入数据(就是表第一列中的数据),其他四个控件就可以立即显示出其同一记录中的其他四列数据

解决方案 »

  1.   

    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
      

  2.   

    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
      

  3.   

          还是不要写在change事件里,写在validate事件里好些。
      

  4.   

    如果要实时检测就写在change事件中,如果确定编辑完成后再检测那么写在validate事件里。把你的代码贴出来看看
      

  5.   

    我建了一个窗体,添加了5个text控件和一个adodc控件,adodc绑定到数据库,text连接到adodc上,代码完全是你给的那个,没有其他的了,就是这些了
      

  6.   

    text不需要連接任何東西
    不知道你看懂上面兩位兄弟帖的代碼沒有???
      

  7.   

    '你得自己梢作改动才成呀,如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"
    建议你不要用绑定的方法,那样太不灵活了
      

  8.   


    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
      

  9.   

    请问最后一帖中的 P_Cnn 是什么啊
      

  10.   

    我给 P_Cnn 一个ADODB.Connection定义,结果出来了"表达式数据类型不匹配"
      

  11.   

    我给 P_Cnn 一个ADODB.Connection定义,结果出来了"表达式数据类型不匹配"
    -------------------
    连接到具体的数据库。
      

  12.   

    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
      

  13.   

    错误出在哪一行?可能你的字段col1不是字符类型,
    改成这个试试:
    StrSql = "SELECT col2,col3,col4 FROM sqx WHERE col1=" & text1.Text 
    debug.print strsql
    然后你再把调试窗口中的strsql贴出来看看