Old M$ sample code doesn't work, when changed? (1 Viewer)

twgonder

Member
Local time
Today, 13:04
Joined
Jul 27, 2022
Messages
178
I'm working my way through an old printed M$ manual on programming. It has some good examples.
However some just don't work. Like this one:

220826Screen.Active.jpg

(I triple checked that I typed it in exactly as the manual says)
I'm guessing that things have changed since M$ wrote the manual.
Can anyone give me a hint where to find the documentation on these kinds of changes and when they happened?
I'm still looking for off-line helps for Office/Access 2021 that I can use when not online. Any clues to that problem?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:04
Joined
Oct 29, 2018
Messages
21,538
I typed it this way, and it worked for me.
Code:
Public Function CurrentForm()
Dim CurrentFormName As String

CurrentFormName = Screen.ActiveForm.Name
Debug.Print CurrentFormName

End Function
Notice I used .Name instead of .FormName

Also, try to test it in the Immediate Window either without using the Parens or precede it with the print (?) command. For example:
Code:
?CurrentForm()
or
CurrentForm
PS. I just tested it with using FormName, it worked as well
PPS. Turns out FormName is a hidden member of the Form and Report objects
 
Last edited:

MarkK

bit cruncher
Local time
Today, 11:04
Joined
Mar 17, 2004
Messages
8,187
CurrentForm is a function that returns a value. When you run it in the immediate pane without a print command, the code executes correctly, but is unable to print the return value or assign it to a variable.
 

twgonder

Member
Local time
Today, 13:04
Joined
Jul 27, 2022
Messages
178
I tried it several ways, with and without the ? in the immediate window. Same compile error.
For grins I cut the middle two lines, deleted the function, created a new function, pasted the lines back in and it ran.
I can't see what went wrong. Go figure.
 

cheekybuddha

AWF VIP
Local time
Today, 19:04
Joined
Jul 21, 2014
Messages
2,321
You really need to have Option Explicit declared at the top of every code module (above or below Option Compare Database)
 

twgonder

Member
Local time
Today, 13:04
Joined
Jul 27, 2022
Messages
178
I did, but I turned it off to see if that made a difference. Still failed but compiled fine before run.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 19:04
Joined
Feb 19, 2013
Messages
16,668
you have not declared the type for currentformname so it will be treated as a variant

but then you say
I turned it off to see if that made a difference. Still failed but compiled fine before run.
it would fail on compile (with Option Explicit) because option explicit requires all variables to be declared. There would have been a very clear message 'variable not defined' and highlighting the variable in question when you click OK - currentformname in this case

The fact you get a different compile error with option explicit implies you have already declared currentformname as something else - perhaps a sub or another function

As far as old examples are concerned, as the VBE has matured it has become less tolerant of sloppy coding, so quite possible that what was acceptable 20 years ago isn't now. Not saying that is the case here, just be aware that it might be a reason
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 19:04
Joined
Sep 12, 2006
Messages
15,710
do you have option explicit in your modules?
you can force it with "require variable declaration" in tools/options in a code module

I note you used currentformname in the code sample, but haven't defined it in that function, although it might be defined elsewhere.
 

twgonder

Member
Local time
Today, 13:04
Joined
Jul 27, 2022
Messages
178
The code as I have it now, with the exception of the first and last lines is an exact cut&paste. It works and compiles fine with or without the Option Explicit. The immediate window has no problem with or without the "?" as the first character (I often forget it). Something strange happened the first time I inserted the procedure, I made it originally private, and then changed it to public to run it from the immediate window. That may have been the source of the problem, as the second time I inserted the procedure I went straight for public.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 19:04
Joined
Feb 19, 2013
Messages
16,668
doesn't really answer the question I posed in post #7
 

twgonder

Member
Local time
Today, 13:04
Joined
Jul 27, 2022
Messages
178
doesn't really answer the question I posed in post #7
I'm sorry, I don't see a question in your #7. It would appear that I hit a glitch in Access, as just copying and pasting the code into a new procedure in the same module fixed the problem. All the other considerations are irrelevant, as it started working with just that (even though I did make several changes, including explicit, before for testing, and those didn't work).
 

CJ_London

Super Moderator
Staff member
Local time
Today, 19:04
Joined
Feb 19, 2013
Messages
16,668
I missed of a question mark

The fact you get a different compile error with option explicit implies you have already declared currentformname as something else - perhaps a sub or another function?
 

Users who are viewing this thread

Top Bottom