Solved

Passing integers

Posted on 2016-08-23
6
42 Views
Last Modified: 2016-09-12
MS Access 2003 sending list of record identifiers to be process in SQL 2008

''===== gather all selected Table_IDs a list box
    Dim ptr As Variant
    Dim tbl_IDs As Variant
    tbl_IDs = "("
    For Each ptr In Me.ActiveTimes.ItemsSelected
        tbl_IDs = tbl_IDs & Me.ActiveTimes.ItemData(ptr) & ","
    Next
    tbl_IDs = Left(TimeIDs, Len(TimeIDs) - 1) & ")"
looks like  ('139415, 139412' )

Conversion failed when converting the varchar value '139415, 139412' to data type int.

I've tried many ways of formatting the argument but it always throws an error
0
Comment
Question by:Leon Sivertson
  • 2
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41767551
>tbl_IDs & Me.ActiveTimes.ItemData(ptr) & ","
Just like you can't compare apples to bananas, you can't take an integer value and concatenate it with a string such as ",".  The way around this is to convert the integer to a string.  Try this..

>tbl_IDs & CStr(Me.ActiveTimes.ItemData(ptr)) & ","
0
 
LVL 2

Expert Comment

by:Antonio Salva Ripoll
ID: 41767845
Hi @Leon.

The first thing you must have in mind is the integers range. The MSAccess integers goes from -32768 to 32767.

If you try to pass bigger values you must use the Long (integer) data type. Also, instead of declaring the variables as Variants, is preferable to use defined data types.

Try this code:

Dim ptr As Variant
    Dim tbl_IDs As String
        
    For Each ptr In Me.ActiveTimes.ItemsSelected
        tbl_IDs = tbl_IDs & ", " & Me.ActiveTimes.ItemData(ptr)
    Next
    
    tbl_IDs = Trim(Mid(tbl_IDs, 2))
    
    tbl_IDs = "(" & tbl_IDs & ")"

Open in new window


If you want to include an apostrophe at the beginning and end of the string, change the last line in the code with this one:

    'Commented to add apostrophes
    'tbl_IDs = "(" & tbl_IDs & ")"
    tbl_IDs = "('" & tbl_IDs & "')"

Open in new window


Best regards.

Antonio (Barcelona, Spain)
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points (awarded by participants)
ID: 41768380
Antonio's code shows the "correction" in this part of the code.

the explanations is that:
looks like  ('139415, 139412' )

is indeed wrong, as sql server will indeed take this:
'139415, 139412'

as 1 string, and not as 2 distinct numbers

more information/explanation about that kind of "issues" is described in this article:
https://www.experts-exchange.com/articles/1536/Delimited-list-as-parameter-what-are-the-options.html
0
 
LVL 2

Accepted Solution

by:
Antonio Salva Ripoll earned 250 total points (awarded by participants)
ID: 41769362
Hi all.

@Guy, I posted my comment based on the results shown by Leon.
In SQL the values are passed in the same way as in Access.

Really, if I want to pass all values as strings, my code would be like this one:
Dim ptr As Variant
    Dim tbl_IDs As String
        
    For Each ptr In Me.ActiveTimes.ItemsSelected
        tbl_IDs = tbl_IDs & ", '" & Me.ActiveTimes.ItemData(ptr) & "'"
    Next
    
    tbl_IDs = Trim(Mid(tbl_IDs, 2))
    
    tbl_IDs = "(" & tbl_IDs & ")"

Open in new window

The result would be  ('139415', '139412' )

And if I want to pass all values as numbres, the code would be slightly different:
Dim ptr As Variant
    Dim tbl_IDs As String
        
    For Each ptr In Me.ActiveTimes.ItemsSelected
        tbl_IDs = tbl_IDs & ", " & Me.ActiveTimes.ItemData(ptr)
    Next
    
    tbl_IDs = Trim(Mid(tbl_IDs, 2))
    
    tbl_IDs = "(" & tbl_IDs & ")"

Open in new window

In this case, the result would be (139415, 139412 ), wich is also admissible by SQL Server.

Best regards.

Antonio (Barcelona, Spain)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41769763
Antonio, I did not disagree with you at all :)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now