Access 2016 VBA无法设置按钮单击表单以将多值字段写入第二个表

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

我正在构建一个应用程序来跟踪项目,员工等的时间。我工作的公司有重要的安全繁文缛节,所以我必须使用MS Access 2016,我被禁止从youtube搜索解决方案,我无法下载任何文件来自网络。幸运的是,我仍然可以访问StackOverflow。

我有一个带有按钮的输入表单(“Task_List_Entry_Form”)。单击时,该按钮将值发送到表(“tbl_Task_List”)。在VBA的形式中,我已经包括表(“tbl_Task_List”)刷新,并且所有值都转到另一个充当更正日志的表(“tbl_Task_List_Corrections”)。但是多值字段(我知道,它们是最糟糕的,如果它不是看似必要的东西,我不会包括它们)不能去。所以我在VBA中包含了第二个查询来获取多值字段的值并输入到字段中,但到目前为止这一点都没有成功。

第一个查询选项如下所示: strSQL2 = "INSERT INTO tbl_Task_List_Corrections " & Tags.Value & " FROM tbl_Task_List " & "WHERE tbl_Task_List.ID IN (SELECT MAX(tbl_Task_List.ID) FROM tbl_Task_List);" 'DoCmd.RunSQL strSQL2 结果:抛出错误“运行时错误'3134':INSERT INTO语句中的语法错误”,错误在线“DoCmd.RunSQL strSQL2”,非多值字段正确传输,但未拉出值, tbl_Task_List_Corrections.Tags字段保持为NULL。

然后我尝试了更像这样的东西: CurrentDb.Execute "INSERT INTO tbl_Task_List_Corrections (Tags) VALUES ('" & Me.[Tags].Value & "');" 结果:未引发任何错误,但未提取该值,并且在“tbl_Task_List_Corrections”表中插入了一个额外的行

可能有一个我可以尝试的第三个解决方案,我在这个源上找到了,但它是基于一个示例访问数据库,由于安全限制,我无法下载和查看,我无法从中找出它仅代码。这是来源:https://www.utteraccess.com/forum/index.php?s=66e048c0880b46f8bd25a0541c30df63&showtopic=2018212&st=0&p=2463687&#entry2463687

这不是所有代码,而是保存表单后与移动值有关的代码。

Me.Requery                'Refresh so the table is complete

Dim strSQL1 As String     'Declaring the Non-Multivalued fields
Dim strSQL2 As String     'attempting the multi-value fields again

strSQL1 = "INSERT INTO tbl_Task_List_Corrections (Task_ID, Task_Title,  
Task_Start_Time, Task_End_Time, Break_Length_Minutes, Priority,  
Blockage_Reason, Requirements, Notes, Task_Complete) SELECT 
tbl_Task_List.ID, tbl_Task_List.Task_Title, tbl_Task_List.Task_Start_Time, 
tbl_Task_List.Task_End_Time, tbl_Task_List.Break_Length_Minutes, 
tbl_Task_List.Priority, tbl_Task_List.Blockage_Reason, 
tbl_Task_List.Requirements, tbl_Task_List.Notes, 
tbl_Task_List.Task_Complete FROM tbl_Task_List WHERE tbl_Task_List.ID IN 
(SELECT MAX(tbl_Task_List.ID) FROM tbl_Task_List);"
DoCmd.RunSQL strSQL1

'strSQL2 = "INSERT INTO tbl_Task_List_Corrections " & Tags.Value & " FROM 
tbl_Task_List " & "WHERE tbl_Task_List.ID IN (SELECT MAX(tbl_Task_List.ID) 
FROM tbl_Task_List);"
'DoCmd.RunSQL strSQL2          'THIS IS ATTEMPT 1

