在Access VBA中更新SQL - 从另一个表更新表值

时间:2016-01-20 21:16:52

标签: sql vba ms-access-2013

我是Access VBA和SQL编码的新手。到目前为止,我已经能够通过互联网找到我所遇到的大部分问题的答案。我目前正在尝试在MS Access(2013)VBA中编写一些代码,当特定表单关闭时,它会在同一数据库中的另一个表中更新一个表中的数据。

到目前为止,我已经解决了几个错误,但是我在SQLReplace的“UPDATE”中遇到了语法错误。可能还有其他一些我还不知道的错误,但我不确定。任何帮助/指导将不胜感激!

谢谢!

Private Sub Form_Close()

Dim SQLMove As String
Dim SQLReplace As String

Dim CountyCaseType As String
Dim CaseNumber As String
Dim County As String
Dim FirstName As String
Dim MiddleName As String
Dim LastName As String
Dim Facility As String
Dim VOL As String
Dim Diagnosis As String
Dim AppearanceWaived As String
Dim Dismissed As String
Dim HearingResults As String
Dim Disposition As String
Dim DOB As String
Dim Minor As String
Dim Sex As String
Dim ClerkName As String
Dim Judge As String
Dim CourtDate As String


CountyCaseType = "Tables!tblTemp.CountyCaseType.Value"
CaseNumber = "Tables!tblTemp.CaseNumber.Value"
County = "Tables!tblTemp.County.Value"
FirstName = "Tables!tblTemp.FirstName.Value"
MiddleName = "Tables!tblTemp.MiddleName.Value"
LastName = "Tables!tblTemp.LastName.Value"
Facility = "Tables!tblTemp.Facility.Value"
VOL = "Tables!tblTemp.VOL.Value"
Diagnosis = "Tables!tblTemp.Diagnosis.Value"
AppearanceWaived = "Tables!tblTemp.AppearanceWaived.Value"
Dismissed = "Tables!tblTemp.Dismissed.Value"
HearingResults = "Tables!tblTemp.HearingResults.Value"
Disposition = "Tables!tblTemp.Disposition.Value"
DOB = "Tables!tblTemp.DOB.Value"
Minor = "Tables!tblTemp.Minor.Value"
Sex = "Tables!tblTemp.Sex.Value"
ClerkName = "Tables!tblTemp.Clerk.Value"
Judge = "Tables!tblTemp.Judge.Value"
CourtDate = "Tables!tblTemp.CourtDate.Value"

SQLMove = "INSERT INTO tblCalendar SELECT * FROM tblTemp"
SQLReplace = "UPDATE tblCalendar " & _
                     "SET tblCalendar.CountyCaseType.Value = CountyCaseType, " & _
                     "    tblCalendar.CaseNumber.Value = CaseNumber, " & _
                     "    tblCalendar.County.Value = County, " & _
                     "    tblCalendar.FirstName.Value = FirstName, " & _
                     "    tblCalendar.MiddleName.Value = MiddleName, " & _
                     "    tblCalendar.LastName.Value = LastName, " & _
                     "    tblCalendar.Facility.Value = Facility, " & _
                     "    tblCalendar.VOL.Value = VOL, " & _
                     "    tblCalendar.Diagnosis.Value = Diagnosis, " & _
                     "    tblCalendar.AppearanceWaived.Value = AppearanceWaived, " & _
                     "    tblCalendar.Dismissed.Value = Dismissed, " & _
                     "    tblCalendar.HearingResults.Value = HearingResults, " & _
                     "    tblCalendar.Disposition.Value = Disposition, " & _
                     "    tblCalendar.DOB.Value = DOB, " & _
                     "    tblCalendar.Minor.Value = Minor, " & _
                     "    tblCalendar.Sex.Value = Sex, " & _
                     "    tblCalendar.ClerkName.Value = Clerk, " & _
                     "    tblCalendar.Judge.Value = Judge, " & _
                     "FROM tblTemp " & _
                     "Where 'CourtDate = tblCalendar.CourtDate.Value'"
DoCmd.SetWarnings False
DoCmd.RunSQL (SQLMove)
DoCmd.RunSQL (SQLReplace)
DoCmd.SetWarnings True
End Sub

2 个答案:

答案 0 :(得分:1)

