Results 1 to 8 of 8
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284

    Data Type Conversion Error when trying to Set Rs = Qdf.OpenRecordset()

    Howdy...



    I am currently using the following code to try and create and run a Qdf but I'm getting a 3421: Data Type Conversion at the: Set Rs = Qdf.OpenRecordset("StrQry1")
    Code:
    Dim Qdf As DAO.QueryDef
    Dim Dbs As DAO.Database
    Set Dbs = CurrentDb
    Dim Rs As DAO.RecordsetDbs.QueryDefs.Delete "StrQry1"
    StrQry1 = "SELECT * From tri.TktEmailTmp"
    Set Qdf = Dbs.CreateQueryDef("StrQry1")
    Qdf.Connect = strConnection
    Qdf.SQL = StrQry1
    Qdf.ReturnsRecords = False
    
    Set Rs = Qdf.OpenRecordset("StrQry1")   <----- Error is occurring on this line
    
        If Not (Rs.BOF And Rs.EOF) Then
            Do While Not Rs.EOF
                lCnt = lCnt + 1
                ReDim Preserve EmailBdy(1 To lCnt)
                  TblRow(1) = Rs("EmpID")
                    TblRow(2) = Rs("EmpName")
                    TblRow(3) = Rs("TempAgency")
                  strBodyPass = strBodyPass & "<tr><td>" & Join(TblRow, "</td><td>") & "</td></tr>"
                Rs.MoveNext
            Loop
        End If
    I've researched and tried as many things as I can find and think of

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,159
    You don't need the quotes - you have set StrQry1 as a string variable , so use the variable.
    Code:
    Set Rs = Qdf.OpenRecordset(StrQry1)
    At the moment you are trying to open a recordset using a sql statement of "StrQry1"
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Hey Minty ~

    Thanks for chiming in on this - I removed the quotes - unfortunately, same error...

    I also removed the quotes surrounding StrQry in the Set Qdf = Dbs.CreateQueryDef(StrQry1) but that only produces a different error.

  4. #4
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    I changed:
    Code:
    Set Rs = Qdf.OpenRecordset(StrQry1)
    To:
    Code:
    Set Rs = Qdf.OpenRecordset
    And the code flew right through and produced the desired results

    So it would 'appear' removing the StrQry1 reference altogether was the answer?

    Does this make sense that removing StrQry1 would be the solution?

    Thanks
    Last edited by RunTime91; 10-29-2018 at 08:54 AM. Reason: Pose a question

  5. #5
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,159
    Doh

    Yes it makes perfect sense. You set the query def to your SQL string earlier in the code, so of course it works.
    I was confusing myself with the methods used, and sort of blindsided myself by ignoring the Qdf. prefix.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    The string variable is not need at all, especially with such a short query statement.

    Set Qdf = Dbs.CreateQueryDef("SELECT * FROM tri.TktEmailTmp")

    If you were building a long query statement with multi-line concatenation, would probably want a variable.

    I have never used QueryDefs just to establish a recordset object. Simplified:

    Dim Dbs As DAO.Database
    Dim Rs As DAO.Recordset
    Set Dbs = CurrentDb
    Set Rs = Dbs.OpenRecordset("SELECT * FROM tri.TktEmailTmp")

    Whether or not the Dbs object variable is needed can be another discussion - some will argue strongly for it:

    Set Rs = CurrentDb.OpenRecordset("SELECT * FROM tri.TktEmailTmp")

    Line in posted code: Dim Rs As DAO.RecordsetDbs.QueryDefs.Delete "StrQry1", should be split to 2 lines. Have to be careful, sometimes forum post doesn't get a line feed when pasting code. Why are you trying to delete a QueryDef?

    Also wonder what is advantage of using an array?
    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
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Hey June...

    I'm new to QDefs as well but my understanding is, it is good to delete your QDef's to prevent errors (I also use error traps) and to free up memory.
    As for the Array - I'm not crazy about array's but the code works so I'm kinda of leaving it alone for now - if you have a suggestion - I'm listening
    And finally, declaring the Dbs object - yeah, I think that is just habit...

    Thanks June

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I am confused by your original code. You ReDim array EmailBody, yet the array filled is TblRow. Code does not show either declared. I have used QueryDefs only when I wanted to create or modify an Access query object, never just to open a recordset. What is your backend - what is 'tri'?)

    Join() is new to me but I have used Array() function:
    Code:
    Dim Dbs As DAO.Database
    Dim Rs As DAO.Recordset
    Set Dbs = CurrentDb
    Set Rs = Dbs.OpenRecordset("SELECT * FROM tri.TktEmailTmp")
    
        If Not (Rs.BOF And Rs.EOF) Then
            Do While Not Rs.EOF
                  strBodyPass = strBodyPass & "<tr><td>" & Join(Array(Rs!EmpID, Rs!EmpName, Rs!TempAgency), "</td><td>") & "</td></tr>"
                  Rs.MoveNext
            Loop
        End If
    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.

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

Similar Threads

  1. Data Type Conversion Error on OpenRecordset
    By nightowl128 in forum Programming
    Replies: 10
    Last Post: 03-17-2017, 06:00 PM
  2. Data Type Conversion Error - WHY?
    By Datamulcher in forum Modules
    Replies: 2
    Last Post: 03-13-2017, 06:00 PM
  3. Replies: 7
    Last Post: 07-24-2013, 02:01 PM
  4. Type conversion error
    By corymw in forum Access
    Replies: 1
    Last Post: 07-25-2012, 11:55 AM
  5. Data Type Conversion Error
    By graviz in forum Forms
    Replies: 7
    Last Post: 06-04-2012, 11:34 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