UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
2 Pages V  1 2 >  (Go to first unread post)
   Reply to this topicStart new topic
> How Do I Populate Array Using A String Data, Access 2016    
 
   
Larry Larsen
post Apr 8 2020, 02:04 PM
Post#1


UA Editor + Utterly Certified
Posts: 24,590
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi

Again just playing around with some random values, that i need to "BubbleSort" ( which I have working )..

Here is the small segment of code that I'm having a difficultly with, I have my array hard coded.

But I want that to be passed as a string or some thing that's usable..
CODE
Function fTestMySort()
    Dim arr As Variant
    Dim I As Integer
    Dim mystr As String
  
    arr = Array(499, 812, 873, 572, 636, 534, 839, 793, 798, 782, 830, 686, 618, 688, 479, 737, 597, 752, 788, 814)
    Call sortAscending(arr)
    For I = 0 To 19
        mystr = mystr & (arr(I)) & ";"
    Next I
    MsgBox mystr
End Function


Any further details required please ask..
iconfused.gif

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
theDBguy
post Apr 8 2020, 02:20 PM
Post#2


UA Moderator
Posts: 78,506
Joined: 19-June 07
From: SunnySandyEggo


Hi Larry. Not sure I understand what you mean by passing a String, but would using the Split() function work for you?

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
RJD
post Apr 8 2020, 02:21 PM
Post#3


UtterAccess VIP
Posts: 10,609
Joined: 25-October 10
From: Gulf South USA


Hi Larry: What is returned to your procedure from the sortAscending function? Is that an array? Where do you grab that? Should that now be a redefined arr?

I think better with everything in front of me, so perhaps you could post the sortAscending function ... and I could puzzle that out along with you ...

HTH
Joe

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 
Larry Larsen
post Apr 8 2020, 02:32 PM
Post#4


UA Editor + Utterly Certified
Posts: 24,590
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi

"Split" looks interesting..

Here is my code, two functions.. and my msgbox..
CODE
Function fTestMySort()
    Dim arr As Variant
    Dim I As Integer
    Dim mystr As String
  
    arr = Array(499, 812, 873, 572, 636, 534, 839, 793, 798, 782, 830, 686, 618, 688, 479, 737, 597, 752, 788, 814)
    Call sortAscending(arr)
    For I = 0 To 19
        mystr = mystr & (arr(I)) & ";"
    Next I
    MsgBox mystr
End Function

Function sortAscending(arr As Variant)
    Dim lngMin As Integer
    Dim lngMax As Integer
    Dim I As Integer
    Dim j As Integer
    Dim strTemp As String
  
    lngMin = LBound(arr)
    lngMax = UBound(arr)
    For I = lngMin To lngMax - 1
        For j = I + 1 To lngMax
            If arr(I) > arr(j) Then
                strTemp = arr(I)
                arr(I) = arr(j)
                arr(j) = strTemp
            End If
        Next j
    Next I
End Function


HTH's
thumbup.gif

Attached File(s)
Attached File  2020_04_08_20_30_04.jpg ( 19.62K )Number of downloads: 0
 

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
ADezii
post Apr 8 2020, 03:07 PM
Post#5



