Solved

creating a vba array function in Access

Posted on 2014-09-10
3
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Make the most of your online learning experience.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

615 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