vba 访问编译错误:参数不是可选的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14042449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:58:45  来源:igfitidea点击:

Access Compile Error: Argument not Optional

formsfunctionvbaaccess-vbasubform

提问by designspeaks

I have an access Database, this is similar to a previous question I asked, with a dropdown and a subform. I want to be able to choose an option from the dropdown and have it open a corresponding subform in the subform below. Here is my code...

我有一个访问数据库,这类似于我之前问过的一个问题,有一个下拉菜单和一个子表单。我希望能够从下拉列表中选择一个选项,并让它在下面的子窗体中打开相应的子窗体。这是我的代码...

Option Compare Database
Option Explicit

Private Sub btnCloseHRForms_Click()
  DoCmd.Close

End Sub

Private Sub cmbSelectFrms_AfterUpdate()
Select Case selectSubform
  Case 1
    Forms!frm_HRForms!subformHRForms.Form!subform1.Visible = True
  Case 2
    Forms!frm_HRForms!subformHRForms.Form!subform2.Visible = True
  Case 3
    Forms!frm_HRForms!subformHRForms.Form!subform3.Visible = True
End Select
End Sub

Private Sub Form_Load()
Dim dba As Database
Dim rst As Recordset
Dim SQL As String

Set dba = CurrentDb
Set rst = dba.OpenRecordset("tbl_Forms", dbOpenDynaset, dbSeeChanges)
SQL = "SELECT ListName FROM tbl_Forms"
Set rst = dba.OpenRecordset(SQL, dbOpenDynaset, dbSeeChanges)

Set rst = Nothing
Set dba = Nothing
End Sub

Function selectSubform(ID)
Dim dbacurrent As Database
Dim rstcurrent As Recordset
Dim SQL As String

Set dbacurrent = CurrentDb
SQL = "SELECT * FROM tbl_Forms WHERE ID = " & ID
Set rstcurrent = dbacurrent.OpenRecordset(SQL, dbOpenDynaset, dbSeeChanges)
selectSubform = rstcurrent.Fields("ID")

Set dbacurrent = Nothing
Set rstcurrent = Nothing

End Function

Any suggestions? New to Access VBA

有什么建议?刚接触 VBA

回答by XIVSolutions

Your Function selectSubform(ID) Requires an ID parameter be passed, and you are not passing one. When you define the function:

您的函数 selectSubform(ID) 需要传递一个 ID 参数,而您没有传递一个。定义函数时:

Function selectSubform(ID)
    ' ... Do some stuff
    selectSubform = SomeValue
End Function

You are telling the compiler that a Parameter named ID is required.

您告诉编译器需要一个名为 ID 的参数。

As an aside, I woiuld strongly recommend you place an Option Explicit statement at the top of each code module, and turn on the Option Explicit option in the editor. This will require you to indicate data types. As things sit, we have no idea what data type is expected as the ID parameter, and no idea what data type is returned by your function ( although it is implied by the use as an apparent integer in your Select Case statement). Making some assumptions, I would try the following changes (I can't test this right now, nor can I speak to the rest of your code).

顺便说一句,我强烈建议您在每个代码模块的顶部放置一个 Option Explicit 语句,并在编辑器中打开 Option Explicit 选项。这将要求您指明数据类型。就目前情况而言,我们不知道期望什么数据类型作为 ID 参数,也不知道您的函数返回什么数据类型(尽管在 Select Case 语句中用作明显整数暗示了这一点)。做出一些假设,我会尝试以下更改(我现在无法测试,也无法与您的其余代码交谈)。

I am assuming here that the Selection in the combo box is the ID for the appropriate sub form. If not, you may have to clarify for us where the ID parameter is sourced from):

我在这里假设组合框中的选择是相应子表单的 ID。如果没有,您可能需要向我们说明 ID 参数的来源):

Private Sub cmbSelectFrms_AfterUpdate()
    Select Case selectSubform(Me.cmbSelectForms)
        Case 1
            Forms!frm_HRForms!subformHRForms.Form!subform1.Visible = True
        Case 2
            Forms!frm_HRForms!subformHRForms.Form!subform2.Visible = True
        Case 3
            Forms!frm_HRForms!subformHRForms.Form!subform3.Visible = True
    End Select
End Sub 

There may be other issues buried in here. However, setting your VBA IDE to require explicit variable declaration and adding Option Explicit to each of your code modules will go a long way towards helping you identify problems.

这里可能还隐藏着其他问题。但是,将您的 VBA IDE 设置为需要显式变量声明并将 Option Explicit 添加到您的每个代码模块将大大有助于您识别问题。