访问中的VBA错误:" cammand或action' RecordsGoToNew'现在不可用"

时间:2014-08-12 23:03:54

标签: vba ms-access access-vba

我想提前道歉。我正在处理的表格非常复杂,有许多活动部件,所以我很抱歉,我的解释并不清楚。

我有一个包含许多子表单的表单。一个特定的子表单fAdminToolsDetails有一个自己的子表单:sfAdminToolsYearly。根据整个表单的状态,不同的查询和表将使用信息填充表单。我使用枚举设计了状态。根据传入子的枚举值,状态将发生变化,并更新相应的记录源,控制源等。但是,前面提到的表单sfAdminToolsYearly没有正确更新。在用户与其父级进行交互之前,其信息仍然反映了之前的状态:fAdminToolsDetails。

我个人认为这很好。但是,我的客户担心这可能会引起一些混乱。为了纠正这个问题,我试图告诉表单转到新记录,只是为了清除所有数据以及创建新选项或记录添加。但是,我收到除DEPARTMENT以外的所有州的标题中的错误。

我一整天都在看这个,但似乎无法找到错误的根本原因。请查看下面的代码,看看您是否可以提供帮助。错误最初会抛到最后一行。

如果对代码有任何疑问,请与我们联系。我很乐意回答他们。

谢谢

Private Function changeState(ByVal state As EnumState, ByVal CrrntId As Long)

Dim prevSub As String
Dim level As String
Dim lvlSub As String
Dim prevLvl As String
Dim fYrIndex As Integer
Dim crrntDeptIndex As Integer
Dim crrntDivIndex As Integer
Dim crrntSDivIndex As Integer
Dim crrntSSDivIndex As Integer
Dim crrntSSSDivIndex As Integer

Me![crrntDept].SetFocus
crrntDeptIndex = Me![crrntDept].ListIndex

Me![crrntDiv].SetFocus
crrntDivIndex = Me![crrntDiv].ListIndex

Me![crrntSDiv].SetFocus
crrntSDivIndex = Me![crrntSDiv].ListIndex

Me![crrntSSDiv].SetFocus
crrntSSDivIndex = Me![crrntSSDiv].ListIndex

Me![crrntSSSDiv].SetFocus
crrntSSSDivIndex = Me![crrntSSSDiv].ListIndex

crrntState = state

Dim selectStatement As String

Select Case state

    Case DEPARTMENT

        level = "Department"
        lvlSub = "Dept"
        prevLvl = vbNullString
        prevSub = vbNullString





    Case DIVISION

        level = "Division"
        lvlSub = "Div"
        prevSub = "Dept"
        prevLvl = "Department"

        Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "DivYearly_SectionNum"
        Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "Division Number"
        Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tbNumbersPages].Visible = True


        If CrrntId = -1 Then
            CrrntId = Me!Dept_ID

        End If

    Case SUBDIVISION
            level = "SubDivision"
            lvlSub = "SubDiv"
            prevSub = "Div"
            prevLvl = "Division"

            Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "SubDivYearly_ParaNum"
            Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "Subdivision Letter"

            If CrrntId = -1 Then
                 CrrntId = Me!Div_Id

            End If

     Case SUBSUBDIVISION
            level = "SubSubDivision"
            lvlSub = "SubSubDiv"
            prevSub = "SubDiv"
            prevLvl = "SubDivision"

            Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "SubSubDivYearly_SubParaNum"
            Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "SubSubdivision Number"

            If CrrntId = -1 Then
                 CrrntId = Me!SubDiv_ID

            End If

    Case SUBSUBSUBDIVISION
            level = "SubSubSubDivision"
            lvlSub = "SubSubSubDiv"
            prevSub = "SubSubDiv"
            prevLvl = "SubSubDivision"

            Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].ControlSource = "SubSubSubDivYearly_SubSectionNum"
            Me.fAdminToolsDetails.Form.sfAdminToolsYearly![SectionNumberLabel].Caption = "SubSubSubdivision Letter"

            If CrrntId = -1 Then
                 CrrntId = Me!SubSubDiv_ID

            End If

End Select

Me![lvlSub].ControlSource = "='" & lvlSub & "'"

Me.sfListOfLvlData.LinkChildFields = vbNullString
Me.sfListOfLvlData.LinkMasterFields = vbNullString

Me.fAdminToolsDetails.LinkMasterFields = vbNullString
Me.fAdminToolsDetails.LinkChildFields = vbNullString

If state <> DEPARTMENT Then

    Me.RecordSource = "SELECT DISTINCT " & prevLvl & "." & prevSub & "_ID FROM " & prevLvl & " WHERE " & prevLvl & "." & prevSub & "_ID = " & CrrntId
    Me.sfListOfLvlData.Form.RecordSource = "SELECT DISTINCT " & level & "." & lvlSub & "_ID, " & level & "." & lvlSub & "_Short_Name, " & level & "Yearly." & prevSub & "_ID FROM " & level & " " & _
    "INNER JOIN " & level & "Yearly ON " & level & "." & lvlSub & "_ID=" & level & "Yearly." & lvlSub & "_ID" & " ORDER BY " & level & "." & lvlSub & "_Short_Name"
    Me.sfListOfLvlData.LinkChildFields = prevSub & "_ID"
    Me.sfListOfLvlData.LinkMasterFields = prevSub & "_ID"
