Results 1 to 6 of 6
  1. #1
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    172

    Learning open recordset

    I have a form with a button 'btnOpenRecordset' with the folling code:

    Private Sub btnOpenRecordset_Click()
    Dim rs As Recordset


    Dim strLastName As String


    Set rs = CurrentDb.OpenRecordset("Select * tblPeople where fTxtFirstName=Loy")
    While Not rs.EOF
    strLastName = "Last Name: " & rs!fTxtLastName
    rs.MoveNext
    Wend

    rs.Close
    Set rs = Nothing
    End Sub

    This produces the error '3075' - Syntax error (missing operator) in query expression'*tlPeople where fTxtFirstName=Loy'

    Can you give me some help?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Where are you getting that data FROM
    Also best to qualify the recordset these days with DAO or ADO?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Need FROM keyword and apostrophe delimiters: "SELECT * FROM tblPeople WHERE fTxtFirstName='Loy'"

    Could use DLookup instead of opening a recordset.

    strLastName = Nz(DLookup("fTxtLastName", "tblPeople", "fTxtFirstName='Loy'"), "")

    But why isn't lastname already available on form along with firstname?
    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.

  4. #4
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    172
    You solved my problem regarding the query and gave me a better way of doing it. I like the dLookup method. For your question regarding the last name, this is just and example, but on my real db there could be more than one first name. Thank yoy for your held.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Then how would code know which first name should be matched to return correct last name?

    And if you want first name input to be dynamic, concatenate reference to control.

    strLastName = Nz(DLookup("fTxtLastName", "tblPeople", "fTxtFirstName='" & Me.tbFirstName & "'"), "")

    Really, this VBA should not even be necessary with good design.
    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.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    lawdy, you have been given advice and have been asked questions to help with your project. As always, it is better to provide an overview of the entire process underlying your specific question and/or provide an example of what you have and what you need. There are often many options to achieve something in Access -- but readers need to see the issue in context in order to provide more focused information.

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

Similar Threads

  1. Open Recordset
    By lamore48 in forum Access
    Replies: 15
    Last Post: 02-15-2018, 07:25 PM
  2. Open recordset
    By Praveenevg in forum Access
    Replies: 2
    Last Post: 08-18-2014, 12:21 PM
  3. Recordset Open Error
    By RayMilhon in forum Programming
    Replies: 4
    Last Post: 08-27-2013, 05:54 PM
  4. open recordset with variable SQL
    By rivereridanus in forum Queries
    Replies: 4
    Last Post: 07-27-2011, 12:58 PM
  5. ADO Recordset.Open (SQL) does nothing
    By workindan in forum Programming
    Replies: 3
    Last Post: 06-23-2010, 02:07 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