有方法修复这段代码以防止运行时错误吗?

huangapple go评论97阅读模式
英文:

Is there a way to fix this code to prevent runtime error?

问题

代码应该插入到一个名为log_time_tbl的表中。
log_time_tbl:
有方法修复这段代码以防止运行时错误吗?

我遇到了一个运行时错误'3061':参数太少。预期为1。

大部分代码都可以运行,只是在这段代码中一直遇到问题:

  1. '插入新记录到log_time_tbl
  2. db.Execute "INSERT INTO log_time_tbl " & "([GO info], [worker name], [phase], [start time], [end time], [Time Elapsed]) VALUES " & "(" & Forms("Tech_frm").GO_list.ItemData(selectedGO) & ", '" & Forms("Tech_frm").signin_txt.Value & "', '" & Forms("Tech_frm").phase_combo.Value & "', #" & startTime & "#, #" & startTime & "#, #" & startTime & "#);"

这是该部分的所有代码。所有的nk消息框都会弹出正确的信息。因此,我认为插入函数写错了,但我不知道如何修复它。

  1. Private Sub Start_btn_Click()
  2. ' 省略部分代码...
  3. ' BREAKS HERE 循环遍历Tech_formGO_list中的选定项目
  4. For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
  5. ' 检查技术人员名称、阶段和GO是否被提取
  6. nk = MsgBox("Selected Tech Name: " & Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
  7. nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
  8. ' GO的值设置为变量名
  9. selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
  10. ' 插入新记录到log_time_tbl
  11. db.Execute "INSERT INTO log_time_tbl " & "([GO info], [worker name], [phase], [start time], [end time], [Time Elapsed]) VALUES " & "(" & Forms("Tech_frm").GO_list.ItemData(selectedGO) & ", '" & Forms("Tech_frm").signin_txt.Value & "', '" & Forms("Tech_frm").phase_combo.Value & "', #" & startTime & "#, #" & startTime & "#, #" & startTime & "#);"
  12. ' 循环遍历Tech_formGO_list中的选定项目
  13. Next selectedGO
  14. ' 关闭记录集和数据库
  15. rs.Close
  16. db.Close
  17. Set rs = Nothing
  18. Set db = Nothing
  19. ' 刷新表单以更新log_time_tbl记录
  20. Forms("Tech_frm").Requery
  21. End Sub
  • 尝试 1:
  1. '不起作用
  2. 'For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
  3. 'nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
  4. 'DoCmd.RunSQL ("INSERT INTO log_time_tbl (GO_info) values ([Forms(Tech_frm]).GO_list.ItemData(selectedGO)")
  • 尝试 2:
  1. '在此中断 循环遍历Tech_form上GO_list中的选定项目
  2. For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
  3. '检查技术人员名称、阶段和GO是否被提取
  4. nk = MsgBox("Selected Tech Name: " &
  5. Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
  6. nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
  7. '将GO的值设置为变量名
  8. selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
  9. '插入新记录到log_time_tbl
  10. db.Execute "INSERT INTO log_time_tbl " & "([GO info], [worker name], [phase], [start time], [end time], [Time Elapsed]) VALUES " & "(" & Forms("Tech_frm").GO_list.ItemData(selectedGO) & ", '" & Forms("Tech_frm").signin_txt.Value & "', '" & Forms("Tech_frm").phase_combo.Value & "', #" & startTime & "#, #" & startTime & "#, #" & startTime & "#);"
英文:

The code is suppose to insert into a table called log_time_tbl.
log_time_tbl:
有方法修复这段代码以防止运行时错误吗?

I've run into a Run-time error '3061': Too few parameters. Expected 1.

Most of the code works I just keep running into a problem with this segment of code:

  1. 'Insert a new record in log_time_tbl
  2. db.Execute "INSERT INTO log_time_tbl " & "([GO info], [worker name], [phase], [start time], [end time], [Time Elapsed]) VALUES " & "(" & Forms("Tech_frm").GO_list.ItemData(selectedGO) & ", '" & Forms("Tech_frm").signin_txt.Value & "', '" & Forms("Tech_frm").phase_combo.Value & "', #" & startTime & "#, #" & startTime & "#, #" & startTime & "#);"

Here is all of the code for the section. ALL the nk message boxes pop up the correct information. So I believe that the insert function is written wrong, but I don't know how to fix it.

  1. Private Sub Start_btn_Click()
  2. Dim nk As Integer
  3. Dim db As DAO.Database
  4. Dim rs As DAO.Recordset
  5. Dim strSQL As String
  6. Dim selectedGO As Variant
  7. Dim techName As String
  8. Dim workPosition As String
  9. Dim startTime As Date
  10. Dim b As Byte
  11. ' Check if phase_combo is null
  12. If IsNull(Me.phase_combo.Value) Then
  13. nk = MsgBox("Please select a Phase and Try Again", vbCritical, "Phase Not Selected")
  14. Else
  15. If Me.GO_list.ListIndex = -1 Then
  16. nk = MsgBox("Please select a G.O.# and Try Again", vbCritical, "G.O.# Not Selected")
  17. Else
  18. nk = MsgBox("GO Numbers were Selected", vbInformation, "No missing data")
  19. Set db = CurrentDb()
  20. Set rs = db.OpenRecordset("log_time_tbl")
  21. ' Get technician name from tech_combo on Login_frm
  22. techName = Forms("Tech_frm").signin_txt.Value
  23. ' Get work position from work_position combo box on Tech_form
  24. workPosition = Forms("Tech_frm").phase_combo.Value
  25. ' Get current time
  26. startTime = Now()
  27. 'make sure multiselect is on for the go list
  28. 'b = Forms("Tech_frm").Controls("GO_list").MultiSelect
  29. 'nk = MsgBox("output: " & b, vbInformation, "MultiSelect output")
  30. 'nk = MsgBox("Selected Tech Name: " & Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
  31. 'DOESNT WORK
  32. 'For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
  33. 'nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
  34. 'DoCmd.RunSQL ("INSERT INTO log_time_tbl (GO_info) values ([Forms(Tech_frm]).GO_list.ItemData(selectedGO)")
  35. 'BREAKES HERE Loop through selected items in GO_list on Tech_form
  36. For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
  37. 'Check to see if the tech name, phase, and GO is being pulled
  38. nk = MsgBox("Selected Tech Name: " & Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
  39. nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
  40. 'set the value of the GO to a varible name
  41. selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
  42. 'Insert a new record in log_time_tbl
  43. db.Execute "INSERT INTO log_time_tbl " & "([GO info], [worker name], [phase], [start time], [end time], [Time Elapsed]) VALUES " & "(" & Forms("Tech_frm").GO_list.ItemData(selectedGO) & ", '" & Forms("Tech_frm").signin_txt.Value & "', '" & Forms("Tech_frm").phase_combo.Value & "', #" & startTime & "#, #" & startTime & "#, #" & startTime & "#);"
  44. 'Loop through selected items in GO_list on Tech_form
  45. Next selectedGO
  46. rs.Close
  47. db.Close
  48. Set rs = Nothing
  49. Set db = Nothing
  50. ' Refresh the form to update the log_time_tbl records
  51. Forms("Tech_frm").Requery
  52. End If
  53. End If
  54. End Sub
  • Attempt 1:

    1. 'DOESN'T WORK
    2. 'For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
    3. 'nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
    4. 'DoCmd.RunSQL ("INSERT INTO log_time_tbl (GO_info) values ([Forms(Tech_frm]).GO_list.ItemData(selectedGO)")
  • Attempt 2:

    1. 'BREAKS HERE Loop through selected items in GO_list on Tech_form.
    2. For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
    3. 'Check to see if the tech name, phase, and GO is being pulled
    4. nk = MsgBox("Selected Tech Name: " &
    5. Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
    6. nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
    7. 'set the value of the GO to a varible name
    8. selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
    9. 'Insert a new record in log_time_tbl
    10. db.Execute "INSERT INTO log_time_tbl " & "([GO info], [worker name], [phase], [start time], [end time], [Time Elapsed]) VALUES " & "(" & Forms("Tech_frm").GO_list.ItemData(selectedGO) & ", '" & Forms("Tech_frm").signin_txt.Value & "', '" & Forms("Tech_frm").phase_combo.Value & "', #" & startTime & "#, #" & startTime & "#, #" & startTime & "#);"

答案1

得分: 1

使用实际字段名(带下划线)在您的SQL中:

([GO_info],[worker_name],[phase],[start_time],[end_time],[Time_Elapsed])

英文:

Use the actual field names (having underscores) in your SQL:

  1. ([GO_info], [worker_name], [phase], [start_time], [end_time], [Time_Elapsed])

huangapple
  • 本文由 发表于 2023年7月13日 21:03:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定