目标:从Microsoft Outlook,我要运行位于Microsoft Access数据库中的追加查询,该查询将在“日志”表中创建记录,其中包含有关已发送电子邮件的信息。 (追加由事件“ Application_ItemSend”触发)
问题:当我运行查询时,Access在七个字段中仅追加两个:变量“ strSenderEmail”和函数“ now()”,该字段插入查询->这意味着在表“ Log”中,我仅具有有关发件人和日期的信息,但主题,接收者等为空。
其他信息:-参数名称->我不知道有什么不正确或缺失的信息-输入所有参数-我尝试传递参数:“ 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;
总结一下。我没有找到运行内置访问附加查询而没有错误的解决方案。
我编写了一个使用数据库引擎运行的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