PDA

View Full Version : Testing vba code



Celiab
03-22-2005, 09:03 AM
Can someone tell me how I can write some code (within an app) and then run (F5) it to test? It tells me macro not found. I don't want to run a macro; I want to run the code in a module. This is functional in Excel---why not in Access!:(

Thanks, celiab

Groundhog
03-22-2005, 10:36 AM
Hi Celiab - Can you attach your code so I can see what you're tyring to do. I'm assuming everything is in access.

Celiab
03-22-2005, 10:48 AM
Yes, it's in Access.

Sub TestWkDay()
Dim MyDate, MyWeekDay
MyDate = Date
MyWeekDay = Weekday(MyDate)
MsgBox ("week day is " & MyWeekDay)
If MyWeekDay = 2 Then
MsgBox ("Today is Monday.")
Else
MsgBox ("It's not Monday today.")
End If
End Sub

To run this code I do F5. This is just a sample of the testing I need to do now and then; obviously this is very simplistic.

Thanks!

Ken Puls
03-22-2005, 11:04 AM
Hi Celia, welcome to VBAX!

a

I've edited your post to take advantage of them here. :yes

Groundhog
03-22-2005, 11:19 AM
If you're using a form you can copy your code to it. Select your form and click on design. Then from the toolbar click on view and select code. From the first drop down box select Form. From the second drop down box select Open. Copy your code here. When the form is opened it will run through your code. You can also add a break point to your code by clicking within the left margin of the code window. To step through the code line by line hit F8 otherwise hit F5. You mentioned that you wanted to do this in a module. If that is the case copy your code to a module and then refer to your module in the Form / Open event mentioned above. I hoped this helped.

Celiab
03-22-2005, 11:41 AM
I know that I can test the code by connecting it to another action,
but it is so easy, quick and efficient in Excel by the F5 to run it while
working on it in the module. Why should it act differently in Access than
Excel? I thought the goal of MS was to standardize vba throughout
the apps!

Celiab

Norie
03-22-2005, 12:39 PM
Celiab

Your code works perfectly fine for me in Access 2000.

Is that the only code you have?

Celiab
03-22-2005, 01:11 PM
Did you run it only from within the module, by hitting F5?

The problem isn't the code; it's testing it from the module.

celiab

Norie
03-22-2005, 01:18 PM
Did you run it only from within the module, by hitting F5?

The problem isn't the code; it's testing it from the module.

celiab

Yes

Have you checked Tools>References...?

Is there any with MISSING against them?

Perhaps the database has become corrupted.

TonyJollans
03-22-2005, 05:30 PM
Hi Celiab,

I don't really understand what your problem is, but there is some confusion with the term macro - an Access Macro and a VBA Macro are different.

You wrote some code in a Module. You pressed F5 to test it. Right so far?

You got an error message - macro not found.

If this happened with the code you have posted I don't know what to suggest beyond what Norie has already said. There is nothing wrong in what (I think) you are doing. Is the error a compile error, or can you single step (F8) till you hit it? Either way, what statement is flagged as in error?

SJ McAbney
03-23-2005, 04:22 AM
The problem is because you are trying to run a subroutine within a Class module. As the code is enclosed within this Class module (being that of the form) it can't be run. Consider moving the routine to a standalone module where it can be run.

Celiab
03-23-2005, 12:20 PM
No, F8 to step through does nothing except beep at me. Checked references and there is no indication "missing". Does F5 work for you, from within a Sub in a module of code??

Hey, I'll try that!! Thx.

That worked! Thanks very much, all.

celiab

Norie
03-23-2005, 01:01 PM
Celia

How did you create the module that the code is in?

Did you goto the Modules tab and select New?

Celiab
03-23-2005, 01:04 PM
I went to Insert / Module from the Visual Basic screen.

Norie
03-23-2005, 01:46 PM
Just tried this.

Right clicked a form, selected View Code, then went Insert>Module.

It created a new module under Modules in the Project Explorer(the form module is under MS Access Class Objects).

Then I pasted your code into this new module and it worked fine with F5 or F8.

By the way where is the cursor when you are trying F5/F8.

If I have the cursor outside the sub and hit F5 the Macro dialog pops up, if I press F8 I get the clicking.

Celiab
03-23-2005, 01:56 PM
I make sure my cursor is within the Sub. Like I said, it works great now that I know I must start a new module, and not try it in a Class module.

However, when I right click a form, either in view or design mode, no "View Code" is available. Where do you see that?

Groundhog
03-23-2005, 08:04 PM
Hi Celiab -

I'm glad F5 is working for you. To view the code for a form, click once on the form so it's highlighted. From the menu toolbar select View and then Code. If you're already in design mode you may also select View/Code from the menu toolbar as well.

Celiab
03-24-2005, 08:34 AM
Thanks. I've often viewed the form's control's code but didn't realize they were in a Class module, and don't know the diff between Class and a regular module, as far as Access is concerned. However, now that I know where and how to test code without going through all the connections, I'm satisfied.

BTW, do you know, or could you give me a clue, on how to save dates from a data entry form where those 2 dates need to appear on the subsequent report (as in the date parameters for the filter shown in the report heading)? They do appear in report view, but not when the report is printed, since the form has been closed and variables not still available. Even when I add code to hold the values in a Public sub, they do not appear on the report. What am I missing?

Groundhog
03-24-2005, 11:19 AM
I'm attaching a sample database. It has been compressed so you will need to unzip it. If the only information that is being supplied on your form is a date range then you may not need a form at all. The date range can be requested from the user when the report is activated. See rptDate report. The date range entered will be displayed as a page header and it will be used as a filter for the data selection (where criteria).

The second report rptDate2 is dependent upon a form, frmReport. Activate frmReport and supply the date values. Then click on print report. The dates supplied within the form are used for the page header and as a filter. This report, however, should only be viewed by accessing the form since it's getting date values from it.

Celiab
03-24-2005, 12:53 PM
Thanks for the help and db. I will be getting back to this early next week. I appreciate your help.

celiab