我做一个人事方面的系统
一个datareport中需要显示员工的所有信息。
脚本如下:
Private Sub DataReport_Initialize()
    
    Dim cn As New ADODB.Connection   '定义ADO的连接对象
    Dim rs As New ADODB.Recordset    '定义ADO的记录集对象
    cn.Open "Provider=SQLOLEDB;Password=sa;User ID=sa;Initial Catalog=hr;Data Source=dd-0c6ff813f959" '调用连接对象的open方法打开连接
    rs.Open "select worker.*,human.*,wage.* from worker,human,wage where worker.worknumber='" & gonghao & "' and worker.worknumber=human.worknumber and worker.worknumber=wage.worknumber", cn, adOpenForwardOnly, adLockReadOnly, adCmdText   '调用记录集对象的open方法来执行SQL语句
    
    Set DataReport1.DataSource = rs
    DataReport1.Sections("section1").Controls("text1").DataField = rs.Fields("worker.worknumber").Name
    DataReport1.Sections("section1").Controls("text2").DataField = rs.Fields("worker.workname").Name
    DataReport1.Sections("section1").Controls("text3").DataField = rs.Fields("worker.sex").Name
    nian = rs.Fields("worker.birthyear").Name
    yue = rs.Fields("worker.birthmonth").Name
    nianyue = nian & "年" & yue & "月"
    DataReport1.Sections("Section1").Controls("Label6").Caption = nianyue
    DataReport1.Sections("section1").Controls("text4").DataField = rs.Fields("worker.education").Name
    DataReport1.Sections("section1").Controls("text5").DataField = rs.Fields("worker.marriage").Name
    DataReport1.Sections("section1").Controls("text6").DataField = rs.Fields("worker.child").Name
    DataReport1.Sections("section1").Controls("text7").DataField = rs.Fields("worker.tech").Name
    DataReport1.Sections("section1").Controls("text8").DataField = rs.Fields("worker.worklevel").Name
    DataReport1.Sections("section1").Controls("text9").DataField = rs.Fields("worker.wyear").Name
    DataReport1.Sections("section1").Controls("text10").DataField = rs.Fields("worker.lionyear").Name
    DataReport1.Sections("section1").Controls("text11").DataField = rs.Fields("worker.party").Name
    DataReport1.Sections("section1").Controls("text12").DataField = rs.Fields("worker.kind").Name
    DataReport1.Sections("section1").Controls("text13").DataField = rs.Fields("worker.idcard").Name
    DataReport1.Sections("section1").Controls("text14").DataField = rs.Fields("worker.other").Name
    
End Sub可是他提示“项目在所需的名称或序数中未被发现”
如果我只检索一个表的话,
rs.Open "select * from worker where worknumber='" & gonghao & "'", cn, adOpenForwardOnly, adLockReadOnly, adCmdText  
一切都是正确的,能运行。这是怎么回事啊?
高手们快帮帮我啊

