Solved

Passing integers

Posted on 2016-08-23
6
41 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

759 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

21 Experts available now in Live!

Get 1:1 Help Now