'CurrentDb.Execute "INSERT INTO tbl_Task_List_Corrections (Tags) VALUES ('" 
& Me.[Tags].Value & "');"      'THIS IS ATTEMPT 2


    Dim db As Database                               'Attempt4
    Dim rs As Recordset                              'Attempt4
    Dim childRS As Recordset                         'Attempt4
                                                'Attempt4
    Set db = CurrentDb()                             'Attempt4
                                                'Attempt4
    ' Open a Recordset for the Tasks table.                    'Attempt4
    Set rs = db.OpenRecordset("tbl_Task_List")                 'Attempt4
    rs.MoveFirst                                               'Attempt4
                                                               'Attempt4
    Do Until rs.EOF                                            'Attempt4
       ' Print the name of the task to the Immediate window.   'Attempt4
       Debug.Print rs!Task_Title.Value                         'Attempt4
                                                               'Attempt4
       ' Open a Recordset for the multivalued field.           'Attempt4
       Set childRS = rs!Tags.Value                             'Attempt4
                                                               'Attempt4
          ' Exit the loop if the multivalued field contains no records 'Attempt4
         Do Until childRS.EOF                                      'Attempt4
              childRS.MoveFirst                                     'Attempt4
                                                               'Attempt4
             ' Loop through the records in the child recordset.    'Attempt4
              Do Until childRS.EOF                                  'Attempt4
                  ' Print the owner(s) of the task to the Immediate 'Attempt4
                  ' window.                                         'Attempt4
                  Debug.Print Chr(0), childRS!Value.Value           'Attempt4
                  childRS.MoveNext                                  'Attempt4
              Loop                                                  'Attempt4
          Loop                                                      'Attempt4
      rs.MoveNext                                                  'Attempt4
    Loop                                                            'Attempt4

MsgBox "You have successfully added this Task"

DoCmd.Close

End Sub
 Below is attempt 5

 Dim db As Database                               'Attempt5
    Dim rs As Recordset                              'Attempt5
    Dim rs2 As Recordset                             'Defining the tbl_Task_List_Corrections
    Dim childRS As Recordset                         'Attempt5
                                                'Attempt5
    Set db = CurrentDb()                             'Attempt5
                                                'Attempt5
    ' Open a Recordset for the Tasks table.                              'Attempt5
    Set rs = db.OpenRecordset("tbl_Task_List")                           'Attempt5
    Set rs2 = db.OpenRecordset("tbl_Task_List_Corrections")              'Setting the value of tbl_Task_List_Corrections
    rs.MoveLast                                                         'Attempt5
                                                                    'Attempt5

'直到rs.EOF'尝试5

  ' Print the name of the task to the Immediate window.             'Attempt5
  Debug.Print rs!ID.Value                                           'Attempt5
  Debug.Print rs!Task_Title.Value                                   'Attempt5
  Debug.Print rs!Priority.Value                                     'Attempt5
  Debug.Print rs!Blockage_Reason.Value                              'Attempt5
  Debug.Print rs!Requirements.Value                                 'Attempt5
  Debug.Print rs!Notes.Value                                        'Attempt5

  ' Open a Recordset for the multivalued field.                     'Attempt5
  Set childRS1 = rs!Tags.Value                                      'Attempt5
  Set childRS2 = rs!Assigned_To.Value
                                                                    'Attempt5
     ' Exit the loop if the multivalued field contains no records.  'Attempt5
     Do Until childRS1.EOF                                          'Attempt5
         childRS1.MoveFirst                                         'Attempt5
                                                                    'Attempt5
         ' Loop through the records in the child recordset.         'Attempt5
         Do Until childRS1.EOF                                      'Attempt5
             ' Print the owner(s) of the task to the Immediate      'Attempt5
             ' window.                                              'Attempt5
             Debug.Print Chr(0), childRS1!Value.Value               'Attempt5

             childRS1.MoveNext                                       'Attempt5
         Loop                                                        'Attempt5
    Loop                                                            'End of loop that checks if Tags multi-value field is NULL

    ' Exit the loop if the multivalued field contains no records.  'Attempt5
     Do Until childRS2.EOF                                           'Attempt5
         childRS2.MoveFirst                                          'Attempt5

         ' Loop through the records in the child recordset.         'Attempt5
         Do Until childRS2.EOF                                       'Attempt5
             ' Print the owner(s) of the task to the Immediate      'Attempt5
             ' window.                                              'Attempt5
             Debug.Print Chr(0), childRS2!Value.Value                'Attempt5

             childRS2.MoveNext                                       'Attempt5
         Loop                                                       'Attempt5

     Loop                                                           'End of loop that checks if Assigned_To multi-value field is NULL

    rs2.AddNew                                                          'Attempt5
    rs2!Task_ID = rs!ID.Value                                           'Attempt5
    rs2!Task_Title = rs!Task_Title                                      'Attempt5
    rs2!Tags = childRS1!Value.Value                                     'Attempt5
    rs2!Priority = rs!Priority                                          'Attempt5
    rs2!Assigned_To = childRS2!Value.Value                              'Attempt5
    rs2!Blockage_Reason = rs!Blockage_Reason                            'Attempt5
    rs2!Requirements = rs!Requirements                                  'Attempt5
    rs2!Notes = rs!Notes                                                'Attempt5
    rs2.Update                                                          'Attempt5