解决方案 »

  1.   

    将你全部的引用:
    rs.Fields("worker.education").Name
    改成:
    rs.Fields("education").Name^_^
      

  2.   

    如果我全部改成rs.Fields("education").Name
    那不是他不知道我现在在取哪个表的值了么?而且我也没有发现字段名和我代码中引用的字段不一致阿
    前面的连接数据库都没有错,开始错的是这一行:
    DataReport1.Sections("section1").Controls("text1").DataField = rs.Fields("worker.worknumber").Name他提示rs.Fields("worker.worknumber").Name这一段“项目在所需的名称或序数中未被发现”这是怎么回事啊?可是明明worker表中是有worknumber这个字段的啊
    救救我啊!
      

  3.   

    我是想把worker,human,wage这三个表里面的同一个员工的信息都列出来
    当sql中只有worker这一个表的时候,是可以使用的
    当我加入了human,wage这两个表后 就出问题了
    当我只有worker这个一个表时,我是这样写的:
    Private Sub DataReport_Initialize()
        
        Dim cn As New ADODB.Connection   '定义ADO的连接对象
        Dim rs As New ADODB.Recordset    '定义ADO的记录集对象
        cn.Open "Provider=SQLOLEDB;Password=sa;User ID=sa;Initial Catalog=hr;Data Source=dd-0c6ff813f959" '调用连接对象的open方法打开连接
        rs.Open "select * from worker where worknumber='" & gonghao & "'", cn, adOpenForwardOnly, adLockReadOnly, adCmdText   '调用记录集对象的open方法来执行SQL语句
        
        Set DataReport1.DataSource = rs
        DataReport1.Sections("section1").Controls("text1").DataField = rs.Fields("worknumber").Name
        DataReport1.Sections("section1").Controls("text2").DataField = rs.Fields("workname").Name
        DataReport1.Sections("section1").Controls("text3").DataField = rs.Fields("sex").Name
        nian = rs.Fields("birthyear").Name
        yue = rs.Fields("birthmonth").Name
        nianyue = nian & "年" & yue & "月"
        DataReport1.Sections("Section1").Controls("Label6").Caption = nianyue
        DataReport1.Sections("section1").Controls("text4").DataField = rs.Fields("education").Name
        DataReport1.Sections("section1").Controls("text5").DataField = rs.Fields("marriage").Name
        DataReport1.Sections("section1").Controls("text6").DataField = rs.Fields("child").Name
        DataReport1.Sections("section1").Controls("text7").DataField = rs.Fields("tech").Name
        DataReport1.Sections("section1").Controls("text8").DataField = rs.Fields("worklevel").Name
        DataReport1.Sections("section1").Controls("text9").DataField = rs.Fields("wyear").Name
        DataReport1.Sections("section1").Controls("text10").DataField = rs.Fields("lionyear").Name
        DataReport1.Sections("section1").Controls("text11").DataField = rs.Fields("party").Name
        DataReport1.Sections("section1").Controls("text12").DataField = rs.Fields("kind").Name
        DataReport1.Sections("section1").Controls("text13").DataField = rs.Fields("idcard").Name
        DataReport1.Sections("section1").Controls("text14").DataField = rs.Fields("other").Name
        
    End Sub为什么我多加了表就不行了呢?
      

  4.   

    worker.worknumber不是字段名,你在select后列出你要打印的就行了,不要aaa.*这种形式。^_^
      

  5.   

    那他怎么知道我要去哪个表的哪个字段呢?
    把worker.worknumber中的worker去掉,不就不知道我要去哪个表的值了吗?
      

  6.   

    我刚刚试过把worker全部去掉
    结果却是没有出错,可是运行之后datareport上也没有任何显示啊
    可是这个员工的信息是确实存在的啊那要怎么解决呢?
    大虾帮帮我!
      

  7.   

    "select worker.A,human.B,wage.C from ...
    之后就用A、B、C就行了。^_^
      

  8.   

    我刚刚按照你的试过了,没有出错了
    可是运行之后没有任何数据被显示出来,而且这些信息是存在的,就是没有任何显示。这是怎么回事呢?
    我调试的时候看了一下他们的值,当我要查询工号为0009的信息,可是rs.Fields("worknumber").Name的实际的值却是:“worknumber”!
    我头都大了!
      

  9.   

    rs.Fields("worknumber").Name
    把.Name去掉试试
      

  10.   

    rs.Fields("worknumber").Name变成
    rs("worknumber")^_^
      

  11.   

    我去掉了.name之后
    他就提醒说 类型不匹配
    也是不对阿
    高手在指教指教!谢谢!
      

  12.   

    rs.Fields("worknumber").Name 应该是 rs.Fields("worknumber").Value 吧
      

  13.   

    按照你说的 改成:rs.Fields("worknumber").Value
    可是结果错误提示:
    实时错误3021
    BOF或EOF中有一个是"真",或者当前的记录已被删除,所需的操作要求一个当前的记录。 这个错误是说没有找到相应记录吧?可是明明有这条记录的
    如果只是在一个表内查询,她就可以显示出来了
    多个表的查询,她就显示不出来,还提示错误。说明 这样做还是不对,还有其他的方法吗?
    你们怎么就没有牵涉到用datareport多表查询的啊?
    快教教我,或者把你们的脚本给我看看哦
    谢谢了!