Results 1 to 6 of 6
  1. #1
    clukem is offline Novice
    Windows 11 Access 2016
    Join Date
    Sep 2024
    Posts
    8

    Executing stored procedure in access vba when a space in in the search string

    Hello

    I have the following code (see below). PhoneSearch is a stored procedure on the SQL server. I'm calling the procedure from Access vba and storing the results in an access form



    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset


    Set cnn = New ADODB.Connection


    cnn.ConnectionString = "Driver={SQL Server};Server=LAPTOP-A35N8041;Database=PhoneDB;Trusted_Connection=yes"




    cnn.Open


    Set rs = New ADODB.Recordset
    Set rs = cnn.Execute("Exec dbo.PhoneSearch " & [DeviceNumber])



    The code works well until there is a space in DeviceNumber. So, ATN102 works fine, but ATN 102 causes an error. Is there a way to get this working with a space between ATN and 102? I have quite a lot of other data in the table like this. It would take too long to remove the spaces. The procedure when executed in SQL server works fine


    Regards




    Chris

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    Try

    cnn.Execute("Exec dbo.PhoneSearch ‘“ & [DeviceNumber] & “‘“)

    I’m on my phone so using smart quotes - change them if you copy/paste



  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    Any string literal should be passed in with single quotes as a delimeter in SQL server.

    I'm quite surprised it has worked at all without them previously.
    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 ↓↓

  4. #4
    clukem is offline Novice
    Windows 11 Access 2016
    Join Date
    Sep 2024
    Posts
    8
    Quote Originally Posted by CJ_London View Post
    Try

    cnn.Execute("Exec dbo.PhoneSearch ‘“ & [DeviceNumber] & “‘“)

    I’m on my phone so using smart quotes - change them if you copy/paste


    That worked well thanks. Wonder why it didn't have problems with letters, but did with spaces?

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    Quote Originally Posted by clukem View Post
    That worked well thanks. Wonder why it didn't have problems with letters, but did with spaces?
    Without the delimiters it wouldn't know if AB CD12 was one or two parameters.

    I'm still really puzzled how it accepted AB1234 instead of the more correct 'AB1324'
    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
    clukem is offline Novice
    Windows 11 Access 2016
    Join Date
    Sep 2024
    Posts
    8
    Quote Originally Posted by Minty View Post
    Without the delimiters it wouldn't know if AB CD12 was one or two parameters.

    I'm still really puzzled how it accepted AB1234 instead of the more correct 'AB1324'

    I assumed it was because it was enclosed in square brackets. Not sure. I'm still quite new to this

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

Similar Threads

  1. Replies: 13
    Last Post: 08-14-2021, 04:53 AM
  2. Replies: 14
    Last Post: 09-12-2018, 04:12 AM
  3. Executing a stored procedure under another windows user
    By system243trd in forum Programming
    Replies: 1
    Last Post: 03-16-2016, 09:29 AM
  4. Replies: 33
    Last Post: 09-25-2015, 08:39 AM
  5. Executing Oracle Stored Procedure
    By gsurfdude in forum Programming
    Replies: 0
    Last Post: 03-01-2011, 09:23 AM

Tags for this Thread

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