Results 1 to 9 of 9
  1. #1
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30

    using SQL in VBA to retrieve data


    Afternoon,

    So im looking for a bit of help.

    i want to retrieve some data from a table i have pieced together a bit of code but get an error to few parameters.

    Code:
    Dim db As dao.Database
       Dim Lrs As dao.Recordset
       Dim LSQL As String
       Dim Lname As String
       
       'Open connection to current Access database
       Set db = CurrentDb()
       
       'Create SQL statement to retrieve value from contacts table
       LSQL = "SELECT Contacts.Cn " & _
                "FROM Contacts " & _
                "WHERE (((Contacts.Cc)=[Forms]![frmClaims]![frmClaims_ThirdParty]![thirdPartyCc]))"
       Debug.Print LSQL
       Set Lrs = db.OpenRecordset(LSQL)
       
       'Retrieve value if data is found
       If Lrs.EOF = False Then
          Lname = Lrs("cn")
       Else
          Lname = "Not found"
       End If
       
       Lrs.Close
       Set Lrs = Nothing
       
       getgst = Lname
       
       Debug.Print getgst
    any help or pointing to the right direction would be appreciated.

    when i paste the SQL into a query it works fine

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The form reference needs to be concatenated into the string, adding delimiters if a text data type.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Quote Originally Posted by pbaldy View Post
    The form reference needs to be concatenated into the string, adding delimiters if a text data type.
    Thank you for the quick response.

    sorry i am new at this have only used the SELECT a couple of times.

    what do you mean by this?

    Regards

    Sorry have now done that thanks, i just needed to think about what you was saying rather than just post back.

    regards
    Last edited by sovereign; 08-24-2015 at 09:19 AM. Reason: now understand

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If the data type is numeric:

    Code:
    "WHERE Contacts.Cc= " & [Forms]![frmClaims]![frmClaims_ThirdParty]![thirdPartyCc]
    if text

    Code:
    "WHERE Contacts.Cc= '" & [Forms]![frmClaims]![frmClaims_ThirdParty]![thirdPartyCc] & "'"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    If frmClaims_ThirdParty is a sub-form on frmClaims, your syntax is not correct - you cannot refer to a sub-form by its name. You have to use the name of the sub-form control on the main form (i.e. the control which contains the sub-form). It would look something like this:

    [Forms]![frmClaims]![subform_container].form![thirdPartyCc]

    Replace subform_container with the actual name of your control. Notice the additional bit, .form.

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by sovereign View Post
    Sorry have now done that thanks, i just needed to think about what you was saying rather than just post back.

    regards
    Just noticed this. Does this mean you have it working now?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Quote Originally Posted by pbaldy View Post
    Just noticed this. Does this mean you have it working now?
    yeah i did get it working perfectly!

    Thank you!

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Quote Originally Posted by pbaldy View Post
    Happy to help!
    now you mention it i have just started a new thread you could lend your knowledge too

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

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2015, 02:00 PM
  2. Replies: 5
    Last Post: 02-21-2013, 02:34 PM
  3. Submit and retrieve data in a form
    By Bertrand82 in forum Import/Export Data
    Replies: 1
    Last Post: 10-05-2012, 11:44 AM
  4. Retrieve Data Too Slow
    By BGF in forum Programming
    Replies: 8
    Last Post: 09-08-2010, 04:28 PM
  5. Unable to retrieve data on Comboboxes
    By ditogui in forum Forms
    Replies: 8
    Last Post: 12-29-2009, 10:26 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