Class语句中的SQL Select语句需要If语句中的表字段

时间:2014-01-31 21:55:56

标签: sql vba ms-access ms-access-2007 access-vba

我正在使用带有VBA SQL语句的MS Access 2007构建一个类模块,以发送文件并发送两封不同的电子邮件。我继续为Status_Tbl中的字段MWStatus定义变量。我现在对所有代码都视而不见,需要帮助。我在表单后面的模块中使用了SQL语句并且它有效,但是正在减慢表单,所以我正在移动SQL语句。我的问题是,为什么Status_Tbl中的Field MWStatus给出了错误“Variable not defined”。这是我第一次将SQL语句放在VBA模块中,我不知道我是否做得对

Public Sub SendConfirm()
On Error GoTo Err_SendConfirm_Click

Dim Borrower As String, LOEmail As String, ProcEmail As String, ClsEmail As String, Caution As String, LNumber As Long, TheFile As String, TheName As String

'SQL Statement to get Processor and Closer email
Dim strSQL As String
Dim strCMCID As String
strCMCID = Me!CMCID_Txt.Value
strSQL = "SELECT Commitments_Tbl.CMCID, Status_Tbl.MWStatus, DBUsers_Tbl.EMail, DBUsers_Tbl_1.EMail " & _
"FROM ((Commitments_Tbl LEFT JOIN Status_Tbl ON Commitments_Tbl.LoanNumber = Status_Tbl.LoanNumber) LEFT JOIN DBUsers_Tbl AS DBUsers_Tbl_1 ON Status_Tbl.Processor = DBUsers_Tbl_1.MWName) LEFT JOIN DBUsers_Tbl ON Status_Tbl.Closer = DBUsers_Tbl.MWName " & _
"WHERE Commitments_Tbl.CMCID)= ' " & strCMCID & " ' ; "

'Message Box
Dim Msg, Style, Title, Response

LOEmail = Me!OrigID_Cbo.Column(3)
Borrower = Me!BorrNameL_Txt
LNumber = Nz(Me!LoanNumber_Txt, 0)

Msg = "Do you want to send an e-mail to Set_up?"
Style = vbYesNo
Title = "Cancel Set-Up E-Mail"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
    GoTo line3
Else
    GoTo line4
End If

line3:
TheName = "" & Borrower & " " & LNumber & ""
TheFile = "P:\mortgage\prodcenters\LOAN ITEMS (SW)\_RateLocks_and_Changes\" & TheName & ".rtf"

DoCmd.OutputTo acOutputReport, "Confirmation_Email2", acFormatRTF, TheFile, False

If Nz(Me!InvestorID_Cbo, "Blank") = "Blank" Then
  DoCmd.SendObject , , , "CommerceMortgage@CommerceBank.com", , , "New Lock: " & Borrower & ": " & LNumber, "A rate lock confirmation has been saved down to the server at P:\mortgage\prodcenters\LOAN ITEMS (SW)\_RateLocks_and_Changes as a word document with the same name and loan number as that is the subject line of this email. Please upload it into the GDR.", -1
Else
    DoCmd.SendObject , , , "CommerceMortgage@CommerceBank.com", , , "Term Change" & ": " & Borrower & ": " & LNumber, "A rate lock confirmation has been saved down to the server at P:\mortgage\prodcenters\LOAN ITEMS (SW)\_RateLocks_and_Changes as a word document with the same name and loan number as that is the subject line of this email. Please upload it into the GDR.", True
End If

line4:
    ClsEmail = Nz([DBUsers_Tbl_1.EMail], "John.Vanesler@CommerceBank.com")
    ProcEmail = Nz([DBUsers_Tbl.EMail], "John.Vanesler@CommerceBank.com")
If Me!RateExpDate_Txt <= Date + 8 Then
    Caution = "STOP Terms Finalized:"
ElseIf ***MWStatus*** = "Closing" And Me!RateExpDate_Txt >= Date + 8 Then
    Caution = "STOP:"
Else
    Caution = ""
End If
If Me!InvestorID_Cbo = "" Then
    DoCmd.SendObject acSendReport, "Confirmation_Email", "SnapshotFormat(*.snp)", LOEmail, ProcEmail & ";" & ClsEmail, , Caution & "New Lock: " & Borrower & ": " & LNumber, , True
Else
    DoCmd.SendObject acSendReport, "Confirmation_Email", "SnapshotFormat(*.snp)", LOEmail, ProcEmail & ";" & ClsEmail, , Caution & "  " & "Term Change" & ": " & Borrower & ": " & LNumber, , True
End If

Exit_SendConfirm_Click:
    Exit Sub

Err_SendConfirm_Click:
    MsgBox Err.Description
    Resume Exit_SendConfirm_Click

End Sub

2 个答案:

答案 0 :(得分:2)

您的表 Status_Tbl 包含名为 MWStatus 的字段。并且您的代码构建了一个包含该字段的SELECT语句。

但是,代码对SELECT语句没有任何作用。所以评估这条线时......

ElseIf MWStatus = "Closing" And Me!RateExpDate_Txt >= Date + 8 Then

... Access将 MWStatus 解释为未声明的变量。

如果您希望 MWStatus 引用SELECT查询返回的字段,请使用OpenRecordset打开查询。然后你可以在记录集的当前行中引用 MWStatus 的值......

YourRecordsetObjectVariableName.Fields("MWStatus").Value ' .Value not strictly required here
YourRecordsetObjectVariableName!MWStatus ' same value, but more concise

答案 1 :(得分:0)

我假设你在尝试执行strSQL时遇到了这个错误(在这个Sub之外的某个地方)。看起来此错误正在显示,因为表Status_Tbl中不存在字段MWStatus。