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

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

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

问题

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

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

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

'插入新记录到log_time_tbl
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消息框都会弹出正确的信息。因此,我认为插入函数写错了,但我不知道如何修复它。

Private Sub Start_btn_Click()
    ' 省略部分代码...
    
    ' BREAKS HERE 循环遍历Tech_form上GO_list中的选定项目
    For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
    
        ' 检查技术人员名称、阶段和GO是否被提取
        nk = MsgBox("Selected Tech Name: " & Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
        nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
        
        ' 将GO的值设置为变量名
        selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
        
        ' 插入新记录到log_time_tbl
        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 & "#);"
    
    ' 循环遍历Tech_form上GO_list中的选定项目
    Next selectedGO
    
    ' 关闭记录集和数据库
    rs.Close
    db.Close
    
    Set rs = Nothing
    Set db = Nothing
    
    ' 刷新表单以更新log_time_tbl记录
    Forms("Tech_frm").Requery
End Sub
  • 尝试 1:
'不起作用

'For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
'nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
'DoCmd.RunSQL ("INSERT INTO log_time_tbl (GO_info) values ([Forms(Tech_frm]).GO_list.ItemData(selectedGO)")
  • 尝试 2:
'在此中断 循环遍历Tech_form上GO_list中的选定项目
For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected  

'检查技术人员名称、阶段和GO是否被提取
nk = MsgBox("Selected Tech Name: " & 
Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")            

'将GO的值设置为变量名
selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
               
'插入新记录到log_time_tbl
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:

'Insert a new record in log_time_tbl
 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.

Private Sub Start_btn_Click()
    Dim nk As Integer
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim selectedGO As Variant
    Dim techName As String
    Dim workPosition As String
    Dim startTime As Date
    Dim b As Byte
    

    ' Check if phase_combo is null
    If IsNull(Me.phase_combo.Value) Then
        nk = MsgBox("Please select a Phase and Try Again", vbCritical, "Phase Not Selected")
    Else
        If Me.GO_list.ListIndex = -1 Then
            nk = MsgBox("Please select a G.O.# and Try Again", vbCritical, "G.O.# Not Selected")
        Else
            nk = MsgBox("GO Numbers were Selected", vbInformation, "No missing data")
            Set db = CurrentDb()
            Set rs = db.OpenRecordset("log_time_tbl")
            
            ' Get technician name from tech_combo on Login_frm
            techName = Forms("Tech_frm").signin_txt.Value
            
            ' Get work position from work_position combo box on Tech_form
            workPosition = Forms("Tech_frm").phase_combo.Value
            
            ' Get current time
            startTime = Now()
            
            'make sure multiselect is on for the go list
            'b = Forms("Tech_frm").Controls("GO_list").MultiSelect
            'nk = MsgBox("output: " & b, vbInformation, "MultiSelect output")
            'nk = MsgBox("Selected Tech Name: " & Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
            
            'DOESNT WORK
            'For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
            'nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
                'DoCmd.RunSQL ("INSERT INTO log_time_tbl (GO_info) values ([Forms(Tech_frm]).GO_list.ItemData(selectedGO)")

                      
            'BREAKES HERE Loop through selected items in GO_list on Tech_form
            For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected
            
            'Check to see if the tech name, phase, and GO is being pulled
            nk = MsgBox("Selected Tech Name: " & Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
            nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")
                
                'set the value of the GO to a varible name
               selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
               
                'Insert a new record in log_time_tbl
                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 & "#);"
            
            'Loop through selected items in GO_list on Tech_form
            Next selectedGO
            rs.Close
            db.Close
                   
            Set rs = Nothing
            Set db = Nothing
            
            ' Refresh the form to update the log_time_tbl records
            Forms("Tech_frm").Requery
            
        End If
    End If
End Sub
  • Attempt 1:

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

    'BREAKS HERE Loop through selected items in GO_list on Tech_form.
    For Each selectedGO In Forms("Tech_frm").GO_list.ItemsSelected  
    
    'Check to see if the tech name, phase, and GO is being pulled
    nk = MsgBox("Selected Tech Name: " & 
    Forms("Tech_frm").signin_txt.Value & "Selected Phase: " & Forms("Tech_frm").phase_combo.Value, vbInformation, "Tech/Phase")
    nk = MsgBox("Selected G.O.#: " & Forms("Tech_frm").GO_list.ItemData(selectedGO), vbInformation, "GO#")            
    
    'set the value of the GO to a varible name
     selectedGOValue = Forms("Tech_frm").GO_list.ItemData(selectedGO)
    
    'Insert a new record in log_time_tbl
    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:

([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:

确定