更新MySQL和Access的问题

问题描述 投票:0回答:1

所以这是一天前开始发生的问题。

我有一个Access数据库文件,它存储一个表单,用于创建作业,更新作业扇区,并从MySQL表中删除它。

有两个表用于此表单:一个存储在Access中的本地表称为“作业路由”,另一个表示通过MYSQL ODBC驱动程序,ANSI 5.3版本称为“待办事项”。本地表存储用户提交的数据,其中包含有关所有作业区域和状态的信息,而MYSQL表一次只显示一个作业区域。

创建新条目时,Access表单中的文本框详细信息将存储在两个表中。每个作业最多包含4个不同的扇区(例如[开始日期],[区域1],[负责人1],[说明1],...... [区域4],[负责人4],[说明4])。每当数据被更新到其下一个状态时,在本地表中只有作业计数器字段递增,而名为“待办事项”的MYSQL表中的每个字段都更新为其下一个状态字段。

与服务器的连接是好的,一切都运行正常,直到更新功能中弹出一个问题。

基本上该功能的工作原理是在列表框控件上,从“待办事项”表中查询所有当前作业数据。用户选择一个条目,并点击一个按钮,该按钮将来自“作业路径”的下一个扇区信息数据加载到各种文本框控件上。用户可以根据需要更改这些文本框输入 - 当函数运行时唯一更改的是“待办事项”。 “工作路线”中的信息保持不变。当用户点击更新按钮时,下一个扇区字段数据被更新为“待办事项”,而只有“作业路径”中的计数器正在递增以表示当前扇区。

我的问题是这个。在大多数情况下,几乎所有内容都运行良好,但是对于“待办事项”表中的一个字段,不会使用文本框中的值进行更新。因此,例如,如果文本框控件设置为“Wyntile”,则应将字段名称设置为该值,但由于某种原因,显示的是不同的值,例如=“Apples”。这是代码:

Private Sub moveJob2_Click()

'get the job number
JobNum = Text31