Posts: 3,090
Joined: 4-February 07
From: USA, Florida, Delray Beach


  1. First of all, the BubbleSort is probably the least effective of any Sorting Algorithm. It is only recommended for small Arrays that are already in a pseudo-sorted state.
  2. The above being said, the Array of Variants will be passed to the SortAscending() Sub-Routine. The Sorted Array (arr) is actually returned (not as in a Function) in the arr Argument since by Default it is passed ByRef.
  3. To now process this Sorted Array (arr) and output it's elements as Strings:
    CODE
    Dim arr As Variant
    Dim I As Integer
    Dim mystr As String
      
    arr = Array(499, 812, 873, 572, 636, 534, 839, 793, 798, 782, 830, 686, 618, 688, 479, 737, 597, 752, 788, 814)

    Call SortAscending(arr)
        
    For I = LBound(arr) To UBound(arr)
      Debug.Print CStr(arr(I))
        mystr = mystr & CStr(arr(I)) & ";"
    Next I

    Debug.Print "-------------------------------------------------------------------------------"
    Debug.Print Left$(mystr, Len(mystr) - 1)
  4. OUTPUT from Call to SortAscending(arr):
    CODE
    479
    499
    534
    572
    597
    618
    636
    686
    688
    737
    752
    782
    788
    793
    798
    812
    814
    830
    839
    873
    -------------------------------------------------------------------------------
    479;499;534;572;597;618;636;686;688;737;752;782;788;793;798;812;814;830;839;873
  5. I hope this answers all of your questions.

@Larry:
Sorry, posted this before I saw your follow-up Post and made some assumptions that obviously were not correct.
This post has been edited by ADezii: Apr 8 2020, 03:10 PM
Go to the top of the page
 
cheekybuddha
post Apr 8 2020, 05:16 PM
Post#6


UtterAccess Moderator
Posts: 13,070
Joined: 6-December 03
From: Telegraph Hill


Hi ADezii,

A good trick with arrays -> string:
CODE
' ...
For I = LBound(arr) To UBound(arr)
  Debug.Print CStr(arr(I))
    mystr = mystr & CStr(arr(I)) & ";"
Next I

Debug.Print "-------------------------------------------------------------------------------"
Debug.Print Left$(mystr, Len(mystr) - 1)

can be:
CODE
Debug.Print "-------------------------------------------------------------------------------"
Debug.Print Join(mystr, ";")


thumbup.gif

d

--------------------


Regards,

David Marten
Go to the top of the page
 
ADezii
post Apr 8 2020, 06:23 PM
Post#7



Posts: 3,090
Joined: 4-February 07
From: USA, Florida, Delray Beach


Nice David, forgot all about Join(), but shouldn't it be
CODE
Debug.Print Join(<array_name>, ";")

to return a semi-colon delimited String of elements in the Array arr() using ';' as the Delimiter?
This post has been edited by ADezii: Apr 8 2020, 06:24 PM
Go to the top of the page
 
cheekybuddha
post Apr 8 2020, 06:25 PM
Post#8


UtterAccess Moderator
Posts: 13,070
Joined: 6-December 03
From: Telegraph Hill


Yup! blush.gif

thumbup.gif

--------------------


Regards,

David Marten
Go to the top of the page
 
Larry Larsen
post Apr 9 2020, 03:25 AM
Post#9


UA Editor + Utterly Certified
Posts: 24,590
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi David & Adezii

Many thanks for the added "tricks" and will incorporate those where needed..

My orig query was how do I take a string value and pass it over to conformed format to use in those array requirements..

Do I simple pass the string as a string or some other object..??

Hopefully my image attempt can not muddy the water..LOL..

thumbup.gif
Attached File(s)
Attached File  String.jpg ( 82.21K )Number of downloads: 6
 

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
cheekybuddha
post Apr 9 2020, 05:36 AM
Post#10


UtterAccess Moderator
Posts: 13,070
Joined: 6-December 03
From: Telegraph Hill


Hi Larry,

You have two options:
1. Build an array instead of a string
2. Use Split() on strConcat and pass the resulting array to your function

1.
CODE
  Dim arrItems() As Variant, i As Integer

  ReDim arrItems(Me.lstNum.ListCount - 1)
  For i = 1 To Me.lstNum.ListCount - 1         ' <-- is there a reason you're starting from index 1 instead of 0?
    arrItems(i) = Me.lstNum.Comlumn(0, i)
  Next i

  Call fTestMySort(arrItems)


