Solved

creating a vba array function in Access

Posted on 2014-09-10
3
364 Views
Last Modified: 2014-10-01
I have a field that I need to parse, there are 6 components to the field.  Each element is separated with a semi-colon. (I can, using a query, put quotes and a comma to separate the elements. (I actually did this, putting quote marks also at the beginning of the field and the end of the field, when the array called in the string, it added additional quotes at the beginning and the end))

here is the field data: "09/06/2014","21:00","24:00","67","Regular","3"

I want to create a function that will return one of the elements of the array.    here is a copy of the function (and versions thereof) I have tried and I receive the error
Run-time error '13':
type mismatch

Function ParseOtherDept(ByVal s As String, ByVal i As Integer) As String
        Dim strArry As Variant
        strArry = s
        ParseOtherDept = strArry(i)
End Function

I've also tried the following function which produced the following error
Compile error:  Can't assign to array
Function ParseField1(ByVal s As String, ByVal i As Integer) As String
    Dim WrdArray() As String
    Dim x As String
        WrdArray() = s
       x = ","
       ParseField1 = Split(WrdArray, x, i)

End Function

I can't seem to find the missing link - can someone please assist...I have a workaround but this solution would be so much cleaner and clear as to which element I'm extracting.

thanks in advance for any assist.
Regards,
0
Comment
Question by:SCMHC
  • 2
3 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 40314803
use thise codes
Function fnParseInfo(vString As Variant, idx As Integer, Optional Delimiter As String = ",") As String

   Dim myArray() As String
   myArray = Split(vString, Delimiter)

   If idx < 0 Or idx > UBound(myArray) + 1 Then

      fnParseInfo = ""

  Else

      fnParseInfo = myArray(idx - 1)

   End If

End Function

Open in new window



pass the string like this

?fnParseInfo("09/06/2014,21:00,24:00,67,Regular,3",3)

it will return  
24:00


.
0
 

Author Comment

by:SCMHC
ID: 40354545
thank you, thank you...this works perfectly just the way you coded it!!!!
Thanks again
regards,
Deby
0
 

Author Closing Comment

by:SCMHC
ID: 40354554
don't know what I was missing in any of the other functions I found, but this one is quick and easy...just copy and paste!!!
Thanks much.
Regards,
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

896 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

15 Experts available now in Live!

Get 1:1 Help Now