½« Microsoft Access ÓÃ×÷ Automation ·þÎñÆ÷
¸ÅÒª ¸ß¼¶£ºÒªÇó¾ßÓи߼¶±à³Ì¡¢»¥²Ù×÷ÐԺͶàÓû§¼¼ÇÉ¡£ ±¾ÎÄÌṩ²¢ËµÃ÷ÓйØÈçºÎ½« Microsoft Access 7.0 »ò 97 ÓÃ×÷ Automation ·þÎñÆ÷µÄÐÅÏ¢ºÍʾÀý¡£Ê¹Óà Visual Basic for Applications£¬¿ÉÒÔͨ¹ýÖ§³Ö Automation µÄ¿ØÖÆÆ÷»ò¿Í»§³ÌÐò£¨Èç Microsoft Excel¡¢Microsoft Project »ò Microsoft Visual Basic£©À´²Ù×Ý Microsoft Access µÄ¹¦ÄÜ¡£ÀýÈ磬¿ÉÒÔÔÚʹÓà Automation µÄ Microsoft Excel Öд´½¨Ó¦ÓóÌÐò£¬´òÓ¡ Microsoft Access Êý¾Ý¿âÖеı¨±í¡£ ±¾ÎļÙÉèÄúÊìϤ Visual Basic for Applications£¬²¢ÊìϤÈçºÎʹÓà Microsoft Access Ëù¸½´øµÄ±à³Ì¹¤¾ßÀ´´´½¨ Microsoft Access Ó¦ÓóÌÐò¡£ÓÐ¹Ø Visual Basic for Applications µÄÐÅÏ¢£¬Çë²Î¼ûÊֲᡶÓà Microsoft Access ¹¹½¨Ó¦ÓóÌÐò¡·¡£ ¸ü¶àÐÅÏ¢ ±¾ÎÄÌÖÂÛÒÔÏÂÖ÷Ì⣺ ¡ñ´´½¨¶Ô Microsoft Access µÄÒýÓà ¡ñʹÓà GetObject() ºÍ CreateObject() º¯Êý ¡ñÁ˽â UserControl ºÍ Visible ÊôÐÔ ¡ñ²é¿´ Microsoft Access µÄʵÀý¡£ ¡ñ¹Ø±Õ Microsoft Access µÄʵÀý ¡ñ´ò¿ªÊý¾Ý¿âʱÈƹýÆô¶¯ÉèÖà ¡ñµ÷Óà Microsoft Access º¯Êý¡£ ¡ñµ÷ÓÃ×Ô¶¨Òå¹ý³Ì ¡ñʹÓÃÔËÐÐʱӦÓóÌÐò ¡ñʹÓð²È«¹¤×÷×é ¡ñʾÀý£º
¡òÔ¤ÀÀ»ò´òÓ¡±¨±í ¡òµ÷Óá°±¨±íÏòµ¼¡±Ð½¨±¨±í ´´½¨¶Ô Microsoft Access µÄÒýÓÃ
Automation ÔÊÐíʹÓà Visual Basic ´úÂëͨ¹ýÆäËüµÄÓ¦ÓóÌÐò¿ØÖÆ»ò´¦Àí Microsoft Access ¶ÔÏó¡£ÎªÊ¹Æä³ÉΪ¿ÉÄÜ£¬ÏµÍ³½«ÓÐ¹Ø Microsoft Access ¶ÔÏóµÄÐÅÏ¢´æ·ÅÔÚÃûΪ Msaccess.tlb µÄÀàÐÍ¿âÖС£Èç¹ûÏ£Íû OLE Automation ´úÂëµÄÐÔÄÜ×î¼Ñ£¬¿ÉÒÔ´´½¨¶Ô Microsoft Access ÀàÐÍ¿âµÄÒýÓá£
±¸×¢£º±¾ÎÄÖеÄÐí¶àʾÀý¹ý³Ì¶¼ÐèÒª¶Ô Microsoft Access µÄÒýÓá£Èç¹û´òËãʹÓÃʾÀý´úÂ룬ÔÚ Automation ¿ØÖÆÆ÷Öд´½¨ÒýÓÃÒ»¶¨Òª×ñÑÏÂÁв½Öè¡£ ´´½¨ÒýÓ㺠ÔÚ Automation ¿ØÖÆÆ÷Öдò¿ªÄ£¿é¡£ ÔÚ¡°¹¤¾ß¡±²Ëµ¥ÉÏ£¬µ¥»÷¡°ÒýÓᱡ£ ÔÚ¡°ÒýÓ᱿òÖУ¬Ñ¡Ôñ Microsoft Access 8.0 ¶ÔÏó¿â£¨»ò Microsoft Access for Windows 95 7.0 °æ£©£¬È»ºóµ¥»÷¡°È·¶¨¡±¡£ ʹÓà Automation ʱ£¬²»ÐèÒª´´½¨¶Ô Microsoft Access µÄÒýÓÃÀ´´¦ÀíÆä¶ÔÏó¡£²»¹ý£¬´´½¨ÒýÓÃÓÐÒÔϼ¸¸öÓŵ㣺
´úÂëÔËÐÐËٶȸü¿ì¡£ ¿ÉÒÔʹÓá°¶ÔÏóä¯ÀÀÆ÷¡±²é¿´ËùÓÐ Microsoft Access µÄ¶ÔÏó¡¢ÊôÐԺͷ½·¨¡£ÕâÑùÒ×ÓÚÈ·¶¨Ã¿¸ö¶ÔÏó¿ÉÓõÄÊôÐԺͷ½·¨¡£ ÔÚ Visual Basic ´úÂëÖпÉÒÔʹÓà Microsoft Access ³£Á¿»òÖ±½Óµ÷Óà Microsoft Access º¯Êý¡£ ¿ÉÒÔ½«±äÁ¿ÉùÃ÷ΪӦÓóÌÐò¶¨ÒåµÄ¶ÔÏóÀàÐÍ£¬ÀýÈ磺 Dim objAccess As Access.Application ÕâÖÖÉùÃ÷ÀàÐͱ»³ÆΪÔçÆڰ󶨣¬ÊÇËÙ¶È×î¿ìµÄ°ó¶¨·½·¨¡£µ«ÊÇ£¬²¢²»ÊÇËùÓÐµÄ Automation ¿ØÖÆÆ÷¶¼Ö§³ÖÕâÖÖÉùÃ÷ÀàÐÍ¡£Òò´Ë£¬±¾ÎÄÖеÄʾÀý´úÂëʹÓúóÆڰ󶨶ø²»ÊÇÔçÆڰ󶨡£ÀýÈ磬Ëü½«±äÁ¿ÉùÃ÷Ϊ¶ÔÏó¶ø²»ÊÇ Access.Application£º Dim objAccess As Object Òª²éÃ÷Ó¦ÓóÌÐòÊÇÖ§³ÖÔçÆڰ󶨻¹ÊǺóÆڰ󶨣¬Çë²éÔÄ Automation ¿ØÖÆÆ÷Îĵµ¡£ ʹÓà GetObject() ºÍ CreateObject() º¯Êý
GetObject() ºÍ CreateObject() º¯ÊýÔÊÐí´Ó Automation ¿ØÖÆÆ÷Ó¦ÓóÌÐòÖдò¿ª»ò¼¤»î Microsoft Access µÄʵÀý²¢¿ØÖÆÆ书ÄÜ¡£ÔÚ Visual Basic Ä£¿éÖÐʹÓÃÕâЩº¯Êýʱ£¬¼¤»î Microsoft Access ×÷Ϊ Automation ¶ÔÏó£¬È»ºó½«¶ÔÏóÖ¸¶¨¸ø±äÁ¿¡£ÀýÈ磬ÏÂÃæÊÇʹÓà GetObject() ºÍ CreateObject() º¯ÊýµÄ¼¸ÖÖ·½·¨¡£ ·½·¨ 1
ͨ¹ýÒÔÏÂÓï·¨£¬¿ÉÒÔʹÓà GetObject() º¯Êý¼¤»î»ò´ò¿ª Microsoft Access ʵÀýºÍÖ¸¶¨µÄÊý¾Ý¿â¡£ Dim objAccess as Object Set objAccess = GetObject("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb") Çë×¢Ò⣬ÔÚ´úÂëÔËÐÐʱ£¬¸ù¾ÝÒÔϲ»Í¬Ìõ¼þ£¬objAccess ±äÁ¿¿ÉÒÔÒýÓò»Í¬µÄ Microsoft Access ʵÀý£º
Èç¹ûÖ¸¶¨µÄÊý¾Ý¿â (Northwind.mdb)ÊÇÔÚÒ»¸ö Microsoft Access ʵÀýÖдò¿ªµÄ£¬Ôò objAccess ÒýÓôò¿ªµÄʵÀý¡£ Èç¹ûÖ¸¶¨µÄÊý¾Ý¿âÊÇÔÚ¶à¸ö Microsoft Access ʵÀýÖдò¿ªµÄ£¬Ôò objAccess Ëæ»úÒýÓÃÆäÖеÄij¸öʵÀý¡£ Èç¹ûÔËÐдúÂëʱ Microsoft Access ûÓÐÔËÐУ¬Ôò´ò¿ªÐ½¨ÊµÀý£¬²¢ÇÒ objAccess ÒýÓøÃʵÀý¡£ ·½·¨ 2
ͨ¹ýÏÂÃæµÄÓï·¨£¬¿ÉÒÔʹÓà GetObject() º¯Êý¼¤»î Microsoft Access ʵÀý£º Dim objAccess as Object Set objAccess = GetObject(,"Access.Application") Çë×¢Ò⣬ÔÚ´úÂëÔËÐÐʱ£¬¸ù¾ÝÒÔϲ»Í¬Ìõ¼þ£¬objAccess ±äÁ¿¿ÉÒÔÒýÓò»Í¬µÄ Microsoft Access ʵÀý£º
Èç¹û´úÂëÔËÐÐʱ Microsoft Access ʵÀýÕýÔÚÔËÐУ¬Ôò objAccess ÒýÓôò¿ªµÄʵÀý¡£ Èç¹û¶à¸ö Microsoft Access ʵÀýÕýÔÚÔËÐУ¬Ôò objAccess Ëæ»úÒýÓÃÆäÖеÄij¸öʵÀý¡£ Èç¹û´úÂëÔËÐÐʱ Microsoft Access ûÓÐÔËÐУ¬Ôò²úÉú´íÎó¡£ ·½·¨ 3
ͨ¹ýÒÔÏÂÓï·¨£¬¿ÉÒÔʹÓà CreateObject() º¯Êý´ò¿ªÐ嵀 Microsoft Access ʵÀý£º Dim objAccess as Object Set objAccess = CreateObject("Access.Application") objAccess ±äÁ¿ÒýÓÃÐÂʵÀýµÄ Application ¶ÔÏó¡£
±¸×¢£º×÷ΪһÖÖÌæ´ú·½·¨£¬Ä³Ð© Automation ¿ØÖÆÆ÷£¨Èç Microsoft Visual Basic 4.0£©Ö§³ÖʹÓùؼü×Ö¡°New¡±À´´ò¿ª Automation ·þÎñÆ÷µÄÐÂʵÀý£¬ÀýÈ磺 Dim objAccess as New Access.Application Çë²ÎÔÄ Automation ¿ØÖÆÆ÷ÎĵµÈ·¶¨ Automation ¿ØÖÆÆ÷ÊÇ·ñÖ§³ÖʹÓá°New¡±¹Ø¼ü×Ö¡£ Á˽â UserControl ºÍ Visible ÊôÐÔ Application ¶ÔÏóµÄ UserControl ºÍ Visible ÊôÐÔ¶ÔÓÚ¿ØÖÆ Microsoft Access µÄÆÁÄ»ÏÔʾ·½Ê½ºÜÖØÒª¡£UserControl ÊôÐÔʹÄúÄÜÈ·¶¨ Microsoft Access ʵÀýÊÇÓÉÓû§»¹ÊÇÓÉÆäËüʹÓà Automation µÄÓ¦ÓóÌÐòÆô¶¯µÄ¡£ Visible ÊôÐÔʹÄúÄÜÈ·¶¨ Microsoft Access ʵÀýÊǿɼûµÄ»¹ÊDZ»×îС»¯ÁË¡£
´ò¿ª»ò¼¤»î Microsoft Access ʵÀýʱ£¬¸ù¾ÝÔÚ Visual Basic Ö줻î Application ¶ÔÏóʱ Microsoft Access ÊÇ·ñÒѾÔËÐУ¬×Ô¶¯ÉèÖà UserControl ºÍ Visible ÊôÐÔ¡£ÀýÈ磬ϱí˵Ã÷ÔÚ²»Í¬ÇéÐÎϵÄÉèÖ㺠¼¤»î MS Access µÄ·½·¨ UserControl Visible ---------------------------------------------------------- ´ò¿ªÁË Microsoft Access True True ʵÀýÖ®ºóµÄ GetObject() º¯Êý Shell() º¯Êý True True ÉÐδ´ò¿ª Microsoft Access ʵÀýʱµÄ False False GetObject() º¯Êý CreateObject() º¯Êý False False UserControl ÊôÐÔ -------------------- UserControl ÊôÐÔ UserControl ÊôÐÔʼÖÕÊÇÖ»¶Á£¬Òò´Ë£¬Ê¹Óà Automation ÎÞ·¨ÉèÖøÃÊôÐÔ¡£µ«ÊÇ£¬Èç¹ûÔÚ Automation ´úÂë¿ÕÏÐʱÓû§¶Ô´úÂë½øÐиÉÔ¤£¬UserControl ÊôÐÔ¿ÉÄÜ×Ô¶¯¸ü¸Ä¡£ÀýÈ磬·¢ÉúÒÔÏÂʼþʱ£¬UserControl ÊôÐÔ½«¸ü¸ÄΪ False£º
Óû§´´½¨ Microsoft Access ʵÀý£¬¸ÃʵÀý½« UserControl ÊôÐÔÉèÖÃΪ True¡£ ÔÚ¿ØÖÆÆ÷Ó¦ÓóÌÐòÖÐÔËÐÐ Automation ´úÂ룬¸Ã´úÂëʹÓà GetObject() º¯Êý¼¤»îÒÔÇ°´ò¿ªµÄ Microsoft Access ʵÀý¡£ÊµÀýʹÓõĶÔÏó±äÁ¿Îª Public »òÄ£¿é¼¶±äÁ¿¡£ Óû§Ê¹Óà Windows ÈÎÎñÀ¸£¨»ò Windows NT Öеġ°ÈÎÎñÁÐ±í¡±£©»¹Ô Microsoft Access¡£ Óû§ÊÔͼͨ¹ýµ¥»÷¡°¹Ø±Õ¡±¿òÀ´¹Ø±Õ Microsoft Access¡£ÓÉÓÚ Automation ¿ØÖÆÆ÷ÓÐÒýÓøà Microsoft Access ʵÀýµÄ Public »òÄ£¿é¼¶¶ÔÏó±äÁ¿£¬ËùÒÔʵÀýûÓа´ÕÕÉèÏëµÄ·½Ê½¹Ø±Õ¡£¶øÊǽ«ÊµÀý×îС»¯£¬ÕâÑù½« UserControl ºÍ Visible ÊôÐÔÉèÖÃΪ False¡£ ͬÑù£¬Èç¹û·¢ÉúÒÔÏÂʼþ£¬»á½« UserControl ÊôÐÔ¸ü¸ÄΪ True¡£
ʹÓà Automaton н¨ Microsoft Access ʵÀý¡£UserControl ÊôÐÔΪ False¡£Visible ÊôÐÔҲΪ False£¬Òò´Ë£¬ÊµÀý±»×îС»¯¡£ Óû§Ê¹Óà Windows ÈÎÎñÀ¸£¨»ò Windows NT Öеġ°ÈÎÎñÁÐ±í¡±£©»¹Ô Microsoft Access¡£»òÕߣ¬ÔÚ Visual Basic Öе÷Óà ShowWindow() API º¯Êý»¹ÔʹÓôúÂëµÄʵÀý¡£ÔÚÉÏÊöÁ½ÖÖÇé¿öÏ£¬UserControl ºÍ Visible ÊôÐÔ½«¸ü¸ÄΪ True¡£ Èç¹û UserControl ÊôÐÔΪ True£¬Ëü¿ÉÒÔÓ°Ïì¿ØÖÆ Microsoft Access ÆÁÄ»ÏÔʾ·½Ê½µÄÄÜÁ¦¡£Ó¦¸ÃÌرð×¢ÒâÒÔÏÂÏÞÖÆ£º
ÔÚÊÔͼ½« Visual ÊôÐÔÉèÖÃΪ True ʱ£¬¿ÉÄÜ»áÔÚ Automation ´úÂëÖÐÊÕµ½´íÎóÏûÏ¢£»UserControl ÊôÐÔΪ True ʱ Visible ÊôÐÔΪֻ¶Á¡£ ÎÞ·¨Ïó OLE Server Ó¦ÓóÌÐòÒ»Ñù²¶»ñ»òÒÖÖÆ Microsoft Access Éú³ÉµÄ´íÎóÏûÏ¢¡£Èç¹ûÖ´ÐдíÎóµÄÃüÁÀýÈ磬ÊÔͼ´ò¿ªµ±Ç°Êý¾Ý¿âÖв»´æÔڵĴ°Ì壩£¬½«ÏÔʾ´íÎóÏûÏ¢¡£ µ±ÒýÓÃʵÀýµÄ¶ÔÏó±äÁ¿ (objAccess) ÉèÖÃΪ Nothing »ò´Ë±äÁ¿Ã»ÓÐÖ¸¶¨·¶Î§Ê±£¬Microsoft Access ʵÀý²»»á×Ô¶¯¹Ø±Õ¡£ Visible ÊôÐÔ
ÔÚ²»Í¬µÄÌõ¼þÏ£¬Visible ÊôÐÔ¿ÉÒÔΪֻ¶Á»ò¶Áд¡£µ± UserControl ÊôÐÔΪ True »ò Microsoft Access ʵÀý×î³õÓÉÓû§£¨¶ø²»ÊÇÓÉ Automation ´úÂ룩´´½¨Ê±£¬Visible ÊôÐÔΪֻ¶Á¡£µ± UserControl ÊôÐÔΪ False ʱ£¬Visible ÊôÐÔΪ¶Áд¡£µ«ÊÇ£¬ÔÚÉÙÊýÇé¿öÏ£¬Èç¹ûʵÀýµÄ Visible ÊôÐÔÒѾΪ True ²¢ÇÒÒѾ±»Óû§×îС»¯£¬½« Visible ÊôÐÔÉèÖÃΪ True ¿ÉÄÜÎÞ·¨¿´¼û Microsoft Access ʵÀý¡£ ²é¿´ Microsoft Access µÄʵÀý¡£
¸ù¾Ý¼¤»î Application ¶ÔÏóʱÊÇ·ñ´ò¿ª Microsoft Access£¬¿ÉÒÔͨ¹ý²»Í¬µÄ·½Ê½Ê¹Óà Automation ²é¿´ Microsoft Access µÄʵÀý¡£ ·½·¨ 1
ʹÓà Automation ´´½¨Ð嵀 Microsoft Access ʵÀýʱ£¬ÓÉÓÚ Application ¶ÔÏóµÄ Visible ÊôÐÔ±»×Ô¶¯ÉèÖÃΪ False£¬ËùÒÔʵÀý´¦ÓÚ×îС»¯×´Ì¬¡£ÒªÏÔʾ Microsoft Access£¬¿ÉÒÔ½« Visible ÊôÐÔÉèÖÃΪ True¡£ÀýÈ磺 Dim objAccess as Object Set objAccess = CreateObject("Access.Application") objAccess.Visible = True ÓÉÓÚ CreateObject() º¯Êý×ÜÊÇ´ò¿ªÖ¸¶¨Ó¦ÓóÌÐòµÄÐÂʵÀý£¬ËùÒÔÕâÖÖ·½·¨¿ÉÒÔÆð×÷Óá£Èç¹ûÖ±½ÓÔÚÔËÐÐ CreateObject() ºóÉèÖà Visible ÊôÐÔ£¬ÔòÓû§ÎÞ·¨Í¨¹ý»¹Ô»ò×îС»¯Ó¦ÓóÌÐò´°¿ÚÖØÉè UserControl ºÍ Visible ÊôÐÔÀ´¶ÔʵÀý½øÐиÉÔ¤¡£
µ«ÊÇ£¬Èç¹ûʹÓà GetObject() º¯Êý¶ø²»ÊÇ CreateObject() ²¢ÉèÖà Visible ÊôÐÔ£¬´úÂëÔÚÒÔÏÂÇé¿öÏ¿ÉÄܲ»Æð×÷Ó㺠ÔËÐдúÂëʱ Microsoft Access ÒѾÔËÐС£ÔÚ´ËÇé¿öÏ£¬UserControl ÊôÐÔ±»ÉèÖÃΪ True£¬ÕâÑù Visible ÊôÐÔΪֻ¶Á¡£ ʹÓà Automation ´ò¿ª Microsoft Access ʵÀý£¬ÓÉÓû§Ê¹ÓÃÈÎÎñÀ¸»¹ÔʵÀý£¬È»ºóÓÉÓû§×îС»¯ÊµÀý¡£ ±¸×¢£ºÒª±Ü¿ªÕâÁ½¸öÏÞÖÆ£¬¿ÉÒÔʹÓ÷½·¨ 2 ²é¿´ Microsoft Access ʵÀý¡£ ·½·¨ 2
Èç¹ûÒª²é¿´ Microsoft Access ʵÀý¶ø²»¹ÜÓû§µÄ¸ÉÔ¤£¬¿ÉÒÔʹÓÃÏÂÃæËùÁÐµÄ ShowAccess() ¹ý³Ì¡£´ËʾÀýº¯Êýͨ¹ýÈý¸ö Windows API µ÷Óú¯ÊýÀ´¿ØÖÆ Microsoft Access ʵÀý£¬²»¿¼Âǵ±Ç°Æä UserControl ºÍ Visible ÊôÐÔµÄÉèÖÃÈçºÎ¡£ ---------------------------------------------------------------------- DECLARATIONS ----------------------------------------------------------------------
Option Explicit Declare Function SetForegroundWindow Lib "User32" _ (ByVal hWnd As Long) As Long Declare Function IsIconic Lib "User32" _ (ByVal hWnd As Long) As Long Declare Function ShowWindow Lib "User32" _ (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long Const SW_NORMAL = 1 Show window in normal size Const SW_MINIMIZE = 2 Show window minimized Const SW_MAXIMIZE = 3 Show window maximized Const SW_SHOW = 9 Show window without changing window size Dim objAccess As Object module-level declaration ---------------------------------------------------------------------- This procedure brings the instance of Microsoft Access referred to as "instance" into view. The instances window size can be SW_NORMAL, SW_MINIMIZE, SW_MAXIMIZE, or SW_SHOW. If size is omitted, the window is not changed (SW_SHOW). To call this function, use this syntax: ShowAccess instance:=objAccess, size:=SW_SHOW ---------------------------------------------------------------------- Sub ShowAccess(instance As Object, Optional size As Variant) Dim hWnd As Long, temp As Long If IsMissing(size) Then size = SW_SHOW On Error Resume Next If Not instance.UserControl Then instance.Visible = True On Error GoTo 0 turn off error handler hWnd = instance.hWndAccessApp temp = SetForegroundWindow(hWnd) If size = SW_SHOW Then keep current window size If IsIconic(hWnd) Then temp = ShowWindow(hWnd, SW_SHOW) Else If IsIconic(hWnd) And size = SW_MAXIMIZE Then _ temp = ShowWindow(hWnd, SW_NORMAL) temp = ShowWindow(hWnd, size) End If End Sub ·½·¨ 3 Èç¹ûʹÓÃ×Ô¶¯´úÂëÔËÐÐʱÒѾÔËÐÐµÄ Microsoft Access ʵÀý²é¿´Ìض¨µÄÊý¾Ý¿â£¬¿ÉÒÔʹÓà ShowAccess() ¹ý³Ì£¨·½·¨ 2 ÖÐËùÊö£©ÒÔ¼°ÏÂÁÐʾÀý´úÂ룺 ---------------------------------------------------------------------- This procedure opens the sample database Northwind.mdb in a new or existing instance of Microsoft Access (if one is already open).
NOTE: This procedure uses the ShowAccess() procedure (listed above for Method 2). You must enter this procedure into the same module as ShowAccess() for the code to run properly. ---------------------------------------------------------------------- Sub OpenNorthwind() Dim path as String On Error Resume Next temporary error handling Set objAccess = GetObject(,"Access.Application") If Err <> 0 Then no existing instances of Access Set objAccess = CreateObject("Access.Application") End If On Error GoTo OpenNorthwind_ErrHandler normal error handler ShowAccess instance:=objAccess, size:=SW_MAXIMIZE With objAccess path = .SysCmd(Access.acSysCmdAccessDir) & "Samples\Northwind.mdb" If .DBEngine.Workspaces(0).Databases.Count = 0 Then .OpenCurrentDatabase filepath:=path ElseIf LCase(Right(.CurrentDb.Name, Len("northwind.mdb"))) _ <> "northwind.mdb" Then .CloseCurrentDatabase .OpenCurrentDatabase filepath:=path End If .DoCmd.OpenForm FormName:="Main SwitchBoard" End With Exit Sub OpenNorthwind_ErrHandler: MsgBox Error$(), , "Open Northwind" End Sub ¹Ø±Õ Microsoft Access µÄʵÀý ͨ³££¬µ±ÒýÓÃʵÀýµÄ¶ÔÏó±äÁ¿±»ÉèÖÃΪ Nothing »òÔÚ¿ØÖÆÆ÷Ó¦ÓóÌÐòÖÐûÓÐÖ¸¶¨·¶Î§Ê±£¬Microsoft Access µÄʵÀý»á×Ô¶¯¹Ø±Õ¡£µ«ÊÇ£¬Èç¹ûÔÚÒÔÏÂÊÓͼÖдò¿ªÈκΠMicrosoft Access ¶ÔÏó£¬ÔòʵÀý²»»áÏóÆÚÍûµÄÄÇÑù×Ô¶¯¹Ø±Õ£º ¶ÔÏó ÊÓͼ ---------------------- Table Datasheet Design Query Datasheet Form Form Report Print Preview µ± Microsoft Access ¶ÔÏó´ò¿ªÊ±£¬Ö±µ½¶ÔÏó¹Ø±ÕÇÒ Application ¶ÔÏóµÄ UserControl ÊôÐÔΪ False ʱ£¬´ËʵÀý²Å¹Ø±Õ¡£µ«ÊÇ£¬Í¨¹ýʹÓà Application ¶ÔÏóµÄ¡°Í˳ö¡±·½·¨¿ÉÒÔÇ¿ÐйرÕʵÀý¡£ÀýÈ磬ÏÂÃæµÄʾÀý´úÂëʹÓá°Í˳ö¡±·½·¨¹Ø±Õ Microsoft Access µÄËùÓÐʵÀý¡£ ---------------------------------------------------------------------- DECLARATIONS ----------------------------------------------------------------------
Option Explicit ---------------------------------------------------------------------- This procedure closes all open instances of Microsoft Access. Once all instances are closed, the error handler is run and the procedure ends. ---------------------------------------------------------------------- Sub CloseAllAccess() Dim objAccess As Object On Error GoTo CloseAllAccess_ErrHandler Do Set objAccess = GetObject(,"Access.Application") objAccess.Quit Loop CloseAllAccess_ErrHandler: Set objAccess = Nothing End Sub ¾¯¸æ£ºÈç¹û´úÂëµ÷ÓÃÆäËü Microsoft Access º¯Êý£¬Ôò²»µÃʹÓá°Í˳ö¡±·½·¨¹Ø±Õͨ¹ýÖ±½Óµ÷Óà Microsoft Access º¯Êý´´½¨µÄʵÀý¡£ÓйØÔÚ Automation ´úÂëÖÐʹÓà Microsoft Access º¯ÊýµÄÏêϸÐÅÏ¢£¬Çë²ÎÔı¾ÎÄÉÔºóµÄ¡°µ÷Óà Microsoft Access º¯Êý¡±²¿·Ö¡£ ±¸×¢£ºÍ¨¹ý½«¶ÔÏó±äÁ¿ÉèÖÃΪģ¿é¼¶»ò public ±äÁ¿¶ø²»Êǹý³Ì¼¶±äÁ¿£¬¿ÉÒÔ·ÀÖ¹ÔÚÆä¶ÔÏó±äÁ¿ (objAccess) ûÓÐÖ¸¶¨·¶Î§Ê±¹Ø±Õ Microsoft Access ʵÀý¡£ ÀýÈ磬Èç¹û objAccess ÊÇÔÚ¹ý³ÌÖÐÉùÃ÷µÄ£¬ÄÇôËüÖ»ÔÚ¹ý³ÌÔËÐÐʱ¿ÉÓ᣹ý³Ì½áÊøºó£¬objAccess ûÓÐÖ¸¶¨·¶Î§ÇÒ Microsoft Access ¿ÉÒÔ×Ô¶¯¹Ø±Õ¡£ µ«ÊÇ£¬Èç¹û objAccess ÊÇÔÚ±ê׼ģ¿éµÄ Declaration ²¿·ÖÖÐÉùÃ÷µÄ£¬ÄÇôËüÊÇÄ£¿é¼¶±äÁ¿£¬¿ÉÒÔÓÃÓÚ¸ÃÄ£¿éµÄËùÓйý³Ì¡£Èç¹û objAccess ÔÚ±ê׼ģ¿éÖÐÊÇ×÷Ϊ Public ±äÁ¿ÉùÃ÷µÄ£¬ÄÇôËü¿ÉÒÔÔÚÊý¾Ý¿âµÄËùÓйý³ÌÖÐʹÓá£ÔÚÕâÁ½ÖÖÇé¿öÏ£¬Automation ´úÂë¿ÕÏÐʱ objAccess ÓÐÖ¸¶¨µÄ·¶Î§¡£Òò´Ë£¬ÔÚʹÓá°Í˳ö¡±·½·¨»ò¹Ø±Õ Automation ¿ØÖÆÆ÷֮ǰ£¬Ò»Ö±±£³ÖÓë Microsoft Access µÄÁ¬½Ó¡£ ´ò¿ªÊý¾Ý¿âʱÈƹýÆô¶¯ÉèÖà µ±×÷Ϊ Automation Server ʹÓà Microsoft Access ʱ£¬¿ÉÒÔͨ¹ý²»Í¬µÄ·½Ê½´ò¿ªÌض¨µÄÊý¾Ý¿â¡£ÀýÈ磬¿ÉÒÔʹÓà GetObject() º¯Êý²¢Ö¸¶¨Êý¾Ý¿â¡£»òÕߣ¬¿ÉÒÔʹÓà Application ¶ÔÏóµÄ OpenCurrentDatabase ·½·¨´ò¿ªÏÖÓÐÊý¾Ý¿â½«Æä×÷Ϊµ±Ç°Êý¾Ý¿â¡£
Èç¹ûÊý¾Ý¿âÓÐ×Ô¶¨Òå¡°Æô¶¯¡±ÉèÖûò AutoExec ºê£¬Ê¹Óà OpenCurrentDatabase ·½·¨Ê±ÕâЩ¹¦ÄÜÊÇÔËÐеġ£µ«ÊÇ£¬´ò¿ªÊý¾Ý¿âʱ¿ÉÒÔʹÓà SendKeys Óï¾äÄ£Äâ°´Ï SHIFT ¼üµÄ²Ù×÷£¬Õ⽫Èƹý Startup ÉèÖÃºÍ AutoExec ºê¡£ÀýÈ磬¿ÉÒÔʹÓÃÏÂÃæµÄʾÀý´úÂ룺 Dim objAccess as object Set objAccess = CreateObject("Access.Application") ShowAccess instance:=objAccess, size:=SW_MAXIMIZE SendKeys "+" Simulates holding down the SHIFT key as the database is being opened objAccess.OpenCurrentDatabase filepath:= _ "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb" ΪÁËʹ SendKeys Óï¾äÕýÈ·ÔËÐУ¬´úÂëÓ¦Âú×ãÒÔÏÂÌõ¼þ£º ´úÂëӦʹÓúóÆڰ󶨣¬ÕâÒâζ×ÅÒýÓà Microsoft Access ʵÀýµÄ±äÁ¿±»ÉùÃ÷Ϊ¶ÔÏó£¬ÀýÈ磺 Dim objAccess as Object Set objAccess = CreateObject("Access.Application") ²»µÃʹÓÃÔçÆڰ󶨣¬ÔçÆÚ°ó¶¨Ê¹ÓÃÓ¦ÓóÌÐò¶¨ÒåµÄ¶ÔÏóÀàÐÍÉùÃ÷±äÁ¿£¬ÀýÈ磺 Dim objAccess as Access.Application Set objAccess = CreateObject("Access.Application")
´úÂëÈ·±£ Microsoft Access ´¦ÓÚÏÔʾ״̬£¬²¢ÇÒÊó±êÖ¸ÕëλÓÚ Microsoft Access ´°¿ÚÖС£Èç¹û Microsoft Access ²»Êǻ´°¿Ú£¬ÔòÔËÐÐij¸ö¹ý³Ì£¬Èç ShowAccess() º¯Êý£¨ÒѾÔÚÇ°ÃæµÄ¡°²é¿´ Microsoft Access ʵÀý¡±²¿·Ö¸øÓè˵Ã÷£©¡£ ÕýÔÚÓà Automation ¿ØÖƵÄÊý¾Ý¿â½«Æä AllowBypassKey ÊôÐÔÉèÖÃΪ True£¨Ä¬ÈÏÉèÖã©¡£ OpenCurrentDatabase ·½·¨Ö±½Ó¸úÔÚ SendKeys Óï¾äÖ®ºó£¬ÇÒ²»°üº¬¼ÆËã¡£ÀýÈ磬²»Ê¹ÓÃÒÔÏ´úÂ룺 objAccess.OpenCurrentDatabase filepath:= _ objAccess.SysCmd(Access.acSysCmdAccessDir) &"Samples\Northwind.mdb" SendKeys "+" ¶øʹÓÃÏÂÁÐʾÀý´úÂ룺 path = objAccess.SysCmd(Access.acSysCmdAccessDir) & _ "Samples\Northwind.mdb" SendKeys "+" objAccess.OpenCurrentDatabase filepath:=path µ÷Óà Microsoft Access º¯Êý¡£ ÔÚ Automation ´úÂëÖУ¬Ö»ÒªÔÚ Application ¶ÔÏó¿ÉÒÔ¿´µ½¹ý³Ì£¬¾Í¿ÉÒÔµ÷ÓÃÄÚǶ Microsoft Access º¯Êý£¬Èç Eval()¡¢SysCmd() »ò Dlookup()¡£Òª²éÃ÷º¯ÊýÊÇ·ñ¿ÉÓã¬ÇëÖ´ÐÐÏÂÃæµÄ²Ù×÷£º
´ò¿ªÄ£¿é¡£ ÔÚ¡°¹¤¾ß¡±²Ëµ¥ÉÏ£¬µ¥»÷¡°ÒýÓᱡ£ ÔÚ¡°ÒýÓ᱿òÖУ¬Ñ¡Ôñ Microsoft Access 8.0 ¶ÔÏó¿â£¨»ò Microsoft Access for Windows 95 7.0 °æ£©£¬È»ºóµ¥»÷¡°È·¶¨¡±¡£ ÔÚ¡°²é¿´¡±²Ëµ¥ÉÏ£¬µ¥»÷¡°¶ÔÏóä¯ÀÀÆ÷¡±¡£ ÔÚ¡°¶ÔÏóä¯ÀÀÆ÷¡±¿òµÄ¡°¿â/Êý¾Ý¿â¡±Ï£¬Ñ¡Ôñ¡°Access¡±£¨»ò¡°Access - Microsoft Access for Windows 95¡± 7.0 °æ£©¡£ ÔÚÀࣨ»òÄ£¿é/Àà 7.0 °æ£©Ï£¬µ¥»÷ Application¡£×¢ÒâÁгöµÄ Application ¶ÔÏóµÄº¯ÊýÏÔʾÔÚ¡°³ÉÔ±¡±¿òÖУ¨ÔÚ 7.0 °æÖÐÏÔʾÔÚ¡°·½·¨/ÊôÐÔ¡±¿òÖУ©¡£¿ÉÒÔÔÚ Automation ´úÂëÖÐʹÓÃÆäÖÐÈÎÒâÒ»¸öº¯Êý¡£ ÔÚ Automation ´úÂëÖÐʹÓà Microsoft Access º¯Êýʱ£¬¿ÉÒÔʹÓà Application ¶ÔÏóÖ±½Óµ÷Óú¯Êý£¬»òÕßʹÓÃÉèÖÃΪ Microsoft Access ʵÀýµÄ¶ÔÏó±äÁ¿¼ä½Óµ÷Óú¯Êý¡£ÒÔÏÂÏêϸÃèÊöÕâÁ½ÖÖ¼¼Êõ¡£ Ö±½Óµ÷Óà Microsoft Access º¯Êý
ÒªÖ±½Óµ÷Óà Microsoft Access º¯Êý£¬Ê×ÏȱØÐëÔÚ Automation ¿ØÖÆÆ÷ÖÐÓÐ¶Ô Microsoft Access 8.0 ¶ÔÏó¿â£¨»ò Microsoft Access for Windows 95 7.0 °æ£©µÄÒýÓá£Óйش´½¨ÒýÓõÄÏêϸÐÅÏ¢£¬Çë²ÎÔı¾Îĵġ°´´½¨ Microsoft Access µÄÒýÓá±²¿·Ö¡£
´´½¨¶Ô Microsoft Access µÄÒýÓú󣬿ÉÒÔʹÓá°Access¡±Application ¶ÔÏóµ÷Óà Microsoft Access º¯Êý£¬ÀýÈ磺 MsgBox Access.Eval("2+2") displays "4" MsgBox Access.SysCmd(Access.acSysCmdAccessDir) displays the path Automation ¿ØÖÆÆ÷Ê×´ÎÖ±½Óµ÷Óà Microsoft Access º¯Êýʱ£¬´´½¨ÐµÄ×îС»¯µÄ Microsoft Access ʵÀý¡£¿ØÖÆÆ÷±£³ÖÓë¸ÃʵÀýÖ®¼äµÄÁ¬½Ó£¬ÒÔ·À´úÂëµ÷ÓÃÆäËüµÄ Microsoft Access º¯Êý¡£Ö±µ½¹Ø±Õ¿ØÖÆÆ÷Ó¦ÓóÌÐò֮ǰ£¬¸ÃÁ¬½ÓÒ»Ö±±£³ÖÓÐЧ¡£ ±¸×¢£º²»µÃʹÓà Application ¶ÔÏóµÄ¡°Í˳ö¡±·½·¨¹Ø±Õͨ¹ýÖ±½Óµ÷Óà Microsoft Access º¯Êý´´½¨µÄʵÀý¡£Õâ»áÔÚºóÀ´µ÷Óà Microsoft Access º¯ÊýʱÔÚ¿ØÖÆÆ÷Öе¼Ö Automation ´íÎó¡£Îª±ÜÃâ¿ÉÄܵĴíÎ󣬿ÉÒÔÔڹرտØÖÆÆ÷ʱÈÿØÖÆÆ÷×Ô¶¯¹Ø±Õ×îС»¯µÄʵÀý¡£ ¼ä½Óµ÷Óà Microsoft Access º¯Êý Òª¼ä½Óµ÷Óà Microsoft Access º¯Êý£¬²»ÐèÒª´´½¨¶ÔÏó¿âµÄÒýÓã¨ÓëÖ±½Óµ÷ÓÃʱµÄÇé¿ö²»Í¬£©¡£ÔÚ Automation ´úÂëÖУ¬¿ÉÒÔʹÓÃΪ Microsoft Access ʵÀýÉèÖõĶÔÏó±äÁ¿À´µ÷Óà Microsoft Access º¯Êý£¬ÀýÈ磺 Dim objAccess as Object On Error Resume Next Set objAccess = GetObject(,"Access.Application") If Err <> 0 Then no instance of Access is open Set objAccess = CreateObject("Access.Application") End If MsgBox objAccess.Eval("2+2") displays 4 MsgBox objAccess.SysCmd(Access.acSysCmdAccessDir) displays the path µ÷ÓÃ×Ô¶¨Òå¹ý³Ì
ÔÚ Automation ´úÂëÖУ¬Í¨¹ýʹÓà Application ¶ÔÏóµÄ¡°ÔËÐС±·½·¨µ÷Óôæ·ÅÔÚ Microsoft Access Êý¾Ý¿âÖеÄ×Ô¶¨Òå Visual Basic ¹ý³Ì¡£×Ô¶¨Òå¹ý³Ì±ØÐë×÷Ϊ Public ÉùÃ÷²¢ÇÒҪλÓÚ±ê׼ģ¿é£¨·Ç´°Ìå»ò±¨±íÄ£¿é£©ÖС£ÀýÈ磬¿ÉÒÔÏò Northwind.mdb ʾÀýÊý¾Ý¿âµÄÐÂÄ£¿éÖÐÌí¼ÓÏÂÃæµÄº¯Êý£º Public Function MyDateAdd(interval As String, number As Integer, _ startdate As Date) As Date MyDateAdd = DateAdd(interval, number, startdate) Calls the Microsoft Access built-in DateAdd function. End Function ÒªÔËÐÐÉÏÊöº¯Êý£¬Ç뽫ÏÂÃæµÄʾÀý´úÂëÌí¼Óµ½ Automation ¿ØÖÆÆ÷ÖС£ Dim objAccess as Object, newdate as Date Set objAccess = GetObject _ ("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb") newdate = objAccess.Run("MyDateAdd", "m", 1, Date) MsgBox newdate,,"MyDateAdd returned" ʹÓÃÔËÐÐʱӦÓóÌÐò
Èç¹ûҪʹÓà Automation ¿ØÖÆÔËÐÐʱ Microsoft Access Ó¦ÓóÌÐò£¬ÐèÒª¶Ô´úÂë½øÐÐһЩ¸ü¸Ä£¬ÌرðÊÇÓû§¼ÆËã»úÉÏûÓа²×°ÍêÕûµÄ Microsoft Access ÁãÊÛ°æʱ£º
Èç¹ûûÓÐÊý¾Ý¿â£¬Microsoft Access ÔËÐÐʱ°æ±¾¾ÍÎÞ·¨Æô¶¯£¬Òò´ËÊ×ÏÈÓ¦¸ÃÑé֤ʵÀýÊÇ·ñÒѾÔËÐС£Èç¹ûûÓÐÔËÐУ¬Ó¦Ê¹Óà Shell() º¯Êý´ò¿ªÔËÐÐʱ°æ±¾²¢Ö¸¶¨ Msaccess.exe ºÍÊý¾Ý¿âµÄ·¾¶¡£ ´úÂëÆô¶¯ Microsoft Access ÔËÐÐʱʵÀýºó£¬Ê¹Óà GetObject()to ÒýÓôËʵÀý¡£ È·±£ÔÚ´ò¿ªÊý¾Ý¿âÇ°´úÂëûÓÐÏÔʾÔËÐÐʱʵÀý¡£·ñÔò£¬ÊµÀý½«ÔÚÆÁÄ»É϶ÌÔÝÏÔʾ£¬È»ºó±äΪ×îС»¯¡£ Èç¹ûÒª¹Ø±Õ Microsoft Access µÄÔËÐÐʱʵÀý£¬ÇëʹÓà Application ¶ÔÏóµÄ¡°Í˳ö¡±·½·¨£¬ÀýÈ磺 objAccess.Quit
Èç¹ûÒª´ò¿ª Microsoft Access ÔËÐÐʱʵÀý£¬¿ÉÒÔʹÓÃÏÂÃæµÄʾÀý¹ý³Ì£º ---------------------------------------------------------------------- DECLARATIONS ---------------------------------------------------------------------- Option Explicit Dim objAccess as Object ---------------------------------------------------------------------- This procedure sets a module-level variable, objAccess, to refer to an instance of Microsoft Access. The code first tries to use GetObject to refer to an instance that might already be open and contains the specified database (dbpath). If the database is not already open in an instance of Microsoft Access, a new instance of the full version of Microsoft Access is opened. If the full version of Microsoft Access is not installed, the Shell() function starts a run-time instance of Microsoft Access. Once the instance is opened, you can use the CloseCurrentDatabase and OpenCurrentDatabase methods to work with other databases. ---------------------------------------------------------------------- Sub OpenRunTime() Dim accpath As String, dbpath As String On Error Resume Next dbpath = "C:\My Application\MyApp.mdb" Set objAccess = GetObject(dbpath) If Err <> 0 Then If Dir(dbpath) = "" Then dbpath is not valid MsgBox "Couldnt find database." Exit Sub Else The full version of Microsoft Access is not installed. accpath = "C:\Program Files\Common Files\Microsoft Shared" & _ "\Microsoft Access Runtime\MSAccess.exe" If Dir(accpath) = "" Then MsgBox "Couldnt find Microsoft Access." Exit Sub Else Shell pathname:=accpath & " " & Chr(34) & dbpath & Chr(34), _ windowstyle:=6 Do Wait for shelled process to finish Err = 0 Set objAccess = GetObject(dbpath) Loop While Err <> 0 End If End If End If End Sub ʹÓð²È«¹¤×÷×é Èç¹ûÒª¿ØÖÆµÄ Microsoft Access Ó¦ÓóÌÐòʹÓÃÁË°²È«¹¤×÷×é (System.mdw)£¬Ôò¿ÉÄÜÏëÈƹýÒªÇóÌṩÓû§ÃûºÍÃÜÂëµÄµÇ¼¿ò¡£ÏÂÃæµÄʾÀý´úÂëʹÓà Shell() º¯ÊýÆô¶¯ Microsoft Access ²¢½«Óû§ÃûºÍÃÜÂë´«¸øÓ¦ÓóÌÐò£º ---------------------------------------------------------------------- DECLARATIONS ----------------------------------------------------------------------
Option Explicit Dim objAccess as Object ---------------------------------------------------------------------- This procedure sets a module-level variable, objAccess, to refer to an instance of Microsoft Access. The code first tries to use GetObject to refer to an instance that might already be open. If an instance is not already open, the Shell() function opens a new instance and specifies the user and password, based on the arguments passed to the procedure. Calling example: OpenSecured varUser:="Admin", varPw:="" ---------------------------------------------------------------------- Sub OpenSecured(Optional varUser As Variant, Optional varPw As Variant) Dim cmd As String On Error Resume Next Set objAccess = GetObject(, "Access.Application") If Err <> 0 Then no instance of Access is open If IsMissing(varUser) Then varUser = "Admin" cmd = "C:\Program Files\Microsoft Office\Office\MSAccess.exe" cmd = cmd & " /nostartup /user " & varUser If Not IsMissing(varPw) Then cmd = cmd & " /pwd " & varPw Shell pathname:=cmd, windowstyle:=6 Do Wait for shelled process to finish. Err = 0 Set objAccess = GetObject(, "Access.Application") Loop While Err <> 0 End If End Sub ʾÀý ±¾²¿·Ö°üº¬Í¨¹ý Automation ¿ØÖÆÆ÷£¨Èç Microsoft Excel¡¢Microsoft Project »ò Microsoft Visual Basic£©¿ØÖÆ Microsoft Access ¹¦ÄܵÄÁ½¸öʾÀý³ÌÐò¡£ÕâÁ½¸ö³ÌÐòÖ´ÐÐÒÔÏÂÈÎÎñ£º Ô¤ÀÀ»ò´òÓ¡±¨±í£¬µ÷Óá°±¨±íÏòµ¼¡±´´½¨Ð±¨±í¡£
±¸×¢£ºÔÚ Automation ¿ØÖÆÆ÷Ó¦ÓóÌÐòÖÐÕâЩʾÀý³ÌÐòÐèÒª¶Ô Microsoft Access ¶ÔÏó¿âµÄÒýÓá£Óйش´½¨ÒýÓõÄÏêϸÐÅÏ¢£¬Çë²ÎÔı¾ÆªÖеġ°´´½¨ Microsoft Access µÄÒýÓá± Ô¤ÀÀ»ò´òÓ¡±¨±í ¿ÉÒÔʹÓÃÏÂÃæµÄʾÀý³ÌÐò£¬Í¨¹ý Automation ¿ØÖÆÆ÷´òÓ¡»òÔ¤ÀÀ Microsoft Access Öеı¨±í£º ---------------------------------------------------------------------- DECLARATIONS ----------------------------------------------------------------------
Option Explicit ---------------------------------------------------------------------- This procedure prints or previews a report, and then closes the current instance of Microsoft Access (because objAccess is a procedure-level variable). To call this procedure, use the following syntax: PrintAccessReport _ dbname:= _ "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", rptname:="Sales by Category", preview:=True ---------------------------------------------------------------------- Sub PrintAccessReport(dbname As String, rptname As String, _ preview As Boolean) Dim objAccess As Object On Error GoTo PrintAccessReport_ErrHandler Set objAccess = CreateObject("Access.Application") With objAccess .OpenCurrentDatabase filepath:=dbname If preview Then Preview report on screen. .Visible = True .DoCmd.OpenReport reportname:=rptname, _ view:=Access.acPreview Else Print report to printer. .DoCmd.OpenReport reportname:=rptname, _ view:=Access.acNormal DoEvents Allow report to be sent to printer. End If End With Set objAccess = Nothing Exit Sub PrintAccessReport_ErrHandler: MsgBox Error$(), , "Print Access Report" End Sub µ÷Óá°±¨±íÏòµ¼¡±Ð½¨±¨±í ¿ÉÒÔʹÓÃÏÂÃæµÄʾÀý³ÌÐò´Ó Automation ¿ØÖÆÆ÷Öдò¿ª Microsoft Access Öеġ°±¨±íÏòµ¼¡±£º ---------------------------------------------------------------------- DECLARATIONS ----------------------------------------------------------------------
Option Explicit Dim objAccess as Object ---------------------------------------------------------------------- This procedure starts the Report Wizard in Microsoft Access using a specified database and table (or query) as the record source. This procedure does not close the instance of Microsoft Access because objAccess is a module-level variable. To call this procedure, use the following syntax: CallReportWizard _ dbname:= _ "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", sourcetype:="table", sourcename:="Employees" ---------------------------------------------------------------------- Sub CallReportWizard(dbname As String, sourcetype As String, _ sourcename As String) Dim objtype As Integer On Error GoTo CallReportWizard_ErrHandler Set objAccess = CreateObject("Access.Application") With objAccess .Visible = True .OpenCurrentDatabase filepath:=dbname If LCase(sourcetype) = "table" Then objtype = Access.acTable Else objtype = Access.acQuery End If .DoCmd.SelectObject objecttype:=objtype, _ objectname:=sourcename, inDatabaseWindow:=True Although the following line of code works in Microsoft Access 97, DoMenuItem exists only for backward compatibility. In Microsoft Access 97, you should use the following RunCommand method instead: .DoCmd.RunCommand (acCmdNewObjectReport) .DoCmd.DoMenuItem MenuBar:=1, MenuName:=3, Command:=3, _ Version:=Access.acMenuVer70 Database menubar, Insert menu, Report command End With Exit Sub CallReportWizard_ErrHandler: If Err <> 2501 Then Error did not occur by canceling Report Wizard. MsgBox Error$(), , "Call Report Wizard" End If End Sub
--------------------------------------------------------------------------------
Ïà¹ØÎÄÕÂ
ûÓÐÏà¹ØÎÄÕÂ
|