2.
CODE
  Dim strConcat As String, arrItems As Variant, varItem As Integer

  For varItem = 1 To Me.lstNum.ListCount - 1
    strConcat = strConcat & ";" & Me.lstNum.Column(0, varItem)
  Next varItem
  strConcat = Mid(strConcat, 2)
  arrItems = Split(strConcat, ";")

  Call fTestMySort(arrItems)


hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
ADezii
post Apr 9 2020, 06:58 AM
Post#11



Posts: 3,090
Joined: 4-February 07
From: USA, Florida, Delray Beach


And yet another Option, one seldom used, would be to pass a Variable number of Arguments to a Sub-Routine using the ParamArray Keyword:
  1. Sub-Routine Definition:
    CODE
    Public Sub DemoParamArray(ParamArray varValues() As Variant)
      Debug.Print Join(varValues, ";")
    End Sub
  2. Call to Sub-Routine:
    CODE
    Call DemoParamArray("499", 812, "873")
    Call DemoParamArray(499, 812, 873, 572, 636)
    Call DemoParamArray(499, 812, 873, 572, 636, 534, 839, 793, 798, 782)
    Call DemoParamArray(499, 812, 873, 572, 636, 534, 839, 793, 798, 782, 830, 686, 618, 688, 479)
    Call DemoParamArray(499, 812, 873, 572, 636, 534, 839, 793, 798, 782, 830, 686, 618, 688, 479, 737, 597, 752, 788, 814)
  3. OUTPUT:
    CODE
    499;812;873
    499;812;873;572;636
    499;812;873;572;636;534;839;793;798;782
    499;812;873;572;636;534;839;793;798;782;830;686;618;688;479
    499;812;873;572;636;534;839;793;798;782;830;686;618;688;479;737;597;752;788;814

This post has been edited by ADezii: Apr 9 2020, 07:00 AM
Go to the top of the page
 
Larry Larsen
post Apr 9 2020, 08:07 AM
Post#12


UA Editor + Utterly Certified
Posts: 24,590
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi Guy's

Many thanks for your time and guidance that got me there...

Having a number of options is so pleasing..

This got me going in the direction..
CODE
ReDim arrItems(Me.lstNum.ListCount - 1)
    For i = 0 To Me.lstNum.ListCount - 1
        arrItems(i) = Me.lstNum.Column(0, i)
    Next i


Pushing my array into the function and out again..
CODE
Function fTestMySort(vArray As Variant) As String
    Dim arr As Variant
    Dim i As Integer
    Dim mystr As String
  
    arr = vArray
    Call sortAscending(arr)
    For i = 0 To 19
        mystr = mystr & (arr(i)) & ";"
    Next i
    fTestMySort = mystr
End Function


It was nothing important just a small issue that during "lock-down" got me locked into.. compute.gif

Again many thanks..

Stay in - Stay Safe.. (we could do with an emoji spreading the message in every post..)
uarulez2.gif thumbup.gif notworthy.gif

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
cheekybuddha
post Apr 9 2020, 08:42 AM
Post#13


UtterAccess Moderator
Posts: 13,070
Joined: 6-December 03
From: Telegraph Hill


ParamArray is cool, but it's not possible to spit out the items in a listbox into a ParamArray.

The arguments have to hard-coded at Design time. One of my greatest frustrations with ParamArray, you can't feed it variable number of arguments at run-time.

(I suppose you can create a string of the function call and use Eval(), but I always shy away from that!)

thumbup.gif

d

--------------------


Regards,

David Marten
Go to the top of the page
 
ADezii
post Apr 9 2020, 09:14 AM
Post#14



Posts: 3,090
Joined: 4-February 07
From: USA, Florida, Delray Beach


You can actually process all of the Items in a Listbox then pass them to ParamArray by first building a Delimited String, then passing that String to a Sub/Function whose Argument uses the ParamArray Keyword. Why you would want to do this is another issue, but you can, and you don't need the Eval() Function. I can whip up a quick Demo if you like.
Go to the top of the page
 
cheekybuddha
post Apr 9 2020, 09:31 AM
Post#15


