Results 1 to 7 of 7
  1. #1
    EvanRosenlieb is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Location
    Fort Collins, CO
    Posts
    37

    NZ() function not working?

    Hello all, you have helped me in the past, lets see if anyone knows what it going on here.



    I have a form that uses a recordset to update a query. Among the fields that need entered are coordinate fields, which are Numeric Double fields. The coordinates must have a Y1 and X1 coordinate, and they may have a Y2 and X2 coordinate, but not necessarily.

    This causes a 'data type conversion' error when either the Y2 or X2 fields are left empty, as it does not want to input a null value into the field.

    Me understanding is that the Nz() function should be able to fix this, as it returns an empty string instead of Null. However I still get the same 'data type conversion" error

    Here is my (selected) code:

    Code:
    Set rsBoxEntry = WorkingDB.OpenRecordset(Me.Year, dbOpenDynaset)
        For i = 1 To Me.HowMany
            rsBoxEntry.AddNew
                 
                    rsBoxEntry!Y1 = Nz(Me.Y1)
                    rsBoxEntry!Y2 = Nz(Me.Y2)
                    rsBoxEntry!X1 = Nz(Me.X1)
                    rsBoxEntry!X2 = Nz(Me.X2)
    For instance, if I have a number entered in the "Y1" field of my form, and not in the "Y2" field, when I try to save the records its pops up with the "data type conversion error" with the "Y2" line of code highlighted in the debugger. Am I using the Nz() function wrong?

    Any help is appreciated.

    --Evan

  2. #2
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    NZ(ME.X1) returns an Empty String If x2 and y2 are Doubles then you'll get a data type conversion error. I would use Val(NZ(me.x2,"0")) Note that the Val() may not be necessary as NZ() returns a variant. I prefer to use the Val Or Str Functions that way I know when I look at the code Next Year What Value type I needed.

  3. #3
    EvanRosenlieb is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Location
    Fort Collins, CO
    Posts
    37
    Dear Ray

    Thanks for the help, I believe this may get me going in the right track. However there is a problem. As these are coordinate values, there is a very big difference between leaving a field blank and having a field equal to 0. If I use this Method, then 0's get entered into the Y2 and X2 fields, whereas it needs to be blank. Is there now way to do this?

    --Evan

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by EvanRosenlieb View Post
    there is a very big difference between leaving a field blank and having a field equal to 0. If I use this Method, then 0's get entered into the Y2 and X2 fields, whereas it needs to be blank. Is there now way to do this?
    Just change the code to this:
    Code:
     
    Set rsBoxEntry = WorkingDB.OpenRecordset(Me.Year, dbOpenDynaset)
     
        For i = 1 To Me.HowMany
            rsBoxEntry.AddNew
     
                    If Not IsNull(Me.Y1) Then rsBoxEntry!Y1 = Me.Y1
                    If Not IsNull(Me.Y2) Then rsBoxEntry!Y2 = Me.Y2
                    If Not IsNull(Me.X1) Then rsBoxEntry!X1 = Me.X1
                    If Not IsNull(Me.X2) Then rsBoxEntry!X2 = Me.X2

  5. #5
    EvanRosenlieb is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Location
    Fort Collins, CO
    Posts
    37
    Dear Bob,
    Thanks for you help. I had tried messing around with coding like this before, however I still get the same error message. When I try to save data with a Y1 Coordinate but not a Y2 coordinate with this code, it gives me the "Data Type Conversion" error, with the underlined segment in the following code being highlighted.

    Code:
    If Not IsNull(Me.Y1) Then rsBoxEntry!Y1 = Me.Y1
    If Not IsNull(Me.Y1) Then rsBoxEntry!Y2 = Me.Y2
    If Not IsNull(Me.X1) Then rsBoxEntry!X1 = Me.X1
    If Not IsNull(Me.X2) Then rsBoxEntry!X2 = Me.X2
    --Evan

  6. #6
    EvanRosenlieb is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Location
    Fort Collins, CO
    Posts
    37
    Bob, I figured it out, this is what the code needs to look like

    Code:
    If Not IsNull(Me.Y1) And Me.Y1 <> "" Then rsBoxEntry!Y1 = Me.Y1
    If Not IsNull(Me.Y2) And Me.Y2 <> "" Then rsBoxEntry!Y2 = Me.Y2
    If Not IsNull(Me.X1) And Me.X1 <> "" Then rsBoxEntry!X1 = Me.X1
    If Not IsNull(Me.X2) And Me.X2 <> "" Then rsBoxEntry!X2 = Me.X2
    I need to account for both null values and empty strings, as if the data entered tabs through the field without entering anything, then there will be an empty string, but if they use the mouse to click on another field without even clicking on the field, then it will be null. This covers both bases. I know you have helped me before, and you have helped me again. I appreciate it very much.

    --Evan

  7. #7
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    You could shorten it to this then:

    If Len(Me.Y1 & vbNullString) > 0 Then rsBoxEntry!Y1 = Me.Y1
    If Len(Me.Y2 & vbNullString) > 0 Then rsBoxEntry!Y2 = Me.Y2
    If Len(Me.X1 & vbNullString) > 0 Then rsBoxEntry!X1 = Me.X1
    If Len(Me.X2 & vbNullString) > 0 Then rsBoxEntry!X2 = Me.X2

    Using Len and appending an empty string will test for both nulls and empty strings.

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

Similar Threads

  1. working as a vlookup function
    By cleon in forum Queries
    Replies: 3
    Last Post: 12-16-2011, 02:51 PM
  2. Outer Join query with function not working
    By davebrads in forum Queries
    Replies: 4
    Last Post: 11-02-2011, 03:05 AM
  3. Total Calculation, nz function, not working
    By Jojojo in forum Programming
    Replies: 3
    Last Post: 10-10-2011, 02:33 PM
  4. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 AM
  5. Why isn't this working?
    By adiecidue in forum Queries
    Replies: 4
    Last Post: 04-27-2009, 10:29 AM

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