下面是尝试7,参考4/13的答案(再次编辑4/23)

Me.Requery                                                          'Refresh the table before running the query

Dim strSQL1 As String                                               'Declaring the Non-Multivalued fields to move to Corrections Log

strSQL1 = "INSERT INTO tbl_Task_List_Corrections (Task_ID, Task_Title, " & _
        "Task_Start_Time, Task_End_Time, Break_Length_Minutes, Priority, " & _
        "Blockage_Reason, Requirements, Notes, Task_Complete) " & _
        "SELECT ID, Task_Title, Task_Start_Time, Task_End_Time, " & _
        "Break_Length_Minutes, Priority, Blockage_Reason, Requirements, " & _
        "Notes, Task_Complete FROM tbl_Task_List WHERE ID IN " & _
        "(SELECT MAX(ID) FROM tbl_Task_List);"
DoCmd.RunSQL strSQL1

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT ID FROM tbl_Task_List") 'error 3061
Set rs = CurrentDb.OpenRecordset("SELECT MAX(ID) FROM tbl_Task_List") '3265
    Do Until rs.EOF
    CurrentDb.Execute "INSERT INTO " & _ 
    " tbl_Task_List_Corrections(Tags.Value) SELECT Tags.Value FROM " & _  
    " (SELECT Tags.Value FROM tbl_Task_List WHERE ID = " & rs!ID & ") " & _ 
    " AS T1 WHERE tbl_Task_List_Corrections.Task_ID = " & rs!ID
    rs.MoveNext
Loop

MsgBox "You have successfully added this Task"

'DoCmd.SetWarnings False                                             'Turning the "You are about to Update 1 Row" warning back on

DoCmd.Close

End Sub
vba ms-access recordset multivalue
1个回答
0
投票

结果比我想象的要简单得多。看起来原始尝试是正确的。

一旦创建了具有非MVF数据的新记录,运行另一个INSERT SELECT操作来填充MVF。示例显示在复制记录的循环中填充第二个表MVF。经过测试和工作。应该很容易为表单上的记录标识符指定的单个记录执行SQL操作,而不是循环记录集。

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT RateID FROM CopyOfRates")
Do Until rs.EOF
    CurrentDb.Execute "INSERT INTO CopyOfRates(MVTest.Value) SELECT MVTest.Value FROM " & _
        "(SELECT MVTest.Value FROM Rates WHERE RateID = " & rs!RateID & ") AS Q1 " & _
        "WHERE CopyOfRates.RateID = " & rs!RateID
    rs.MoveNext
Loop
© www.soinside.com 2019 - 2024. All rights reserved.