Results 1 to 9 of 9
  1. #1
    Scyclone is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    51

    VBA Record Set - Read Only

    Hello Everyone,

    I am having no issue retrieving the record set, however, I am unable to get the record set to display and lock as a read-only table. I am essentially creating a lookup function where someone can enter in a name, find the customer information associated with it and use it for referencing, I do not want them to be able to change any of this information though.

    ======

    Function SearchName()
    Dim ivalue As String


    Dim strSQL As String
    Dim dbs As Database
    Dim qdf As QueryDef
    Set dbs = CurrentDb()

    ivalue = InputBox("Please enter the name or portion of the name that you wish to search for.", "Customer Name Lookup")
    strSQL = "SELECT * FROM CustList WHERE CustName LIKE '*" & ivalue & "*'"
    rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
    With dbs
    Set qdf = .CreateQueryDef("Search Results", strSQL)
    DoCmd.OpenQuery "Search Results", acViewNormal, acReadOnly
    .QueryDefs.Delete "Search Results"
    End With
    dbs.Close
    qdf.Close

    End Function
    =======

    Thoughts?

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Go into the saved query and change the recordset type from Dynaset to Snapshot. You do that by going into the QBE grid of the query and click on the gray part where the table is located in the diagram and in the query properties you should see Recordset Type.

  3. #3
    Scyclone is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    51
    Hmm not sure I follow you.

    When you say saved query, are you referring to the table "CustList" or the query thats created and stored as the variable strSQL?

    I am not sure where to find the portion that you are refering to.

    =======
    Edit

    Ahh... I think I understand what you mean now, though this would only apply if the query already exists in Access and isn't created through VBA, correct?

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Sorry, I didn't see that you were using a temporary query.

    I would just create a base query as a saved query. So instead of using CreateQueryDef you would just open the saved query and modify it and then open it.

  5. #5
    Scyclone is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    51
    Hmm, so to call it through VBA, would I just use the

    Docmd.OpenQuery "QueryName"

    Also, when making a base query, how do I set an input box as a parameter *refer to the code above*?

  6. #6
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    You would still use your querydef object code to change the SQL including the parameter values just like you have. Just delete this line:

    .QueryDefs.Delete "Search Results"

    and revise this line:
    Set qdf = .CreateQueryDef("Search Results", strSQL)

    to this

    Set qdf = .QueryDefs("Search Results")


    and you should be good to go.

  7. #7
    Scyclone is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    51
    Okee, so if I understand correctly.

    SQL Query is just Select * From CustList

    Then I just use this function?

    ===

    Function SearchName()
    Dim ivalue As String
    Dim strSQL As String
    Dim dbs As Database
    Dim qdf As QueryDef
    Set dbs = CurrentDb()

    ivalue = InputBox("Please enter the name or portion of the name that you wish to search for.", "Customer Name Lookup")
    strSQL = "SELECT * FROM CustList WHERE CustName LIKE '*" & ivalue & "*'"
    rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
    With dbs
    Set qdf = .QueryDefs("Search Results")
    DoCmd.OpenQuery "Search Results", acViewNormal, acReadOnly
    End With
    dbs.Close
    qdf.Close

    End Function
    ====

  8. #8
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Well, with a slight modification from what you have.

    Code:
    Function SearchName()
    Dim ivalue As String
    Dim strSQL As String
    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    
    Set dbs = CurrentDb()
    
    ivalue = InputBox("Please enter the name or portion of the name that you wish to search for.", "Customer Name Lookup")
    strSQL = "SELECT * FROM CustList WHERE CustName LIKE '*" & ivalue & "*'"
    
    With dbs
    Set qdf = .QueryDefs("Search Results")
     
    qdf.SQL = strSQL
    qdf.Close
    
    DoCmd.OpenQuery "Search Results", acViewNormal, acReadOnly
    
    End With
    
    Set dbs = Nothing
    
    End Function
    And I have no idea why yhou were using the recordset but I deleted it completely because from what you have there, it isn't needed at all. You also don't need to close dbs because it won't close it anyway as it is the current database and it needs to remain open, so it is moot.

  9. #9
    Scyclone is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    51
    That did the trick, awesome. Thank you

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

Similar Threads

  1. MDB in Read Only, No .ldb, What to do
    By schwimms in forum Access
    Replies: 11
    Last Post: 02-25-2015, 03:12 PM
  2. Button on a Form to make record read only
    By billgyrotech in forum Forms
    Replies: 8
    Last Post: 08-08-2011, 03:28 PM
  3. Allow Read or Update dep on record
    By AndreT in forum Forms
    Replies: 2
    Last Post: 07-31-2011, 09:46 PM
  4. Who read this book?
    By masip2004 in forum General Chat
    Replies: 0
    Last Post: 02-25-2011, 12:01 AM
  5. Replies: 2
    Last Post: 10-19-2006, 04:37 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