设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 913|回复: 1
打印 上一主题 下一主题

[窗体] [求助]关于子窗体数据源的设置方式

[复制链接]
跳转到指定楼层
1#
发表于 2007-7-19 18:42:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
< >我没有计算机专业基础知识,正在自学Access.<o:p></o:p></P>
< >以SQL Server 作数据库,用Access编程.<o:p></o:p></P>
< >在窗体A(员工登记表)中有子窗体B(员工家庭成员表).(关系字段为工号)<o:p></o:p></P>
<P >用ADO读取SQL Server数据,显示在Access窗体中:<o:p></o:p></P>
<P ><FONT face="Times New Roman">Set con = New ADODB.Connection<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">Set rst = New ADODB.Recordset<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">‘</FONT>更新主窗体<o:p></o:p></P>
<P ><FONT face="Times New Roman">con.Open "provider=SQLOLEDB;database=</FONT>数据库<FONT face="Times New Roman">sql;server=</FONT>主机<FONT face="Times New Roman">;uid=wdl;pwd=123456;"<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">sql = "select * from</FONT>员工登记表<FONT face="Times New Roman"> where </FONT>工号<FONT face="Times New Roman">=" &amp; me.</FONT>选择工号<FONT face="Times New Roman">.value                ‘</FONT>主窗体<FONT face="Times New Roman">A</FONT>数据源<o:p></o:p></P>
<P ><FONT face="Times New Roman">Set rst = con.Execute(sql)<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">rst.MoveFirst<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">Do Until rst.EOF = True<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">    For Each fil In rst.Fields<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">                   For Each ctl In Me.Controls<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">                             If ctl.name=fil.name then<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">                                     Ctl.value=rst(“fil.name”)<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">                            End if<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">                   Next ctl<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">    Next fil<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">    rst.MoveNext<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman"><st1:place w:st="on">Loop</st1:place><o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">‘</FONT>更新子窗体<o:p></o:p></P>
<P ><FONT face="Times New Roman">sql = "select * from</FONT>员工家庭成员表<FONT face="Times New Roman">where </FONT>工号<FONT face="Times New Roman">=" &amp; me.</FONT>选择工号<FONT face="Times New Roman">.value<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">me.B.Form.RecordSource=sql                                                            ‘</FONT>更新子窗体<FONT face="Times New Roman">B</FONT>数据源<o:p></o:p></P>
<P ><FONT face="Times New Roman">me.B.Requery                                                                                       ‘</FONT>刷新子窗体<FONT face="Times New Roman">B<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">rst.Close<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">con.Close<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">Set rst = Nothing<o:p></o:p></FONT></P>
<P ><FONT face="Times New Roman">Set con = Nothing<o:p></o:p></FONT></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >以上当<FONT face="Times New Roman">Access</FONT>窗体<FONT face="Times New Roman">,</FONT>文件菜单<FONT face="Times New Roman">,</FONT>连接<FONT face="Times New Roman">(T)….</FONT>与SQL Server”主机<FONT face="Times New Roman">”</FONT>的”数据库sql”处于连接状态时, 子窗体数据正常显示.<o:p></o:p></P>
<P >问题发生在<FONT face="Times New Roman">:</FONT>当<FONT face="Times New Roman">Access</FONT>窗体<FONT face="Times New Roman">,</FONT>文件菜单<FONT face="Times New Roman">,</FONT>连接<FONT face="Times New Roman">(T)….</FONT>与SQL Server”主机<FONT face="Times New Roman">”</FONT>的”数据库sql”处于非连接状态时,子窗体数据没有显示.更改子窗体的sql为rst仿照主窗体方式时,仅仅显示出(非绑定)子窗体的最后一行数据.更进一步,此时,甚至连报表和子报表也无法用sql来提供其数据.<o:p></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >请教<FONT face="Times New Roman">:</FONT>当<FONT face="Times New Roman">Access</FONT>窗体<FONT face="Times New Roman">,
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2007-7-19 19:25:00 | 只看该作者
这么长的代码看不懂呀,上传你的示例吧!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-22 12:16 , Processed in 0.109471 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表