Solved

creating a vba array function in Access

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

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

12 Experts available now in Live!

Get 1:1 Help Now