• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 50
  • Last Modified:

Passing integers

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
Leon Sivertson
Asked:
Leon Sivertson
  • 2
  • 2
2 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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
 
Antonio Salva RipollCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Antonio Salva RipollCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Antonio, I did not disagree with you at all :)
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now