Results 1 to 2 of 2
  1. #1
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34

    parametrized Queries

    I am looking for a good tutorial or sample code on parametrized queries in VBA.

    I am creating a query based on results of another query... but one of the strings contains a single quote and destroys my second query and it cannot execute properly.



    Code:
    Private Sub getUserLeaderInfo(ByVal record As Long, ByVal rs As DAO.Recordset)
        With rs
            queryStr = "INSERT INTO t_user_ldr_info VALUES(" & _
                record & ", " & _
                "'" & .Fields("Controlp") & "', " & _
                "'" & .Fields("Test") & "', " & _
                "'" & .Fields("LoginID") & "', " & _
                "'" & .Fields("DTG_Submit") & "', " & _
                "'" & .Fields("PIN") & "', " & _
                "'" & .Fields("SystemID") & "', " & _
                "'" & .Fields("Role") & "', " & _
                "'" & .Fields("Mission") & "', " & _
                "'" & .Fields("Location") & "', " & _
                "'" & .Fields("Other") & "', " & _
                "'" & .Fields("Terrain") & "')"
        End With
                        
        DoCmd.SetWarnings False
        DoCmd.RunSQL (queryStr)
    
    End Sub
    How can I turn this into a parametrized query?
    Yes, I've googled it... but I don't get much out of the crap that comes up in the results.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,973
    Use replace function to double the apostrophe so code will recognize the literal character.

    Replace(fieldname, "'", "''")

    Replace(.Fields("Other"), "'", "''")

    This syntax should work also:

    Replace(!Other, "'", "''")

    Are any of those fields actually number type? If so, don't use apostrophe delimiters. And date/time fields use # delimeter.
    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. Using Sub Queries?
    By djclntn in forum Queries
    Replies: 4
    Last Post: 04-05-2012, 08:58 AM
  2. Help with SQL queries
    By gary223 in forum Queries
    Replies: 7
    Last Post: 04-29-2011, 09:43 PM
  3. Need Help with Queries (I think)
    By Cath_from_Canada in forum Access
    Replies: 2
    Last Post: 03-14-2011, 08:34 AM
  4. Sum Queries
    By Lilsug in forum Access
    Replies: 4
    Last Post: 12-17-2010, 08:45 AM
  5. need help with queries
    By gromit95 in forum Queries
    Replies: 1
    Last Post: 02-06-2009, 06:50 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