Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access VBA written inside form module, need to access Array from there in sub in a Module 2

Status
Not open for further replies.

03Explorer

Technical User
Sep 13, 2005
304
0
0
US
Using MS Access 365, I have an array established in a standard FORM subroutine.

I am trying to use a MODULE Sub that utilizes the established Array.

I have tried changing the type of FORM SUB to Public. No luck. I also tried changing the DIM statement in the FORM SUB to Public... again, no luck. I have the new Module SUBroutine set to Public... again, no luck.

What am I missing?
 
Just an example:

Code:
Option Explicit

Public aryMyArray(5) As String

Public Sub MyPublicSub()

aryMyArray(0) = "Hello"
aryMyArray(1) = "User"
aryMyArray(2) = "Explorer"
aryMyArray(3) = "from"
aryMyArray(4) = "TT"

End Sub

Code:
Option Explicit

Private Sub cmdOK_Click()
Dim i As Integer

Call MyPublicSub()

For i = LBound(aryMyArray) To UBound(aryMyArray)
    Debug.Print aryMyArray(I)
Next i

End Sub


---- Andy

There is a great need for a sarcasm font.
 
So Andy, you are building and populating the array in a Module Sub; then calling it from the Form Sub?

Any way to go other way around? Form Sub --> Module Sub?
 
It would probably help if you would describe what you want to do in plain English, no code.

or this example:

Code:
Option Explicit

Public Sub ABCD()

Call Form1.DoSomthing("Do what you want")

End Sub

Code:
Option Explicit

Public Sub DoSomthing(ByRef strTemp As String)

MsgBox strTemp

End Sub

Private Sub cmdOK_Click()

Call ABCD

End Sub

---- Andy

There is a great need for a sarcasm font.
 
[Code FORM:Sub]
Public Sub bthYes_Click()
Dim AssociateArray(8, 13)
AssociateArray(1,0) = "something"
AssociateArray(1,1) = "something different"
AssociateArray(1,2) = "different"
...
AssociateArray(1,12) = "last one"

Call ExcelBehavorial

[/Code]

[Code Module2:Sub]
Public Sub ExcelBehavorial()
For i = 0 To 12
Debug.Print "Associate: " & AssociateArray(1, i)
Next
End Sub
[/Code]
 
Well, try this:

Code:
Option Explicit

Private Sub bthYes_Click()
Dim AssociateArray(8, 13) As String

AssociateArray(1, 0) = "something"
AssociateArray(1, 1) = "something different"
AssociateArray(1, 2) = "different"
'...
AssociateArray(1, 12) = "last one"

Call ExcelBehavorial(AssociateArray)
    
End Sub

Code:
Option Explicit

Public Sub ExcelBehavorial(ByRef ary() As String)
Dim i As Integer

For i = LBound(ary) To UBound(ary)
    Debug.Print "Associate: " & ary(1, i)
Next
    
End Sub


---- Andy

There is a great need for a sarcasm font.
 
That didn't work... errors out with

[Code Compile_Error:]Type mismatch: array or user-defined type expected[/code]
 
On which line of code?


---- Andy

There is a great need for a sarcasm font.
 
on the Form:Sub (calling the Module:Sub)

Code:
Call ExcelBehavorial(AssociateArray)
 
Can you simply declae array in the module??
Code:
Public Sub bthYes_Click()
    AssociateArray(1,0) = "something"
    AssociateArray(1,1) = "something different"
    AssociateArray(1,2) = "different"
    ...
    AssociateArray(1,12) = "last one"

    Call ExcelBehavorial
and:
Code:
Public AssociateArray(8, 13) 
Public Sub ExcelBehavorial()
    For i = 0 To 12
      Debug.Print "Associate: " & AssociateArray(1, i)
    Next
End Sub

BTW, for me Andy's code works perfectly.

combo
 
Thanks Combo, The call (without an argument/array name) got me past that point. I replicated your code for Module2:sub like this:

[CODE Module2:sub]Public AssociateArray(8, 13)
Public Sub ExcelBehavorial()
Dim i As Integer
For i = 0 To 12
Debug.Print "Associate: " & AssociateArray(1, i)
Next
End Sub[/code]

System errors on the line <debug.print. "Run-time error 13: Type mismatch
 
Okay I am working on the point that when I am defining the Array (as I noted above in my original code share) I didn't identify the array "AS STRING"

by adding that, I found some code that needs to be updated to capture the null values.

Once I have updated my code, I'll post on the status. :) Thanks guys!!
 
BINGO!!! Andy's code did work... I needed to define the Array "AS STRING".

Thanks guys!! LOVE IT!!

Code:
Private Sub bthYes_Click()
Dim AssociateArray(8, 13) As String

AssociateArray(1, 0) = "something"
AssociateArray(1, 1) = "something different"
AssociateArray(1, 2) = "different"
'...
AssociateArray(1, 12) = "last one"

Call ExcelBehavorial(AssociateArray)
    
End Sub

Code:
Public Sub ExcelBehavorial(ByRef ary() As String)
    Dim i As Integer
    
    For i = LBound(ary) To UBound(ary)
      Debug.Print "Associate: " & ary(1, i)
    Next
    
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top