Microsoft Access Click Event on Form produces error

11.8k Views Asked by At

I created two forms in Microsoft Access 2010: let's call them Form1 and Form2.

A Button on Form1 is supposed to call Form2. Form2 has two option buttons that are inside and optiongroup form, and two buttons, one of which is cancel. I created the following code which worked perfectly for a while:

Private Sub cmdCancel_Click()
    DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdCreateFactsheet_Click()
Dim sFund As String

If Me.OptionGroup = 2 Then
    On Error Resume Next
        sFund = Me.ComboFundliste.Value
    On Error GoTo 0
    If sFund = "" Then
        MsgBox ("Please select a Fund")
        End
    Else
        Call modAdvisoryFactSheet.FactSheetSelection(sFund)
    End If
End If

End Sub

Private Sub frmSelection_Open()
    Me.OptionGroup.DefaultValue = 1
End Sub

Private Sub OptOneFund_GotFocus()
    If Me.OptionGroup = 2 Then
        Me.ComboFundliste.Enabled = True
    End If
End Sub

Private Sub OptAllFunds_GotFocus()
    If Me.OptionGroup = 1 Then
        Me.ComboFundliste.Value = ""
        Me.ComboFundliste.Enabled = False
    End If
End Sub

Now, every single Sub produces an error when I try to open form2, or if I open form2 manually, when I try to klick on any of the controls.

The message is always the same: The expression On Click you entered as the event property setting produced the following error: variable not defined

I realize this must have something to do with how I reference the form, but I don't understand why this worked well and then suddenly stopped working. I didn't change anything as far as I can remember and I don't see what is wrong!

Any help appreciated.

2

There are 2 best solutions below

2
On BEST ANSWER

This answer really belongs to Matt Hall. Matt, if you want to answer it, I'd be happy to give you the checkmark. For the time being, I'll put this up so it's clear what the solution was:

I had to run Debug>Compile in order to see the line that caused the error. It turned out that I had a Variable in a Sub several level down, that wasn't properly defined.

0
On

This did not quite cover the problem but I wanted to share my really simple solution. I have an Access 2010 Split DB that runs a form on startup. Occasionally, I'll get "The Expression X you entered as the event property produced the following error...". Since this occurs at start up, X initially = "On Load".

Clicking through the dialog box will bring up the form, but then ALL the other controls will produce the same error with X being the event that would normally be handled (On Click, Dbl Click, After Update, etc.).

A temporary fix occurs by just opening the Visual Basic window through Database Tools or switching to Design View and then View Code. The form will then work until you close the DB. When you reopen it, the error occurs again.

A longer fix occurs by opening the Visual Basic window and making ANY change. By any, I do mean adding a space or deleting one. Saving the form and exiting fixes the problem DB until.....it decides to happen again.