3
votes

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
Try debugging your code. Click on the margin of the line that calls your form 2 (this should come up with a brown highlight. When you go back to your form and click the button, the control will go to the VBA code, now manually walk through the code using F8 button, you will know where the error occurs.PaulFrancis
I don't even get to the point where I can start stepping through the code. I have tried commenting out lines of code, but in order for it to open the form without an error message, I need to comment out the entire sub!rohrl77
The best place to start (as mentioned in my comment) the line that calls your form 2.PaulFrancis
i am calling Form2 using the On Click Event Property in Form1 using a macro (not VBA Code, but the built in ones you create using Macro Tools)... so, as I said, I don't get to a first line of code.rohrl77
When you did it in VBA did you catch the line of code it was highlighting the error on? The other thing you can do is Debug > Compile and see if breaks somewhere (if it does, tell us where). Can you walk us through the line Call modAdvisoryFactSheet.FactSheetSelection(sFund).Matt Hall

2 Answers

2
votes

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.

2
votes

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.