Else


    Me.sfListOfLvlData.Form.RecordSource = "SELECT " & level & "." & lvlSub & "_ID, " & level & "." & lvlSub & "_Short_Name FROM " & level & " ORDER BY " & lvlSub & "_Short_Name"
    Me.sfListOfLvlData.LinkChildFields = vbNullString
    Me.RecordSource = level
    Me.sfListOfLvlData.LinkMasterFields = vbNullString

End If




Me![Title].ControlSource = "='" & level & "'"
Me.sfListOfLvlData.Form![ShortName].ControlSource = lvlSub & "_Short_Name"
Me![parentId].ControlSource = prevSub & "_ID"

Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Year].SetFocus

If Not state = DIVISION Then

    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tbNumbersPages].Visible = False

End If


If Not state = DEPARTMENT Then







    Me.fAdminToolsDetails![COFRSCode].Visible = False
    Me.fAdminToolsDetails![functArea].Visible = False
    Me.fAdminToolsDetails![chbOperatingBudget].Visible = False
    Me.fAdminToolsDetails![chbCapitalBudget].Visible = False

    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].Visible = True
    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tabDirectors].Visible = False
    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].Visible = True
    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].RowSource = "SELECT " & prevLvl & "." & prevSub & "_Short_Name, " & prevLvl & "." & prevSub & "_ID " & _
                                                                            "FROM " & prevLvl & " " & _
                                                                            "ORDER BY " & prevLvl & "." & prevSub & "_Short_Name"
    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].ControlSource = prevSub & "_ID"




Else

    Me.fAdminToolsDetails![COFRSCode].Visible = True
    Me.fAdminToolsDetails![functArea].Visible = True
    Me.fAdminToolsDetails![chbOperatingBudget].Visible = True
    Me.fAdminToolsDetails![chbCapitalBudget].Visible = True

    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![tabDirectors].Visible = True
    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent].Visible = False
    Me.fAdminToolsDetails.Form.sfAdminToolsYearly![sectionNumber].Visible = False

End If
Me.mainID.ControlSource = "=[sfListOfLvlData]![" & lvlSub & "_ID]"

Me.fAdminToolsDetails.Form.RecordSource = level
Me.fAdminToolsDetails.LinkMasterFields = "mainID"
Me.fAdminToolsDetails.LinkChildFields = lvlSub & "_ID"
Me.fAdminToolsDetails![Name].ControlSource = lvlSub & "_Name"
Me.fAdminToolsDetails![Short_Name].ControlSource = lvlSub & "_Short_Name"
Me.fAdminToolsDetails![Cd].ControlSource = lvlSub & "_Cd"
Me.fAdminToolsDetails![Active].ControlSource = lvlSub & "_Active"
 Me.fAdminToolsDetails![Id].ControlSource = lvlSub & "_ID"

Me.fAdminToolsDetails![lstItemByYear].RowSource = "SELECT " & level & "Yearly." & lvlSub & "Yearly_Id, " & level & "Yearly." & lvlSub & "Yearly_Year" & _
                                                    " FROM " & level & "Yearly" & _
                                                    " WHERE (((" & level & "Yearly." & lvlSub & "_ID) = [Forms]![testAdminForm]![mainID])) " & _
                                                    "ORDER BY " & level & "Yearly." & lvlSub & "Yearly_Year"


Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form.RecordSource = level & "Yearly"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.LinkMasterFields = "Yearly_Id"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.LinkChildFields = lvlSub & "Yearly_ID"

Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Id].ControlSource = lvlSub &          "Yearly_ID"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Parent_Id].ControlSource = lvlSub & "_ID"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Year].ControlSource = lvlSub & "Yearly_Year"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Order].ControlSource = lvlSub & "Yearly_Order"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_Name].ControlSource = lvlSub & "Yearly_Name"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_FDesc].ControlSource = lvlSub & "Yearly_FDesc"
Me.fAdminToolsDetails.Form.sfAdminToolsYearly![Yearly_SDesc].ControlSource = lvlSub & "Yearly_SDesc"


Me![crrntDept].SetFocus
Me.crrntDept = Me.crrntDept.ItemData(crrntDeptIndex)

Me![crrntDiv].SetFocus
Me.crrntDiv = Me.crrntDiv.ItemData(crrntDivIndex)

Me![crrntSDiv].SetFocus
Me.crrntSDiv = Me.crrntSDiv.ItemData(crrntSDivIndex)

Me![crrntSSDiv].SetFocus
Me.crrntSSDiv = Me.crrntSSDiv.ItemData(crrntSSDivIndex)

Me.fAdminToolsDetails.Form.sfAdminToolsYearly.SetFocus
Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form.AllowAdditions = True
' Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form!Yearly_Order.SetFocus

DoCmd.RunCommand acCmdRecordsGoToNew

结束功能

2 个答案:

答案 0 :(得分:1)

我的问题的解决方案很简单:我本人就是一个新手的错误。我没有把焦点放在我想要操纵的形式上 最终代码:

Me.fAdminToolsDetails.Form.sfAdminToolsYearly.Form.SetFocus
DoCmd.RunCommand acCmdRecordsGoToNew

感谢您的帮助

答案 1 :(得分:0)

这是因为您尝试将新记录添加到不是通过表格而是通过选择显示的表单中

从“从表中选择”更改记录源

到“表格”

不可能在使用远程数据库时将其插入选择中