⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vb13.htm

📁 从太平洋上下载下来的打包学习VB的教程。 》VB专区 ·Visual Basic 的数据库编程 ·使用VB建立Web Server   ·VB与Windows资源管理器互拷文件
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://www.pconline.com.cn/experience/subject/VB/vb13.htm -->
<HTML><HEAD><TITLE>|><| 太平洋电脑信息网</TITLE><!-- #BeginTemplate "/Templates/0723.dwt" --><!-- #BeginEditable "doctitle" --><!-- #EndEditable -->
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK 
href="vb13.files/text.css" rel=stylesheet>
<STYLE type=text/css>.sfont {
	LINE-HEIGHT: 18pt
}
</STYLE>

<META content="MSHTML 5.00.2014.210" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width="100%">
  <TBODY>
  <TR>
    <TD vAlign=top width="100%">
      <TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
        <TBODY>
        <TR bgColor=#303880>
          <TD><A href="http://www.pconline.com.cn/"><IMG alt=太平洋电脑信息网 border=0 
            height=18 hspace=30 src="vb13.files/txtpconline.gif" 
            width=96></A><IMG height=10 src="vb13.files/blank.gif" 
        width=400></TD></TR></TBODY></TABLE></TD>
    <TD align=right background=vb13.files/bw.gif noWrap width=130><IMG 
      height=26 src="vb13.files/bw.gif" width=10><A 
      href="http://www.pconline.com.cn/sitemap/"><IMG alt=网站地图 border=0 
      height=26 src="vb13.files/sitemap.gif" width=21></A><IMG height=26 
      src="vb13.files/bw.gif" width=10><A 
      href="http://www.pconline.com.cn/tpy/"><IMG alt=关于太平洋 border=0 height=26 
      src="vb13.files/aboutus.gif" width=21></A><IMG height=26 
      src="vb13.files/bw.gif" width=10><A 
      href="http://www.pconline.com.cn/tpy/tariff.html"><IMG alt=与我们联系 border=0 
      height=26 src="vb13.files/contact.gif" width=21></A><IMG height=26 
      src="vb13.files/bw.gif" width=10></TD></TR></TBODY></TABLE><BR>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
  <TBODY>
  <TR>
    <TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
    <TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
    <TD>&nbsp;</TD>
    <TD colSpan=2 noWrap><A 
      href="http://www.pconline.com.cn/experience/index.html"><IMG 
      align=absBottom alt=企业之窗 border=0 height=15 
      src="vb13.files/txtexperience.gif" width=68></A> <A 
      href="http://www.pconline.com.cn/">首  页</A> <A 
      href="http://www.pconline.com.cn/market/">今日报价</A> <A 
      href="http://www.pconline.com.cn/recommend/">产品推介</A> <A 
      href="http://www.pconline.com.cn/yp/">企业之窗</A> <A 
      href="http://www.pconline.com.cn/news/">科技新闻</A> <A 
      href="http://www.pconline.com.cn/product/">产品查询</A> <A 
      href="http://www.pconline.com.cn/download/">软件下载</A></TD></TR>
  <TR>
    <TD><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
    <TD><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
    <TD><IMG height=1 src="vb13.files/blank.gif" width=21></TD>
    <TD bgColor=#ffa000 colSpan=2><IMG height=1 src="vb13.files/blank.gif" 
      width=1></TD></TR>
  <TR>
    <TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
    <TD bgColor=#cdcdcd><IMG height=5 src="vb13.files/blank.gif" width=1></TD>
    <TD align=right colSpan=3><IMG height=6 src="vb13.files/ffa000.gif" 
      width=1></TD></TR>
  <TR>
    <TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
    <TD align=right><A href="http://www.pconline.com.cn/"><IMG alt=太平洋电脑信息网 
      border=0 height=35 src="vb13.files/pconline.com.cn.gif" vspace=10 
      width=162></A></TD>
    <TD>&nbsp;</TD>
    <TD vAlign=bottom><!-- #BeginEditable "important_news" --><IMG height=40 
      src="vb13.files/blank.gif" width=350><!-- #EndEditable --></TD>
    <TD align=right vAlign=top><!-- #BeginEditable "adv_top" --><IMG alt=广告位 
      height=43 src="vb13.files/icon.gif" 
  width=190><!-- #EndEditable --></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
  <TBODY>
  <TR>
    <TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif" 
      width=1></TD>
    <TD vAlign=top width=170><!-- #BeginEditable "addons" --><!--替代为附加信息 --><!-- #EndEditable --></TD>
    <TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif" 
      width=1></TD>
    <TD><IMG height=1 src="vb13.files/blank.gif" width=21></TD>
    <TD colSpan=3 vAlign=top width=566><!-- #BeginEditable "1" --><IMG 
      height=1 src="vb13.files/blank.gif" width=566><BR><IMG height=1 
      src="vb13.files/666666.gif" width=420><BR><IMG height=13 
      src="vb13.files/blank.gif" width=1><A 
      href="http://www.pconline.com.cn/experience/">经验共享</A> &gt; <SPAN 
      class=greenb><A 
      href="http://www.pconline.com.cn/experience/subject/VB/index.html">专题教程</A></SPAN><A 
      href="http://www.pconline.com.cn/experience/subject/VB/index.html"> 
      VB专区</A>&gt; <BR><IMG height=1 src="vb13.files/666666.gif" vspace=1 
      width=420><BR>
      <P align=center><B><FONT color=#000066><SPAN 
      class=sfont>利用VB动态设计多“主细表”应用</SPAN></FONT></B><SPAN 
      class=sfont><BR></SPAN></P>
      <P align=center><SPAN class=sfont>方云波 </SPAN></P>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      在开发人事管理软件的工程中,当我们建立一个职工档案记录时,通常都会遇到设计职工履历表、职工家属情况的问题,我这里介绍一种用“主细表”方法的开发思路。 
      </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> <B>一. 数据库的建立</B> 
      </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      一个设计合理的数据库就是管理软件成功的一半。因此我们在开发之初就应该先慎重设计好数据库的结构。笔者在开发人事管理软件时建立一个名为:HR.mdb的数据库,该库包括下面三个表: 
      </SPAN><PRE><SPAN class=sfont>
