Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing integers

Posted on 2016-08-23
6
Medium Priority
?
49 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 66

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 1000 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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

636 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