Results 1 to 6 of 6
  1. #1
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44

    Type mismatch when setting Date variable from textbox

    Hi guys



    I have a simple form with 8 controls where the user can input into data into a minumum of one up to all eight. Therefore, not every control is required to be filled in.

    I set a bunch of variables based on the values in the controls for later use in some SQL code. The rest all work perfectly, apart from my Date of Birth component. This is a basic textbox but with the Short Date format and an input mask. I plan on adding data validation at some point as well, but I just want to get up and running with some VBA SQL code first.

    The code is used to test if the textbox is neither null nor an empty string, and if it isn't then it pulls out the date entered into a Date type variable. It works fine when a date is entered. However, it bugs out due to "type mismatch" when the textbox is empty, hovering my cursor over the value indicates Me.TxtPatDoB = "" (I think).

    Code:
    'patient DoB should be entered full (will use data validation to ensure)    If Not IsNull(Me.TxtPatDoB) Or Me.TxtPatDoB <> "" Then
            dateOfBirth = Me.TxtPatDoB.Value
        End If

    Is there a workaround or better solution?

    Thank you

  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,521
    Your test is logically incorrect. You want And rather than Or. You could also use:

    If Len(Me.txtPatDoB & vbNullString) > 0 Then

    or IsDate().
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    I was testing an OR because for my other textboxes, sometimes they are classed as either null (for instance on the Form load) or "" is the user enters a value and then deletes it. Therefore, I want to test whether it was one condition or the other. Surely using an AND would never be fulfilled, as it cannot be Null and an empty string at the same time?

    Using IsDate still gave me the error, but the Len solution appears to work.

    Thank you

  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,521
    You can use Or when you're testing with =. When you use Not or <>, the logic reverses. If my test is "if my name is not Bill OR my name is not Paul", I've passed the test because it's not Bill, and I used OR. I'd want "if my name is not Bill AND my name is not Paul".
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    I typed out a massive post replying how I thought OR was giving me the same answer as AND, only to realise that you were in fact correct

    Thank you

  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,521
    No problem. I'm always correct...except when I'm wrong.
    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: 5
    Last Post: 09-18-2014, 12:26 PM
  2. type mismatch
    By slimjen in forum Forms
    Replies: 21
    Last Post: 07-24-2012, 03:14 PM
  3. Year(date) returns type mismatch
    By reema in forum Programming
    Replies: 9
    Last Post: 03-28-2012, 08:46 AM
  4. Date - Textbox type mismatch 13
    By mdex in forum Programming
    Replies: 7
    Last Post: 01-20-2012, 08:44 AM
  5. Type Mismatch
    By Alex Motilal in forum Programming
    Replies: 10
    Last Post: 02-13-2011, 05:42 AM

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