【问题标题】:SQL Select statement in a Class module need table field in If StatementClass模块中的SQL Select语句需要If语句中的表字段
【发布时间】:2014-01-31 21:55:55
【问题描述】:

我正在使用带有 VBA SQL 语句的 MS Access 2007 构建一个类模块,以发送保存文件并发送两封不同的电子邮件。我不断收到未为 Status_Tbl 中的字段 MWStatus 定义的变量。我现在对所有代码都视而不见,需要帮助。我在表单后面的模块中有 SQL 语句,它可以工作,但是会减慢表单的速度,所以我正在移动 SQL 语句。我的问题是,为什么 Status_Tbl 中的字段 MWStatus 给我错误“变量未定义”。这是我第一次把SQL语句放到VBA Module中,不知道是不是做对了

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

【问题讨论】:

  • 你的问题到底是什么......?
  • @Dan 感谢您查看这篇文章。背景故事,我在表单后面的模块中有 SQL 语句,它可以工作,但是减慢了表单的速度,所以我正在移动 SQL 语句。我的问题是,为什么 Status_Tbl 中的字段 MWStatus 给我错误“变量未定义”。这是我第一次把SQL语句放到VBA Module中,不知道做对了没有。
  • @Dan 我不认为我把你的名字写对了,所以我又做了一次。很抱歉,如果您有两次联系。
  • 别担心,@user3258917。

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


【解决方案1】:

您的表 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

【讨论】:

  • 我感觉不好问,但我就是不明白。我缺少的部分是“YourRecordsetObjectVariableName” Dim dbs As DAO.Database Dim rst As DAO.Recordset Dim strSQL As String Dim strCMCID As String Dim strMWS As String strCMCID = Me!CMCID_Txt.Value strSQL = "SELECT Commitments_Tbl.CMCID, Status_Tbl. MWStatus, DBUsers_Tbl.EMail, DBUsers_Tbl_1.EMail " & _ "FROM ..."WHERE Commitments_Tbl.CMCID)= ' " & strCMCID & " ' ; " 设置 rst = dbs.OpenRecordset(strSQL) strMWS = rst!MWStatus
  • Dim dbs As DAO.Database Dim rst As DAO.Recordset Dim strSQL As String Dim strCMCID As String Dim strMWS As String strCMCID = Me!CMCID_Txt.Value strSQL = "SELECT Commitments_Tbl.CMCID, Status_Tbl.MWStatus , DBUsers_Tbl.EMail, DBUsers_Tbl_1.EMail " & _ "FROM ..."WHERE Commitments_Tbl.CMCID)= ' " & strCMCID & " ' ; " 设置 rst = dbs.OpenRecordset(strSQL) strMWS = rst!MWStatus
  • 现在我得到对象变量未设置。我是对的,我也试图从 SQL DBUsers_Tbl.EMail 和 DBUsers_Tbl_!.EMail 中提取我是否需要为它们做同样的事情?这是我第一次使用这个网站,很抱歉发布的混乱。
  • 这对我有帮助吗?一旦我得到这个,我会很高兴。
  • 您是否编写过打开 DAO 记录集并从该记录集中读取值的 VBA 代码?我之所以这么问,是因为我觉得你对那个话题迷失了方向。
【解决方案2】:

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

【讨论】:

  • 我检查了表格和字段名称,它就在那里。我应该打破 SQL 语句吗?
猜你喜欢
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 2013-10-10
  • 2014-10-18
  • 1970-01-01
相关资源
最近更新 更多