Solved

creating a vba array function in Access

Posted on 2014-09-10
3
359 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

705 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