Results 1 to 5 of 5
  1. #1
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194

    recordset value failing on null variable

    Hey pplz!



    WOW ive been writing some crazy code today and been very successful up until now!


    In basic terms:
    I have a table [customers]
    and i have [firstname] [lastname] and [address]

    im using this fields as variables in a dao.recordset as 'lookup' variables in a query.

    My code runs 100% EXCEPT when one of the fields is null...

    So have:

    Code:
    Dim Fname As String
    Dim Lname As String
    Dim add As string
    
    dupcust.MoveFirst
    
    Do Until dupcust.EOF
    
    
    Fname = dupcust(1)
    Lname = dupcust(2)
    
    
    If IsNull(dupcust(3)) Then
    Set dupsingle = CurrentDb.OpenRecordset("SELECT * FROM Customers_Date_Sub WHERE Surname ='" & Lname & "' and Firstname = '" & Fname & "'")
    Else
    add = dupcust(3)
    
    
    Set dupsingle = CurrentDb.OpenRecordset("SELECT * FROM Customers_Date_Sub WHERE Surname ='" & Lname & "' and Firstname = '" & Fname & "' and Address1 = '" & add & "'")
    End If
    
    
    etc etc

    ...10 mins later

    I have changed dim add as VARIANT and this has solved the issue it seems.

    I am now having issues with lastnames like O'Brien

    I read somewhere that you can change the ' to " and it solves the issue... but in my case it seems to not work. it doesnt Error, it just runs through the loop and ends as the dupsingle seems to have no records....



    So if anyone coud help me on the ' issue and maybe confirm that if im using a dao.recordset and assigning MYVAR = RST(0) i MUST have myvar as a variant because if i do a string and then a record has a null value it will always fail?

    Thanks for helping!!

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    to solve the O'Brien issue you need to replace the ' with two so

    ....Surname ='" & replace(Lname,"'","''") & "'....

    with regards nulls, look at the nz function

    add=nz(dupcast(3),"")

    Note that add is a reserved word, so using it can create unexpected errors

  3. #3
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194
    Amazing thanks Ajax, it's midnight but I'll try it out tm!!
    And I bet the add thing was causing an issue!


    I'll do nz and also change to fadd and get back tm

  4. #4
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194
    hmmm it works for the ' issue but im having issues with the nz still.

    Works for everything except this one field which:

    Dim subid as integer

    subid = nz(dupsingle(5),"")

    Both tables have suburbID as a number

    this ends up with a "type mismatch" error....

    ideas?

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    you are trying to assign a text value to a numeric variable

    try

    subid = nz(dupsingle(5),0)

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

Similar Threads

  1. ADO recordset if null/0
    By Ruegen in forum Programming
    Replies: 3
    Last Post: 11-24-2013, 04:37 PM
  2. Replies: 3
    Last Post: 11-13-2013, 08:56 AM
  3. open recordset with variable SQL
    By rivereridanus in forum Queries
    Replies: 4
    Last Post: 07-27-2011, 12:58 PM
  4. Replies: 1
    Last Post: 07-19-2011, 05:45 PM
  5. Replies: 3
    Last Post: 02-16-2010, 10:43 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