Some VBA OO help, please?

M

MikeB

Right, I'm playing around with VBA.

I want to create a class with some methods. The class will manage
producing formatted output. I will call the class several times with
fragments of an output line and a tab position for each fragment. Then
I will call the class to write all the accumulated
fragments in a single output operation.

In my Class module I have a user-defined type of linePart that has a
tabPosition and a stringFragment.

I also have a collection object OutLIne, that is a collection of
lineParts.

In my code I'm trying to create a new linePart, and then add it to the
Collection object outLine.

I get a VB error at run-time that says: Only public user defined types
defined in public object modules can be used as parameters or return
types for public procedures of class modules or as fields of public
user defined types

I tried making the user defined type Public, but that simply created a
different error.

Where am I going wrong? Thanks.

This is the calling code: PrintTest

Option Compare Database
Option Explicit

Sub x1()
Dim pr As New PrMgr
pr.setPr "Append", "TestFile.txt"
pr.Say
pr.add 1, "Hello world"

End Sub


This is my Class module: prMgr


Option Compare Database
Option Explicit

Private boolReady As Boolean
Private Type linePart
tabPos As Integer
str As String
End Type

Private outLine As Collection

Private Sub Class_Initialize()
Dim num As Integer
Set outLine = New Collection
Debug.Print "PrMgr: Initialize" & Now()
'For num = 1 To outLine.Count()
' outLine.Remove 1
' Next
End Sub

Public Sub setPr(ByVal method As String, _
ByVal fn As String)
' this is here because I couldn't find a way to initialize
' the class with parameters to the constructor.
boolReady = False
If Not fn <> "" Then
fn = "Sample_output.txt"
End If
Select Case method
Case "Append"
Open fn For Append As 1
Debug.Print "Output opened for Append."
Case "Output"
Open fn For Output As 1
Debug.Print "Output opened."
Case Else
err.Raise vbObjectError + 1, "PrMgr.Set", "Invalid Printer
options"
End Select
End Sub

Public Sub add(ByVal mTabPos As Integer, _
ByVal mStr As String)
Dim item As linePart
'item = AddLineItem(tabPos, str)
item.tabPos = mTabPos
item.str = mStr
outLine.add item
Debug.Print "There are now " & outLine.Count & " items in outline."

End Sub

Private Sub Class_Terminate()
Close 1
Set outLine = Nothing
Debug.Print "PrMgr: Terminate"
End Sub

Sub Say()
Debug.Print "Hello World"
End Sub
 
M

MikeB

Is there any other way to do this? I'm getting really frustrated. I
came to write a simple VBA routine to update one of my tables and now
I'm in the bowels of Access, struggling with stuff that really seems
arcane.

It brings to mind the old saying: "It's hard to remember that your
original objective was to drain the swamp when you are knee-deep
fighting the alligators."

And it's not that I'm clueless, I can write Java and VB.net after a
fashion. I've also invested in books, but they simply don't seem to
cover exactly the scenarios I'm trying to code.

As books I have:

- Microsoft Access VBA Programming by Michael Vine
- Wrox Access 2007 VBA by Henning, Cooper, Griffith and Stein.
 
D

Dirk Goldgar

Move the definition of linePart into a standard module, and make it Public
there. Currently it's defined in the class module. I believe the message
is telling you that it needs to be defined in a standard module.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


"MikeB" wrote in message
 
M

MikeB

Move the definition of linePart into a standard module, and make it Public
there. Currently it's defined in the class module. I believe the message
is telling you that it needs to be defined in a standard module.

Actually, I did try that. It still didn't work.

I believe that Chris O'C seems to have hit on the solution.
 
M

MikeB

You can't use a user defined type to add to a collection object in your class
but you can create another class, instantiate an object of that class and add
that object to the collection.

Is this something where VBA is different than other OO languages such
as Java?
Don't use method names or properties for the names of your variables. Item
and str shouldn't be used as variables, unless you like to hunt bugs.

Yea, I was going to change that. Always good advice.

Don't
hard code a file number. What if it's already taken? Your code bombs. Use
freefile() instead.

Thanks!, I was struggling with that and thought that for the time
being I'd simply hardcode it and then later figure out a way to assign
a file number.


Thanks so much for your help with my code.
Here's the linepart class code:

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top