Object variable or with block variable not set in 2 places but nowhere else with same code

bioplz

New Member
Joined
Dec 1, 2021
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
In only 2 of 27 buttons this error occurs. In the first instance, it works, but any other selection after causes error.
1644341958812.png
1644342064511.png

When selecting Test 28 (or 29) with train 7 (The first Train) They work just fine.
Any train after causes the error on 28 and 29.
Train number is a value carried through multiple forms in a text box.
1644342300021.png

The issue highlights "frmTest29.txtTrain = Me.txtTrain"
The value of "Me.txtTrain" is correct (7, 8, 9, etc.)
The value of "frmTest28.txtTrain" contains the error.

If anyone can shine some light on this issue it would be greatly appreciated.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
To troubleshoot, put a break point at the start of the code. Cause the code to run (click the appropriate button in this case) and then step through line by line by pressing F8. After a line executes you can mouse over variables and references to see what values they contain, or you can inquire by typing ? followed by whatever it is you're trying to reference, and then press Return. Sometimes you need a property to check if the object reference is valid or not - Name almost always works, so
?Me.frmTest28.Name will tell you whether or not you've misspelled the form name. NOTE: the code has to be running in break mode for the Me. reference to work, so you'd do that when you reach the Call line. If that check doesn't cause an error, then check the control name you have after that:
?Me.frmTest28.txtTrain.Name

Your code is so simple that I have to think you've misspelled the form or textbox name.
 
Upvote 0
I get the correct value for Me.txtTrain (7, 8, 9, etc.). In the image provided you can see that I get the error for frmTest28.txtTrain. I also thought it was a misspelled word as it almost always is, but this code works for Me.txtTrain = 7 and not for Me.txtTrain = 8 or any subsequent number.

For reference, Train 7 is the first train used for this program, and this exact code is used for 26 tests and only these two fail (test28&test29) even though they pass for Train 7.
 

Attachments

  • objectVariableNotSet.png
    objectVariableNotSet.png
    6.8 KB · Views: 4
Upvote 0
You need to verify that all the objects are correctly referenced, not just Me.txtTrain.
What about frmTest28, and frmTest28.txtTrain? Did you try the Name thing in the immediate window with each of those?
You can check your userform properties in the immediate window as well (perhaps easier than writing a script to iterate over them). However, it's not clear if the problem is related to the userform reference or to the textbox reference. Those names have to be correctly spelled too.
Only once did I ever see that your error message was solved by adding a With block. Usually it's because a name is misspelled or you try to write code as if you were using a with block but didn't start one.
 
Upvote 0
You need to verify that all the objects are correctly referenced, not just Me.txtTrain.
What about frmTest28, and frmTest28.txtTrain? Did you try the Name thing in the immediate window with each of those?
You can check your userform properties in the immediate window as well (perhaps easier than writing a script to iterate over them). However, it's not clear if the problem is related to the userform reference or to the textbox reference. Those names have to be correctly spelled too.
Only once did I ever see that your error message was solved by adding a With block. Usually it's because a name is misspelled or you try to write code as if you were using a with block but didn't start one.
I have tried that method immediate window .Name and .Value method (which I wasn't familiar with before, so thank you), but the issue remains the same.
1644424564946.png

In this picture you see that the same code produces a value when "7" is the value for Me.txtTrain
That should mean that the spelling isn't the issue. I'm not sure how to set all of the variables or objects to avoid this.
It's just frustrating that this code works everywhere except these two exceptions (28, 29), but they work with "7".
 
Upvote 0
Wild shot in the dark but you could try
VBA Code:
UserForm1.Hide
With Me
 frmTest28.txtTrain = .txtTrain
End With

Call ClickTest28
Not sure about the Call line as I can't see what's being called. The only other suggestions I have at present is
a) replace the textbox on either or both forms in case it's corrupted
b) post a copy of the workbook somewhere and I'll take a look.
 
Upvote 0
Wild shot in the dark but you could try
VBA Code:
UserForm1.Hide
With Me
 frmTest28.txtTrain = .txtTrain
End With

Call ClickTest28
Not sure about the Call line as I can't see what's being called. The only other suggestions I have at present is
a) replace the textbox on either or both forms in case it's corrupted
b) post a copy of the workbook somewhere and I'll take a look.
Username: Admin
Password: Admin

After login click "Issues" to take you to the excel sheets and close the userforms.

There are also file locations that may have to be changed, but you should be able to look at the program without.
Thank you for your help.
 
Upvote 0
I click 'add new report', choose 7 from dropdown, click enter. Then it seems that no matter what button I choose from 10 to 36, there is no error. Instructions on how to cause the error might help.
 
Upvote 0
I click 'add new report', choose 7 from dropdown, click enter. Then it seems that no matter what button I choose from 10 to 36, there is no error. Instructions on how to cause the error might help.
Sorry. I thought that was in the above comments. Train 7 does not cause the error. The error is only caused in all subsequent cars (8, 9, 10, etc.) Selecting 28 or 29 in any of those trains will cause the error.
 
Upvote 0
OK, was finally able to raise the error. Will take some time to investigate.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top