Tiffany V
Tiffany V

Reputation: 57

MS Access VBA - Variable not defined error when variables have been declared

I have created a form with some fields that I want to transfer to a new form with the use of variables. The settings for the form's code are 'Option Compare Database' and 'Option Explicit.' I have declared four public variables just below this as follows:

Option Compare Database
Option Explicit
Public LnNum As Long
Public InvLnNum As String
Public Inv As String
Public DefID As Long

Further down is the code to open the form. I have a button set-up on the first form, and when it opens I want a few fields to copy over to the new form (these fields have the same names). This is the code for that specific event:

Private Sub cmdNewDefect_Click()

LnNum = Me.LoanNumber.Value
InvLnNum = Me.InvestorLoanNumber.Value
Inv = Me.Investor.Value
DefID = Me.ID.Value

DoCmd.OpenForm "frmInvestorDefects"
DoCmd.GoToRecord acDataForm, "frmInvestorDefects", acNewRec
Form_frmInvestorDefects.LoanNumber.Value = LnNum
Form_frmInvestorDefects.InvestorLoanNumber.Value = InvLnNum
Form_frmInvestorDefects.Investor.Value = Inv
Form_frmInvestorDefects.ID.Value = DefID

End Sub

When I click the button I get the error message, 'Compile Error: Variable Not Defined.' I have another database with multiple forms that do the exact thing that I am wanting this form to do without issue. The code is exactly the same, except that the event occurs on an After Update event instead of On Click, so I am stumped. I have tried moving the variable declarations into the actual sub procedure with Dim statements instead of Public, but that doesn't work either. I am thinking there is a setting or something that I am forgetting. Thanks in advance!

Upvotes: 0

Views: 1044

Answers (1)

user3662334
user3662334

Reputation: 41

If the form you are opening is: "frmInvestorDefects" Then try:

frmInvestorDefects.LoanNumber.Value = LnNum

Not sure why the 'Form_' prefix is required...
Not sure why the .Value part is needed either

frmInvestorDefects.LoanNumber = LnNum 

or

[frmInvestorDefects].[LoanNumber] = LnNum

Upvotes: 0

Related Questions