|
< >我没有计算机专业基础知识,正在自学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">=" & 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">=" & 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">, |
|