Solved

Passing integers

Posted on 2016-08-23
6
45 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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 143

Expert Comment

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

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

733 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