职工的自然情况一般是下面这些内容的:(Employee)
职工姓名	性别	出生日期	民族	政治面貌	婚否……
职工履历表的内容一般是下面这些内容的:(Vita)
起始时间  终止时间  所在单位及部门  任职情况……
职工家属情况一般是下面这些内容的:(Relative)
亲人姓名	与本人关系  所在单位及部门  任职情况……
</SPAN></PRE>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 很显然,Employee 
      与Vita和Relative表之间是“一对多”的关系。即一条Employee记录对应多条Vita和Relative记录,亦即Employee为主表,Vita和Relative为细表。由于在设计ACCESS数据库时每个表必须要有一个主关键字(Primary 
      Key),因此笔者设计了一个“职工ID”字段作为三个表相互连接的字段,同时也作为Employee表的主关键字。而另外两个表的主关键字就采用自动编号(AutoNum)字段了。 
      </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> <B>二. 建立表之间的连接</B> 
      </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      数据库设计好之后就可以着手设计各种应用,但程序设计中关键的问题是如何建立“主细表”之间的连接。 </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 建立“主细表”之间的连接有两种方法: 
      </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 1、 
      利用VB的数据窗体向导生成“主细表”的连接。 </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      根据向导,可以生成一个主表和一个细表的连接。但是这种方法生成的连接有它的缺陷。首先是它只能生成两个表的连接,对我们上面所说的情况就无能为力了;其次是开发者在为向导提供数据库时,系统把数据库的绝对路径加入了DATA控件,非常不利于程序的分发。 
      下面介绍笔者使用的一种方法。 </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 2、 动态创建“主细表”的连接 
      </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      新建一个Form,然后添加三个DATA控件和三个DBGRID控件。三个DATA控件的名称分别为:datPrimaryRS,datSecondaryRS,DatThirdRS;三个DBGRID控件的名称分别为:grdPrimaryRS,grdSecondaryRS,grdDatThirdRS。三个DATA控件Form上既没有指定DatabaseName,也没有指定RecordSource,只是在Form调用时才动态赋值。三个DBGRID控件倒是指定了所对应的DATA控件。下面是Form_Load()函数: 
      </SPAN><PRE><SPAN class=sfont>
