Consulting

Results 1 to 20 of 20

Thread: Testing vba code

  1. #1
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location

    Question Testing vba code

    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

  2. #2
    Hi Celiab - Can you attach your code so I can see what you're tyring to do. I'm assuming everything is in access.

  3. #3
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    Yes, it's in Access.

    [vba]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[/vba]

    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!
    Last edited by Celiab; 03-22-2005 at 10:50 AM. Reason: explanation

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Celia, welcome to VBAX!

    [uvba]a[/uvba]

    I've edited your post to take advantage of them here.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    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.

  6. #6
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    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
    Last edited by Celiab; 03-22-2005 at 11:43 AM. Reason: figuring out how to enter msg.

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Celiab

    Your code works perfectly fine for me in Access 2000.

    Is that the only code you have?

  8. #8
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    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

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Quote Originally Posted by Celiab
    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.

  10. #10
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  11. #11
    VBAX Tutor SJ McAbney's Avatar
    Joined
    May 2004
    Location
    Glasgow
    Posts
    243
    Location
    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.

  12. #12
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    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

  13. #13
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Celia

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

    Did you goto the Modules tab and select New?

  14. #14
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    I went to Insert / Module from the Visual Basic screen.

  15. #15
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.

  16. #16
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    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?

  17. #17
    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.

  18. #18
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    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?

  19. #19
    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.

  20. #20
    VBAX Regular
    Joined
    Feb 2005
    Posts
    9
    Location
    Thanks for the help and db. I will be getting back to this early next week. I appreciate your help.

    celiab

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •