Results 1 to 11 of 11
  1. #1
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    479

    Using Format in On Click Event fails

    I'm converting code in an Excel workbook to Access and have struck something I don't understand.

    This works in Excel (and an Access module) but Type Mismatch errors in the On Click event of an Access Form. Is there any reason/explanation for that ?

    Code:
    Dim x As String
    x = "12/3/1967"
    Debug.Print Format(x, "dddd, dd mmm yyyy")

    Thanks for any help.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I just tested your code and it works perfect for me.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    479
    Thanks for replying June7, that is a bit odd it works differently. I solved it here (after a fashion) by
    sending x to a Module Function and returning it formatted. Maybe it's an Access2007 thing? I hope I have this
    right - I spent about an hour testing it.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    AFAIK, should be no different for 2007.

    Why not declare the variable as a date type instead of string? Or maybe don't even use variable. If variable is not used again, why have one? Or use CDate() function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    479
    I agree it should work, and I spent several hours trying all sorts, because it seemed so illogical.

    But the error is constant.
    Code:
    Private Sub Date_Entered_Click()
    Dim x As String
    x = "12/3/1967"
    Debug.Print Format(x, "dddd, dd mmm yyyy")
    End Sub
    I do want to use Date_Entered (which is a date Datatype), but when this errored x was tried as a test.

    This also returned the same Type mismatch Error
    Debug.Print Format(Date_Entered, "dddd, dd mmm yyyy")

    In the end this worked:

    Code:
    Private Sub Date_Entered_Click()
     Debug.Print DateFormatThis (.Date_Entered)
    End Sub
    
    Function DateFormatThis(d) As String
        DateFormatThis = Format(d, "dddd, dd mmm yyyy")
    End Function
    With the function in a module.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What type of control is the Click event associated with?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    479
    It's a text box bound to Date/Time field, and in continuous forms view.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Before I tested with a command button. Just tested with bound and unbound textboxes. Works.

    Are you using date picker?

    More testing with your final code. This does not work. No error but no formatted date. Until I gave the textbox a name different from the field. Now it calculates formatted date.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    479
    Not using date picker. I tried changing the textbox name but still a Type Mismatch.
    I give up !! But the function solves the problem so I guess I'll stick with that.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Bizarre. If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    479
    Bizarre I'll say. It's OK in a new database.

    But in this one

    Click image for larger version. 

Name:	Image3.jpg 
Views:	4 
Size:	19.0 KB 
ID:	20139



    The database shows no sign of any other problem. It's beyond me.
    Thanks for responding and helping though
    Attached Thumbnails Attached Thumbnails Image3.jpg  
    Last edited by Middlemarch; 03-24-2015 at 11:06 PM. Reason: tried to remove attachment too big.

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

Similar Threads

  1. Replies: 0
    Last Post: 02-25-2015, 08:09 AM
  2. SubForm on Click event
    By raffi in forum Forms
    Replies: 8
    Last Post: 09-27-2014, 11:22 AM
  3. On click event script
    By VOLB in forum Programming
    Replies: 2
    Last Post: 02-21-2014, 03:44 PM
  4. Access 2010 InfoPath Data Collection Export Fails Due to Date Format
    By raghureddy02 in forum Import/Export Data
    Replies: 3
    Last Post: 01-22-2014, 10:40 AM
  5. On-Click event transfer
    By nkenney in forum Forms
    Replies: 16
    Last Post: 03-26-2009, 09:02 PM

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