Setting a field to today's date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sueb
    Contributor
    • Apr 2010
    • 379

    #1

    Setting a field to today's date

    I would have thought that this operation would be simple:
    Code:
    Me.IUR_DATE.Value = DATE()
    but the parentheses simply refuse to remain.

    (Although I've been a programmer for 20 years, I'm finding Access really murky. Is it just me? That would be depressing...)

    Anyway, here's the context:
    Code:
    Private Sub CLEAR_IUR_1_Click()
    On Error GoTo Err_CLEAR_IUR_1_Click
    
        Dim ctl As Control
        
        For Each ctl In Me.Controls
          If ctl.Tag = "Clear" Then
             ctl.Value = Null
          End If
        Next
        
        Me.IUR_DATE.Value = DATE
    
    Exit_CLEAR_IUR_1_Click:
       Exit Sub
    
    Err_CLEAR_IUR_1_Click:
        MsgBox Err.Description
        Resume Exit_CLEAR_IUR_1_Click
    What am I not understanding about this syntax?

    Thanks!
    Last edited by NeoPa; May 4 '10, 02:30 PM. Reason: Please use the [CODE] tags provided.
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Yes, Access VBA will remove the parens in some instances, but it doesn't affect the code, it should still work.

    IMHO, Access VBA is no 'murkier' than any other language; they all have their quirks! The important thing to understand, especially for programmers who have worked previously in straight Visual Basic, is that VBA is a different language. Many things are the same, but many functions, even those with similar or even identical names, perform differently.

    Linq ;0)>

    Comment

    • sueb
      Contributor
      • Apr 2010
      • 379

      #3
      I've never worked in "straight Visual Basic"--my time was spent mostly with Ada.

      Be that as it may, the very reason I'm posting the question is because it DOESN'T work. My date field does not get set to today.

      Comment

      • slenish
        Contributor
        • Feb 2010
        • 283

        #4
        Are you trying to make the field and autodate feature? Where it will put the date in for you instead of someone one having to key in the date.

        If that is the case all you need is in the properties for the text box (on your form) you are using put this in the default value field

        Code:
        =Int(Now())
        Then you can actually sent the text box to not visible link it to your table and it will automatically save todays date to your table every time something is entered.
        hope that helps

        Comment

        • sueb
          Contributor
          • Apr 2010
          • 379

          #5
          Not exactly. I need the field to be set when the user takes a specific action (clicks a little button that is on the form) rather than when there record is created.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            Sue,

            It looks to me very much like some other object is defined, and available to your database, with a name of DATE. If it were being recognised as the Date() function (from the VBA library) then only the D would be capitalised. Do you know how to use the Object Browser (See Debugging in VBA) to find what is declared where?

            As Linq intimated earlier, your code has no obvious bug in it. What you have should normally work.

            Comment

            • sueb
              Contributor
              • Apr 2010
              • 379

              #7
              Originally posted by NeoPa
              Sue,

              It looks to me very much like some other object is defined, and available to your database, with a name of DATE. If it were being recognised as the Date() function (from the VBA library) then only the D would be capitalised. Do you know how to use the Object Browser (See Debugging in VBA) to find what is declared where?

              As Linq intimated earlier, your code has no obvious bug in it. What you have should normally work.
              That makes real sense. I'll look around, but on the good chance that that is actually the case, I know that I'll not want to mess that object, so how can I specify here that what I want is the Date from the VBA library and not any other?

              Comment

              • missinglinq
                Recognized Expert Specialist
                • Nov 2006
                • 3533

                #8
                but on the good chance that that is actually the case, I know that I'll not want to mess that object, so how can I specify here that what I want is the Date from the VBA library and not any other?
                You can't! You simply have to change the name of the object to something other than Date!

                Linq ;0)>

                Comment

                • sueb
                  Contributor
                  • Apr 2010
                  • 379

                  #9
                  Originally posted by missinglinq
                  You can't! You simply have to change the name of the object to something other than Date!

                  Linq ;0)>
                  Wait, what?? You mean the language doesn't support overloaded names and the ability to specify which you want?

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32669

                    #10
                    It does to a certain extent, but Date is a fundamental part of the basic setup. It is always recommended very strongly not to overload such items due to the confusion they will necessarily cause.

                    In this case, it is possible, but I repeat not a recommended solution, to use VBA.Date.

                    Comment

                    • sueb
                      Contributor
                      • Apr 2010
                      • 379

                      #11
                      What are the pitfalls, in this case, of using specific notation to access VBA's date function?

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32669

                        #12
                        None, as such Sue.

                        The pitfalls generally are associated with reusing common names in a project. Reading and understanding what is going on is always likely to be harder when there is any possibility of ambiguity. Using VBA.Date() is not ambiguous, but having some object called DATE is likely to be. Does that clarify?

                        Comment

                        • sueb
                          Contributor
                          • Apr 2010
                          • 379

                          #13
                          I totally agree, but since I'm just maintaining this databse (and part-time at that!) I just wanted to know how I could specify that I wanted to access the system date and not whatever function some previous developer overloaded that name with.

                          Thanks to you, I can now do that!

                          (This site is wonderful!)

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32669

                            #14
                            It's always pleasant to read such responses. You're welcome of course :)

                            Comment

                            Working...