Private Sub Form_Load()
    datPrimaryRS.DatabaseName = App.path &amp; “\HR.mdb”
datPrimaryRS.RecordSource =
 "select * from [Employee] Order by [职工      ID]"
    datSecondaryRS.DatabaseName = App.path &amp; “\HR.mdb
    datThirdRS.DatabaseName = App.path &amp; “\HR.mdb
  '创建主表网格记录集
  datPrimaryRS.Refresh
End Sub
</SPAN></PRE>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      这里只是创建和显示了主表的数据,那么两个细表的数据如何创建和显示呢?从主细表的概念我们知道,我们查看主表的任何一条记录,细表必须同时显示出对应的多条记录。因此我们必须对主表的任何一条记录成为当前记录时触发的事件编码。而该事件就是Reposition。 
      </SPAN><PRE><SPAN class=sfont>
Private Sub datPrimaryRS_Reposition()
  Screen.MousePointer = vbDefault
    On Error Resume Next
    datSecondaryRS.Refresh
    DatThirdRS.Refresh
  '使网格与主表记录集同步
  datSecondaryRS.RecordSource = "select [职工ID],
[起始日期],[终止日期],[所在单位及部门],[任职情况] 
from [Vita] where [职工ID]=" &amp; datPrimaryRS.Recordset!
[职工ID] &amp; " Order by [起始日期]"
  DatThirdRS.RecordSource = "select [职工ID],[亲人姓名],
[关系],[所在单位及部门],[任职情况] 
from [Relative] where [职工ID]=" &amp; 
datPrimaryRS.Recordset![职工ID] &amp; ""
  datSecondaryRS.Refresh
  DatThirdRS.Refresh
  '为 dynasets 和快照显示当前记录位置
  datPrimaryRS.Caption = "当前记录: " &amp; 
(datPrimaryRS.Recordset.AbsolutePosition + 1)
End Sub
</SPAN></PRE>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      很显然,如果有三个、四个…等多个细表也可以如法炮制。 </SPAN>
      <P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 
      至此,一个灵活的,表数不限的主细表连接就建立起来了。只要将三个DBGRID的AllowAddNew、AllowDelete和AllowUpdate三个属性设置为TRUE,那么就可以对这些主细表进行各种数据库表作了。</SPAN> 

      <P align=right><A 
      href="http://www.pconline.com.cn/experience/subject/VB/index.html">----更多的文章</A> 
<!-- #EndEditable --></P></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
  <TBODY>
  <TR>
    <TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif" 
      width=1></TD>
    <TD align=right vAlign=bottom width=170><IMG height=10 
      src="vb13.files/blank.gif" width=170><BR></TD>
    <TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif" 
      width=1></TD>
    <TD width=21><IMG height=1 src="vb13.files/blank.gif" width=21></TD>
    <TD bgColor=#eeeeee width=566><IMG height=10 src="vb13.files/blank.gif" 
      width=566><BR>版权所有&copy;1999 太平洋电脑信息网<BR><FONT 
      face="Arial, Helvetica, sans-serif"><A 
      href="mailto:exp@pconline.com.cn">exp@pconline.com.cn</A></FONT> <IMG 
      height=1 src="vb13.files/hhvisit.html" width=1> </TD></TR></TBODY></TABLE><!-- #EndTemplate --></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -