Results 1 to 12 of 12
  1. #1
    bgephart is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    15

    Trouble with Date()

    I'm trying to create a button that will automatically put the current date in the [InDate] field of one of my forms. It shouldn't be that complicated, but I can't get it to work. I've tried using InDate = Date in the code, and that didn't work. I've also tried [InDate] = Date() in the Expression Builder. What am I doing wrong?

    Thanks for your help.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    In code, refer to the control containing the field:

    Me.InDate = Date()
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    bgephart is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    15
    That's actually one of the things I tried, but the VBA editor keeps removing the "()" from the code. I don't know whether it means anything, but I'm creating this on Access 2007, but it's a DB that has to be compatible with 2003, so that's the way it is saved. That shouldn't make a difference, should it? Any idea what's going on or how to fix it?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Sometimes the compiler will remove the parentheses. Does it not work? Do you by chance have a field or control named "date"? If so, or in any case, try

    Me.InDate = VBA.Date()
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    bgephart is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    15
    Just tried that and I got the same error message, "The expression On Click you entered... :Invalid outside procedure". I don't have any controls named just "date".

    Help!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Where do you have that? It would have to be in the VBA editor:

    http://www.baldyweb.com/FirstVBA.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by bgephart View Post
    "The expression On Click you entered... :Invalid outside procedure"
    This error message generally means that you have some text, in the Code Module of your Form, that is not inside of the Opening Header and Closing Footer of a Sub Procedure. For instance, this code would run without problems:

    Code:
    Private Sub FirstCommandButton_Click()
      Msgbox "First Button was Clicked"
    End Sub
       
    Private Sub SecondCommandButton_Click()
       Msgbox "Second Button was Clicked"
    End Sub


    But in this code:

    Code:
    Private Sub FirstCommandButton_Click()
      Msgbox "First Button was Clicked"
    End Sub
    
     Msgbox "Second Button was Clicked"  
    
    Private Sub ThirdCommandButton_Click()
       Msgbox "Third Button was Clicked"
    End Sub


    the line


    Msgbox "Second Button was Clicked"

    would give you this very error because the line falls 'outside procedure' and therefore is 'Invalid.'

    This kind of thing can happen, especially in older apps, or in apps that have been in development for a fair amount of time, because the developer has deleted some code, but left some remnants undeleted .

    Also, as Paul has suggested, it could be the result of your having gone to Properties - Events and entering Me.InDate = VBA.Date().

    If it is in the Code Module, I believe this errant code will be found just prior to an OnClick event, in your code. Does the error actually give you the name of the OnClick sub; I can't remember. IF it does, go to that Sub, in your Code Module, and look around.

    Linq ;0)>

  8. #8
    bgephart is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    15
    What I have is in the VBA Code - exactly this:

    Private Sub Command15_Click()
    Me.InDate = VBA.Date()
    End Sub

    And the error does refer to "On Click". Is there anything else that could be causing this?

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Try compiling the code. Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by bgephart View Post
    What I have is in the VBA Code - exactly this:

    Private Sub Command15_Click()
    Me.InDate = VBA.Date()
    End Sub
    Do you meant that this code is all the code that you have in the Module? If not, have you searched for 'stray' text, as suggested above?

    Also, although we usually think of Forms and even entire Databases when we speak of corruption, Controls such as Command Buttons and Comboboxes can and do become corrupted, and seem particularly susceptible to this during app development. The test/cure for this is short and simple...delete and then re-create the Control.

    Linq ;0)>

  11. #11
    bgephart is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    15
    Okay - I finally just started over with a new database (I hadn't done THAT much work), and it works now. Something along the line must have gotten corrupted or something.

    Thanks!

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-23-2012, 01:18 PM
  2. Trouble importing date from CSV
    By NickP in forum Import/Export Data
    Replies: 1
    Last Post: 01-13-2012, 04:12 AM
  3. Date diff trouble
    By Cran29 in forum Queries
    Replies: 3
    Last Post: 03-11-2011, 01:02 PM
  4. Trouble with (R)
    By NOTLguy in forum Access
    Replies: 3
    Last Post: 10-29-2010, 11:55 AM
  5. Iff trouble
    By JackT in forum Access
    Replies: 3
    Last Post: 08-18-2010, 02:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums