首页 > 解决方案 > 从 Microsoft Outlook 在 Microsoft Access 中运行附加查询时缺少字段

问题描述

目标:
从 Microsoft Outlook 中,我想运行位于 Microsoft Access 数据库中的追加查询,它将在“日志”表中创建记录,其中包含有关已发送电子邮件的信息。(附加由事件“Application_ItemSend”触发)

问题:
当我运行查询时,Access 仅附加七个字段中的两个:变量“strSenderEmail”和插入查询中的函数“now()”-> 我的意思是在表“日志”中我只有关于发件人的信息和日期,但主题、接收者等为空。

附加信息:
- 参数名称 -> 我没有任何信息表明某些内容不正确或缺失
- 所有参数均已输入
- 我尝试交替传递参数:“qdf.parameters(1)=test”但不起作用
- 如果我从访问运行此查询并“从手”插入参数一切正常。

问题:
为什么我的查询没有正确附加?
或者,我将尝试使用 SQL 命令插入数据或将记录集添加到表中。

功能代码:

strBackendPath - path of backend access
Qapp_001 - name of the append query in access

Set dbs = DBEngine.OpenDatabase(strBackendPath) 'set connection with backend 
Set qdf = dbs.QueryDefs(Qapp_001)               'set update merged processes query 
<br>
qdf![strSenderEmail] = GetOriginalSenderAddress(objMailItem)  'this is external function, this variable is actually working properly -> information are added in new record
qdf!strReceiverEmail = "test1"      'insert receiver 
qdf!strSubject = "test2"            'subject of the e-mail 
qdf!struserUID = "test3"            '(Environ$("Username"))         
qdf!bytCommunicationType = 1        'define type of communication 
qdf!bytStatus = 1                   'define status

qdf.Execute                         'run append query 
<br>
Set qdf = Nothing   'clear query 
Set dbs = Nothing   'clear database 


日志表中的列:

ID  SenderEmail ReceiverEmail   Subject PreparationDateAndTime  userUID CommunicationType

追加查询的 SQL 代码(来自 Access):

    PARAMETERS 
strSenderEmail LongText, strReceiverEmail LongText, strSubject LongText, struserUID Text ( 255 ), bytCommunicationType Byte, bytStatus Byte;    
    INSERT INTO 
Tbl_02_Log ( SenderEmail, ReceiverEmail, Subject, PreparationDateAndTime, userUID, CommunicationType, Status )
    SELECT 
[strSenderEmail] AS Expr1, [strReceiverEmail] AS Expr2, [strSubject] AS Expr3, Now() AS Expr4, [struserUID] AS Expr5, [bytCommunicationType] AS Expr6, [bytStatus] AS Expr7;

标签: vbams-accessoutlook

解决方案


只是总结一下。我没有找到运行内置访问附加查询而没有错误的解决方案。

我编写了一个使用数据库引擎运行的 SQL 代码,有一些全局/私有变量,但解决方案的想法如下:

Dim dbe As Object                       'dao database
Dim dbs As Object                       'access database with actual data
Dim strSender As String                 'address of group mail, from which e-mail is being sent
Dim strReceiver As String               'consolidated string of recipients: To + CC + Bcc
Dim strSubject As String                'subject of sent e-mail
Dim strUID As String                    'uid of the user who sent e-mail
Dim strSQL As String                    'SQL code of append query, which adds new log record to database

Set dbe = CreateObject("DAO.DBEngine.120")  'version on win7 and win10 is 3.6
Set dbs = dbe.OpenDatabase(GC_BackendPath, False, False, ";pwd=" & GC_Password & "") 'set connection with backend

strSender = "'" & G_OriginalSender & "'"        'get sender e-mail - group mailbox
strReceiver = G_MailReceiver                    'assign recipient list
strSubject = G_MailSubject                      'assign subject of email

strUID = "'" & CreateObject("wscript.Network").UserName & "'"   'get user UID and assign under variable

strSubject = "'" & Replace(Mid(strSubject, 2, Len(strSubject) - 2), "'", "") & "'"
strSQL = "INSERT INTO " & Tbl_02 & " ( SenderEmail, ReceiverEmail, Subject, PreparationDateAndTime, userUID, CommunicationType, Status) SELECT " & strSender & " AS SenderEmail, " & strReceiver & " AS ReceiverEmail, " & strSubject & " AS Subject, Now() AS PreparationDateAndTime, " & strUID & " AS userUID, " & bytCommunication & " AS CommunicationType, " & bytStatus & " AS Status;"

Set dbs = dbe.OpenDatabase(GC_BackendPath)      'set database which will be updated with new record log

dbs.Execute strSQL                              'run SQL code for given database
dbs.Close                                   'close database
Set dbs = Nothing

推荐阅读