您的代码中存在多个潜在错误:

  1. 您无需在属性末尾添加.Value即可获取其实际值。

  2. 当您直接在Access中工作时,您也不需要Tables!部分。这是处理记录集时使用的语法。例如,请写tblTemp.CountyCaseType而不是Tables!tblTemp.CountyCaseType.Value

  3. 变量的值不在SQL字符串中。您必须使用 [&] 将它们连接到SQLReplace String。例如,写

    SQLReplace = "UPDATE tblCalendar " & _
                     "SET tblCalendar.CountyCaseType = " & CountyCaseType & ", " & _
                     "    tblCalendar.CaseNumber = " & CaseNumber & ", " & _
                     ....
    
  4. 正如@AlanHadsell指出的那样,从WHERE子句中删除单引号。

    Where 'CourtDate = tblCalendar.CourtDate.Value'
    

    应该是

    WHERE CourtDate = tblCalendar.CourtDate
    

    但正如我在 3中所说的那样 CourTDate是一个String变量,所以它需要连接起来。您的最终WHERE条款应为:

    "WHERE " & CourtDate & " = tblCalendar.CourtDate"
    
  5. 您不需要SQLReplace字符串中的FROM tblTemp子句。

  6. 编辑:正如@Parfait指出的那样,在SQLReplace语句的范围内不存在tblTemp。你应该INNER JOIN来解决这个问题:

    UPDATE tblCalendar INNER JOIN tblTemp ON tblCalendar.CourtDate = tblTemp.CourtDate SET ...
    
  7. 修好所有内容后,您的最终代码应如下所示:

    Private Sub Form_Close()
    
        Dim SQLMove As String
        Dim SQLReplace As String
    
        Dim CountyCaseType As String
        Dim CaseNumber As String
        Dim County As String
        Dim FirstName As String
        Dim MiddleName As String
        Dim LastName As String
        Dim Facility As String
        Dim VOL As String
        Dim Diagnosis As String
        Dim AppearanceWaived As String
        Dim Dismissed As String
        Dim HearingResults As String
        Dim Disposition As String
        Dim DOB As String
        Dim Minor As String
        Dim Sex As String
        Dim ClerkName As String
        Dim Judge As String
        Dim CourtDate As String
    
    
        CountyCaseType = "tblTemp.CountyCaseType"
        CaseNumber = "tblTemp.CaseNumber"
        County = "tblTemp.County"
        FirstName = "tblTemp.FirstName"
        MiddleName = "tblTemp.MiddleName"
        LastName = "tblTemp.LastName"
        Facility = "tblTemp.Facility"
        VOL = "tblTemp.VOL"
        Diagnosis = "tblTemp.Diagnosis"
        AppearanceWaived = "tblTemp.AppearanceWaived"
        Dismissed = "tblTemp.Dismissed"
        HearingResults = "tblTemp.HearingResults"
        Disposition = "tblTemp.Disposition"
        DOB = "tblTemp.DOB"
        Minor = "tblTemp.Minor"
        Sex = "tblTemp.Sex"
        ClerkName = "tblTemp.Clerk"
        Judge = "tblTemp.Judge"
        CourtDate = "tblTemp.CourtDate"
    
        SQLMove = "INSERT INTO tblCalendar SELECT * FROM tblTemp"
        SQLReplace = "UPDATE tblCalendar " & _
                     "INNER JOIN tblTemp ON tblCalendar.CourtDate = tblTemp.CourtDate " & _
                             "SET tblCalendar.CountyCaseType = " & CountyCaseType & ", " & _
                             "    tblCalendar.CaseNumber = " & CaseNumber & ", " & _
                             "    tblCalendar.County = " & County & ", " & _
                             "    tblCalendar.FirstName = " & FirstName & ", " & _
                             "    tblCalendar.MiddleName = " & MiddleName & ", " & _
                             "    tblCalendar.LastName = " & LastName & ", " & _
                             "    tblCalendar.Facility = " & Facility & ", " & _
                             "    tblCalendar.VOL = " & VOL & ", " & _
                             "    tblCalendar.Diagnosis = " & Diagnosis & ", " & _
                             "    tblCalendar.AppearanceWaived = " & AppearanceWaived & ", " & _
                             "    tblCalendar.Dismissed = " & Dismissed & ", " & _
                             "    tblCalendar.HearingResults = " & HearingResults & ", " & _
                             "    tblCalendar.Disposition = " & Disposition & ", " & _
                             "    tblCalendar.DOB = " & DOB & ", " & _
                             "    tblCalendar.Minor = " & Minor & ", " & _
                             "    tblCalendar.Sex = " & Sex & ", " & _
                             "    tblCalendar.ClerkName = " & Clerk & ", " & _
                             "    tblCalendar.Judge = " & Judge 
        DoCmd.SetWarnings False
        DoCmd.RunSQL (SQLMove)
        DoCmd.RunSQL (SQLReplace)
        DoCmd.SetWarnings True
    End Sub
    

    要完成,而不是为String中要复制的每个属性声明一个tableTemp变量,然后为它们分配一些值,您可以简单地省略声明并直接放置属性在SQL中。这将大大减少代码的长度,如下所示:

    Private Sub Form_Close()
    
        Dim SQLMove As String
        Dim SQLReplace As String
    
        SQLMove = "INSERT INTO tblCalendar SELECT * FROM tblTemp"
        SQLReplace = "UPDATE tblCalendar " & _
                     "INNER JOIN tblTemp ON tblCalendar.CourtDate = tblTemp.CourtDate " & _
                             "SET tblCalendar.CountyCaseType = tblTemp.CountyCaseType, " & _
                             "    tblCalendar.CaseNumber = tblTemp.CaseNumber, " & _
                             "    tblCalendar.County = tblTemp.County, " & _
                             "    tblCalendar.FirstName = tblTemp.FirstName, " & _
                             "    tblCalendar.MiddleName = tblTemp.MiddleName, " & _
                             "    tblCalendar.LastName = tblTemp.LastName, " & _
                             "    tblCalendar.Facility = tblTemp.Facility, " & _
                             "    tblCalendar.VOL = tblTemp.VOL, " & _
                             "    tblCalendar.Diagnosis = tblTemp.Diagnosis, " & _
                             "    tblCalendar.AppearanceWaived = tblTemp.AppearanceWaived, " & _
                             "    tblCalendar.Dismissed = tblTemp.Dismissed, " & _
                             "    tblCalendar.HearingResults = tblTemp.HearingResults, " & _
                             "    tblCalendar.Disposition = tblTemp.Disposition, " & _
                             "    tblCalendar.DOB = tblTemp.DOB, " & _
                             "    tblCalendar.Minor = tblTemp.Minor, " & _
                             "    tblCalendar.Sex = tblTemp.Sex, " & _
                             "    tblCalendar.ClerkName = tblTemp.ClerkName, " & _
                             "    tblCalendar.Judge = tblTemp.Judge"
        DoCmd.SetWarnings False
        DoCmd.RunSQL (SQLMove)
        DoCmd.RunSQL (SQLReplace)
        DoCmd.SetWarnings True
    End Sub
    

答案 1 :(得分:0)

"Where 'CourtDate = tblCalendar.CourtDate.Value'"

中删除单引号