UtterAccess Moderator
Posts: 13,070
Joined: 6-December 03
From: Telegraph Hill


>> I can whip up a quick Demo if you like. <<

Yes please! (But just post the code, don't upload a db)

Thanks!

d

--------------------


Regards,

David Marten
Go to the top of the page
 
Larry Larsen
post Apr 9 2020, 10:09 AM
Post#16


UA Editor + Utterly Certified
Posts: 24,590
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi ADezii

Ditto...
thumbup.gif

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
ADezii
post Apr 9 2020, 10:33 AM
Post#17



Posts: 3,090
Joined: 4-February 07
From: USA, Florida, Delray Beach


  1. I hope that I am not off track on this one since I do have a tendency to go askew at times. If I am deviating, please let me know, I'l won't be insulted.
  2. The following Code Segments will:
    1. Loop thru every Row in Column 1 of a Listbox named lstDemo.
    2. Build a Delimited String of all entries in Column 1 with ';' as the Delimiter.
    3. Pass this String to a Public Sub-Routine (DemoParamArray()) that uses ParamArray as an Argument.
    4. List the Values in the ParamArray Argument and verify against the original Values in lstDemo.
  3. Entry Level Code:
    CODE
    For intCtr = 0 To Me![lstDemo].ListCount - 1
      strBuild = strBuild & Me![lstDemo].Column(0, intCtr) & ";"
    Next

    strBuild = Left$(strBuild, Len(strBuild) - 1)

    Debug.Print "strBuild: " & strBuild

    Call DemoParamArray(strBuild)
  4. Function Definition:
    CODE
    Public Sub DemoParamArray(ParamArray varValues() As Variant)
      Debug.Print "In DemoParamArray: " & Join(varValues, ";")
    End Sub
  5. OUTPUT:
    CODE
    strBuild: 30;67;31;99;31;48;32;33;34;33;36;37;38;39;40;41;101;42;77;43
    In DemoParamArray: 30;67;31;99;31;48;32;33;34;33;36;37;38;39;40;41;101;42;77;43
  6. Again, if I am off track, I do apologize.
Go to the top of the page
 
theDBguy
post Apr 9 2020, 10:57 AM
Post#18


UA Moderator
Posts: 78,506
Joined: 19-June 07
From: SunnySandyEggo


Hi ADezii. What happens if you add this line to your function?

Debug.Print UBound(varValues)

For example:
CODE
Public Sub DemoParamArray(ParamArray varValues() As Variant)
  Debug.Print "In DemoParamArray: " & Join(varValues, ";")
  Debug.Print UBound(varValues)
End Sub

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
ADezii
post Apr 9 2020, 11:24 AM
Post#19



Posts: 3,090
Joined: 4-February 07
From: USA, Florida, Delray Beach


QUOTE
What happens if you add this line to your function?

  1. Both the LBound(varValues) and the UBound(varValues) = 0
  2. varValues(LBound(varValues)) will return the Delimited ParamArray List itself
  3. Any Index other than LBound(varValues) will return:
    CODE
    Subscript out of range

This post has been edited by ADezii: Apr 9 2020, 11:26 AM
Go to the top of the page
 
theDBguy
post Apr 9 2020, 11:28 AM
Post#20


UA Moderator
Posts: 78,506
Joined: 19-June 07
From: SunnySandyEggo


So, was that the result you were expecting? Here's another modified copy of your function. See what happens when you run it.

CODE
Public Sub DemoParamArray(ParamArray varValues() As Variant)
Dim x As Long

  'Debug.Print "In DemoParamArray: " & Join(varValues, ";")
  
For x = 0 To UBound(varValues)
  Debug.Print "varValue(" & x; "): " & varValues(x)
Next

End Sub

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
2 Pages V  1 2 >


Custom Search


RSSSearch   Top   Lo-Fi    16th July 2020 - 09:12 AM