简体   繁体   中英

Access Compile Error: Argument not Optional

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

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

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

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

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).

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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM