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

时间:2022-12-05 01:57:59

I'm fairly new to Access VBA and SQL coding. So far, I've been able to find most of the answers to issues I've had by using the internet. I'm currently trying to write some code in MS Access (2013) VBA that updates the data in one table from another table in the same database when a particular form closes.

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

I've worked out several errors so far, but I'm stuck on a syntax error in the "UPDATE" for SQLReplace. There could be other errors that I don't know about yet, but I'm not sure. Any help/Guidance would be greatly appreciated!

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

Thanks!

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 个解决方案

#1


1  

There are several potential errors in your code:

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

  1. You do not need to add .Value to the end of an attribute to get its actual value.

    您不需要将.Value添加到属性的末尾以获取其实际值。

  2. As you are working directly in Access, you to not need the Tables! part either. That is the syntax used when dealing with recordsets. For example, write tblTemp.CountyCaseType instead of Tables!tblTemp.CountyCaseType.Value

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

  3. The values of your variables are not in the SQL string. You have to concatenate them to the SQLReplace String using [&]. For example, write

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

    SQLReplace = "UPDATE tblCalendar " & _
                     "SET tblCalendar.CountyCaseType = " & CountyCaseType & ", " & _
                     "    tblCalendar.CaseNumber = " & CaseNumber & ", " & _
                     ....
    
  4. As @AlanHadsell pointed out, remove the single quotes from the WHERE clause.

    正如@AlanHadsell指出的那样,从WHERE子句中删除单引号。

    Where 'CourtDate = tblCalendar.CourtDate.Value'
    

    should be

    WHERE CourtDate = tblCalendar.CourtDate
    

    But as I said in 3) CourTDate is a String variable, so it needs to be concatenated. Your final WHERE clause should be:

    但正如我在3)中所说,CourTDate是一个String变量,所以它需要连接起来。你的最终WHERE子句应该是:

    "WHERE " & CourtDate & " = tblCalendar.CourtDate"
    
  5. You don't need the FROM tblTemp clause in the SQLReplace String.

    您不需要SQLReplace String中的FROM tblTemp子句。

  6. EDIT: As @Parfait pointed out, tblTemp does not exist in scope of the SQLReplace statement. You should do an INNER JOIN to fix that:

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

    UPDATE tblCalendar INNER JOIN tblTemp ON tblCalendar.CourtDate = tblTemp.CourtDate SET ...
    

After fixing everything, your final code should look like:

修复完所有内容后,您的最终代码应如下所示:

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

To finish, instead of declaring a String variable for each attributes in tableTemp that you want to copy, and then assigning some values to them, you can simply omit the declarations and put the attributes dicrectly in the SQL. That will geatly reduce the length of your code as follow:

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

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

#2


0  

Remove the single quotes from "Where 'CourtDate = tblCalendar.CourtDate.Value'"

从“Where'ChankDate = tblCalendar.CourtDate.Value'中删除单引号”

#1


1  

There are several potential errors in your code:

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

  1. You do not need to add .Value to the end of an attribute to get its actual value.

    您不需要将.Value添加到属性的末尾以获取其实际值。

  2. As you are working directly in Access, you to not need the Tables! part either. That is the syntax used when dealing with recordsets. For example, write tblTemp.CountyCaseType instead of Tables!tblTemp.CountyCaseType.Value

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

  3. The values of your variables are not in the SQL string. You have to concatenate them to the SQLReplace String using [&]. For example, write

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

    SQLReplace = "UPDATE tblCalendar " & _
                     "SET tblCalendar.CountyCaseType = " & CountyCaseType & ", " & _
                     "    tblCalendar.CaseNumber = " & CaseNumber & ", " & _
                     ....
    
  4. As @AlanHadsell pointed out, remove the single quotes from the WHERE clause.

    正如@AlanHadsell指出的那样,从WHERE子句中删除单引号。

    Where 'CourtDate = tblCalendar.CourtDate.Value'
    

    should be

    WHERE CourtDate = tblCalendar.CourtDate
    

    But as I said in 3) CourTDate is a String variable, so it needs to be concatenated. Your final WHERE clause should be:

    但正如我在3)中所说,CourTDate是一个String变量,所以它需要连接起来。你的最终WHERE子句应该是:

    "WHERE " & CourtDate & " = tblCalendar.CourtDate"
    
  5. You don't need the FROM tblTemp clause in the SQLReplace String.

    您不需要SQLReplace String中的FROM tblTemp子句。

  6. EDIT: As @Parfait pointed out, tblTemp does not exist in scope of the SQLReplace statement. You should do an INNER JOIN to fix that:

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

    UPDATE tblCalendar INNER JOIN tblTemp ON tblCalendar.CourtDate = tblTemp.CourtDate SET ...
    

After fixing everything, your final code should look like:

修复完所有内容后,您的最终代码应如下所示:

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

To finish, instead of declaring a String variable for each attributes in tableTemp that you want to copy, and then assigning some values to them, you can simply omit the declarations and put the attributes dicrectly in the SQL. That will geatly reduce the length of your code as follow:

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

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

#2


0  

Remove the single quotes from "Where 'CourtDate = tblCalendar.CourtDate.Value'"

从“Where'ChankDate = tblCalendar.CourtDate.Value'中删除单引号”