Even Sample code from Microsoft won't work for me

Umpire

Member
Local time
Yesterday, 23:56
Joined
Mar 24, 2020
Messages
120
(Running Access365)
As I am trying to learn VBA I am experimenting with the On Event feature of some of my forms. Just something simple so I can understand what is happening and when. Just a simple place to start.
I tried to set it up so that when the cursor left one field the program would beep. So I tried to put the code in the Before Update event.

I found this code on a Microsoft site:
Dim I
For I = 1 To 3 ' Loop 3 times.
Beep ' Sound a tone.
Next I

And it did not work.
So I tried running the code in the VBA editor Immediate window and I received a "Compile Error: Next without For" pop-up.

If I can not even get Microsoft Provided code to work what the heck am I supposed to do?
 
So the code you've posted is exactly a copy and paste of what you're currently using - no changes? Did you copy & paste directly from your code project into AWF ?

What line of code is being highlighted by the compiler?
 
I used the copy function on the Microsoft page. (See Beep Source.png)
There is no line highlighted. (See Beep Fail 1.png)
I was looking up to see if I could change the tone of the beep (I saw there was a way to do that but that is for future me) and saw this code that wil beep 3 times and thought I would try it.

Where I want to get to is checking the contents of a field and if it passes or fails the validation, it would beep once or 3 times. Again this is mainly a way for me to get used to writing and working with VBA.
 

Attachments

  • Beep Source.PNG
    Beep Source.PNG
    146.2 KB · Views: 273
  • Beep Fail 1.PNG
    Beep Fail 1.PNG
    11.1 KB · Views: 396
Hi. The error message is understandable. You can't execute a block of code in the Immediate Window. By design, it can only execute one line of code at a time.

Just to see if your event is firing, try replacing Beep with a MsgBox command.
 
Hi. The error message is understandable. You can't execute a block of code in the Immediate Window. By design, it can only execute one line of code at a time.

Just to see if your event is firing, try replacing Beep with a MsgBox command.
Thanks.
THAT was not explained properly in the book I am using. (Or maybe I miss-read it. Time to re-read it)
So the error is not a "True" error, just the programs way of telling me I am trying to do something that it cant do.
yeah a MsgBox ("It Worked") will tell me if things are happening the way and time I expect.
 
I used the copy function on the Microsoft page. (See Beep Source.png)
There is no line highlighted. (See Beep Fail 1.png)
I was looking up to see if I could change the tone of the beep (I saw there was a way to do that but that is for future me) and saw this code that wil beep 3 times and thought I would try it.

Where I want to get to is checking the contents of a field and if it passes or fails the validation, it would beep once or 3 times. Again this is mainly a way for me to get used to writing and working with VBA.
It makes sense now , you are trying to execute it in the immediate window. Try putting it in a regular Sub

Sub Test()
...your code
End Sub

The immediate window is generally good for one-liners. It can also tolerate line continuation symbols.
 

Users who are viewing this thread

Back
Top Bottom