Type mismatch issue with array (1 Viewer)

Euler271

New member
Local time
Today, 05:40
Joined
Oct 19, 2022
Messages
12
I'm creating a string of primary keys, called PKs, and then converting that string to an array, called A(). I'm using a comma as the delimiter.
The problem is that I'm getting a "Type Mismatch" error on the line: A = Split(PKs, ","). Here's the code:

Code:
    Dim PKs As String
    Dim ListItem as Variant
    Dim A() As Long
    PKs = "("
    For Each ListItem In Me!ListBox.ItemsSelected: PKs = PKs & Me!ListBox.Column(0, ListItem) & ", ": Next ListItem
    PKs = Left(PKs, Len(PKs) - 2) & ")"
    A = Split(PKs, ",")

I have this same code in other areas of the project and it works just fine. Can anybody see what I'm doing wrong?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:40
Joined
Aug 30, 2003
Messages
36,125
I don't think you want parentheses around the string. The first item will be "(123", the last will be "789)".
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:40
Joined
Sep 21, 2011
Messages
14,301
Have you walked your code to see what PKs contains?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:40
Joined
Feb 19, 2002
Messages
43,275
Stop the code on the last line and print the value of a to the immediate window.

What are you intending to do with the value in A?
 

Josef P.

Well-known member
Local time
Today, 12:40
Joined
Feb 2, 2023
Messages
826
Last edited:

Euler271

New member
Local time
Today, 05:40
Joined
Oct 19, 2022
Messages
12
Since I set A() as Long I didn't think I needed quotes. I need the items to remain Long datatypes.

When I stopped the code and printed what was in PKs I found (23456), just one primary key which is as it should be since I only selected one item in the list box. If I select two items in the list box, PKs shows two primary keys; e.g., (23456, 78912)

What am I doing with A()? I'm looping through the array and updating records based on the primary key in the array. The user can select any number of items in the list box so the array could have many elements.
 

KitaYama

Well-known member
Local time
Today, 19:40
Joined
Jan 6, 2022
Messages
1,541
Since I set A() as Long I didn't think I needed quotes. I need the items to remain Long datatypes.

When I stopped the code and printed what was in PKs I found (23456), just one primary key which is as it should be since I only selected one item in the list box. If I select two items in the list box, PKs shows two primary keys; e.g., (23456, 78912)

What am I doing with A()? I'm looping through the array and updating records based on the primary key in the array. The user can select any number of items in the list box so the array could have many elements.
I deleted my post because I thought Joseph's response is enough.
You don't need "(" and ")"
And
You have to change your data type

Dim A As Variant
Or
Dim A() As String

If you need Long data type you can use CLng to convert the string or variant to Long
CLng(A(0))

Code:
    Dim PKs As String
    Dim ListItem as Variant
    Dim A As Variant    ' Or Dim A() AS String
    For Each ListItem In Me!ListBox.ItemsSelected: PKs = PKs & Me!ListBox.Column(0, ListItem) & ",": Next ListItem
    PKs = Left(PKs, Len(PKs) - 1)
    A = Split(PKs, ",")

I Also deleted the space after ", "
It saves me using Trim if the datatype is string.
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 03:40
Joined
Oct 29, 2018
Messages
21,473
What am I doing with A()? I'm looping through the array and updating records based on the primary key in the array. The user can select any number of items in the list box so the array could have many elements.
If you're planning to do something like:
Code:
For x = 0 To UBound(A)
    CurrentDb.Execute "UPDATE TableName SET FieldName='SomeValue' WHERE PK=" & A(x), dbFailOnError
Next
Then, you should also be able to do the following:
Code:
For Each ListItem In Me.ListBox.ItemsSelected
    CurrentDb.Execute "UPDATE TableName SET FieldName='SomeValue' WHERE PK=" & Me.ListBox.ItemData(ListItem)
Next
Just a thought...
 

Josef P.

Well-known member
Local time
Today, 12:40
Joined
Feb 2, 2023
Messages
826
I need the items to remain Long datatypes.
If you want to use a long array, you cannot use split.

=> Write values directly into the array:
Code:
Dim SelectedItemCount As Long
Dim ListItem  As Variant
Dim PkArray() As Long
Dim i As Long

With Me!ListBox  ' <-- bad name

   SelectedItemCount = .ItemsSelected.Count
   If SelectedItemCount = 0 Then
      ' Err.Raise ...
      ' or
      'MsgBox "..."
      Exit Sub/Function
   End If

   ReDim PkArray(SelectedItemCount - 1)

   For Each ListItem In .ItemsSelected
      PkArray(i) = .Column(0, ListItem)
      i = i + 1
   Next

End With

or as reusable code:
Code:
   Dim PkArray() As Long
   If Not FillArrayFromItemsSelected(PkArray, Me.ListBox, 0) Then
      ' Err.Raise ...
      ' or
      'MsgBox "..."
      Exit Sub/Function
   End If
   ...

'###################
Public Function FillArrayFromItemsSelected(ByRef Array2Fill As Variant, ByVal SelectListBox As ListBox, ByVal ColumnIndex As Long) As Boolean

   Dim SelectedItemCount As Long
   Dim ListItem  As Variant
   Dim i As Long

   With SelectListBox

      SelectedItemCount = .ItemsSelected.Count
      If SelectedItemCount = 0 Then
         FillArrayFromItemsSelected = False
         Exit Function
      End If

      ReDim Array2Fill(SelectedItemCount - 1)
   
      For Each ListItem In .ItemsSelected
         Array2Fill(i) = .Column(ColumnIndex, ListItem)
         i = i + 1
      Next

   End With

   FillArrayFromItemsSelected = True

End Function
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 11:40
Joined
Sep 21, 2011
Messages
14,301

Euler271

New member
Local time
Today, 05:40
Joined
Oct 19, 2022
Messages
12
Thanks to everyone for taking the time to respond.
I ended up created the string and array at the same time:
Code:
    PKs = "("
    ReDim A(Me!MyListBox.ItemsSelected.count - 1)
    For Each ListItem In Me!MyListBox.ItemsSelected
        PKs = PKs & Me!MyListBox.Column(0, ListItem) & ", "
        A(i) = Me!MyListBox.Column(0, ListItem)
        i = i + 1
    Next ListItem
    PKs = Left(PKs, Len(PKs) - 2) & ")"
 

Users who are viewing this thread

Top Bottom