CurrArea = DLookup("[Area]", "[To_Do]", "[Job_Number] =""" & JobNum & """")
area1 = DLookup("[Area1]", "[Job Route]", "[Job Number] =""" & JobNum & """")
area2 = DLookup("[Area2]", "[Job Route]", "[Job Number] =""" & JobNum & """")
area3 = DLookup("[Area3]", "[Job Route]", "[Job Number] =""" & JobNum & """")
area4 = DLookup("[Area4]", "[Job Route]", "[Job Number] =""" & JobNum & """")

'get what the current area is
Current = DLookup("[Current]", "[Job Route]", "[Job Number] =""" & JobNum & """")

'if the current area is the first area then check to make sure there is a second
'if so, then set the new area to it
If Current = 1 Then
    If area2 = "---" Then
        MsgBox area1 + " was the last area in the route. The job cannot be moved."
        Exit Sub
    End If
    newArea = area2
ElseIf Current = 2 Then
    If area3 = "---" Then
        MsgBox area2 + " was the last area in the route. The job cannot be moved."
        Exit Sub
    End If
    newArea = area3
ElseIf Current = 3 Then
    If area4 = "---" Then
        MsgBox area3 + " was the last area in the route. The job cannot be moved."
        Exit Sub
    End If
    newArea = area4
Else
    MsgBox area4 + " was the last area in the route. The job cannot be moved."
    Exit Sub
End If

'set up link to both the To_Do and Job Route tables
Dim dbJobNumbers As DAO.Database
Dim rstJob As DAO.Recordset
Dim jobRoute As DAO.Recordset

Set dbJobNumbers = CurrentDb
Set rstJob = dbJobNumbers.OpenRecordset("To_Do")
Set jobRoute = dbJobNumbers.OpenRecordset("Job Route")

' >> Edit the job in the To_Do table
****' ERROR: Out of all these, only [Person_In_Charge] is being set to something
****' completely different from Text33, which wasn't changed by the user.

rstJob.FindFirst "[Job_Number]=""" + Text31 + """"
rstJob.Edit
rstJob("[Area]").Value = newArea
rstJob("[Person_In_Charge]").Value = Text33  
rstJob("[Equipment]").Value = Text37
rstJob("[Description]").Value = Text35
rstJob.Update

'update the current area for the Job Route
jobRoute.FindFirst "[Job Number]=""" + Text31 + """"
jobRoute.Edit
jobRoute("[Current]").Value = CInt(Current) + 1
jobRoute.Update

'success message
MsgBox Text31 + " has been moved from " + CurrArea + " to " + newArea + "."

'requery the listboxes
Dim selectParas As String
selectParas = "SELECT [a].[Job_Number] as [Job Number], [a].[Description], [a].[Person_In_Charge] as [Person in Charge], [a].[Area] " & _
              " FROM [To_Do] As [a];"

listRemoveJobs.RowSource = selectParas
listRemoveJobs.Requery
listChangeJobArea.RowSource = selectParas
listChangeJobArea.Requery

End Sub

该功能一直运行良好,即使现在我再次测试它按程序运行。虽然今天我收到了“To Do'上的ODBC插入失败”错误,但这是针对不同的功能。所以我认为ODBC连接/ MySQL表中出现了问题,但是当我在phpmyadmin中检查表时,该表大部分都遵循了Access中使用的其他mysql表的类似格式。

另外需要注意的是,告诉我这个问题的人在一个旧的Windows XP版本上运行,在那台计算机之前曾经有一些已知的定义OBDC ANSI 5.3驱动程序实例的问题在之前的Access'数据源列表中完全消失。 (驱动程序仍安装在Windows上)。那个时候,当计算机重新启动时,显然驱动程序实例后来在D.S.列表中再次神奇地重新出现。 ...我知道这是相当长的,但我似乎无法找到为什么在Access中发生这种更新错误的原因。是否存在已知的ODBC问题,其中存在稳定性问题?为什么在更新时价值变为完全不同的东西?任何见解将不胜感激。

mysql ms-access access-vba odbc
1个回答
0
投票

虽然没有可重现的示例来帮助您了解具体情况,但请考虑使用绑定参数运行纯SQL UPDATE查询。您的区域条件逻辑可以重写为嵌套的IIF表达式。可能,这将简化您的问题并简化您的需求,无需DLookup或多个记录集更新。此外,您没有必要重新分配到RowSource。下面使用参数化,这是在应用程序层运行SQL时的最佳实践:

SQL(以下同时保存为Access查询)

  1. mySavedJoinUpdateQuery PARAMETERS Text33Param Text(255), Text35Param Text(255) Text37Param Text(255), JobNumberParam Text(255); UPDATE [To_Do] d INNER JOIN [Job Route] r ON d.Job_Number = r.Job_Number SET [Area] = IIF([Current] = 1 AND [Area2] != '---', [Area2], IIF([Current] = 2 AND [Area3] != '---', [Area3], IIF([Current] = 3 AND [Area4] != '---', [Area4], [Area1) ) ), [Person_In_Charge] = Text33Param, [Equipment] = Text37Param, [Description] = Text35Param WHERE r.[Job Number] = JobNumberParam;
  2. mySavedSimpleUpdateQuery PARAMETERS JobNumberParam Text(255); UPDATE [Job Route] r SET r.[Current] = r.[Current] + 1 WHERE r.[Job Number] = JobNumberParam;

VBA

Private Sub moveJob2_Click()
   Dim qdef As QueryDef
   Dim selectParas As String

   ' UPDATE JOIN QUERY
   Set qdef = CurrentDb.QueryDefs("mySavedJoinUpdateQuery")

   qdef!JobNumberParam = Text31
   qdef!Text33Param = Text33
   qdef!Text35Param = Text35
   qdef!Text37Param = Text37

   qdef.Execute dbFailOnError 
   Set qdef = Nothing

   ' UPDATE SIMPLE QUERY
   Set qdef = CurrentDb.QueryDefs("mySavedSimpleUpdateQuery")

   qdef!JobNumberParam = Text31   
   qdef.Execute dbFailOnError 
   Set qdef = Nothing

   ' REQUERY LIST BOXES
   listRemoveJobs.Requery
   listChangeJobArea.Requery
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.