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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class module to save and manage BOOKMARKS for forms 1

Status
Not open for further replies.

cloverdog

Technical User
Mar 4, 2008
41
GB
Hello. I am new to classes and have not used bookmarks much but I have use for saving a bookmark and a string to a 2D array, used as a ring buffer. I have the code working in some forms but use it so much it would easier if I could develop it into a class.

The problem I face is that in the class version I want to pass a bookmark and a string to the class as elements of a 2D array. A form can then store and later retrieve these details from the class as required.

Works great in the form version, which does not use a class to manage the ring buffer, but I cannot get the bookmark and string to be accepted by the class version when passing them over from the form.

The property Let is as follows:

Public Property Let SaveToRingBuffer(inBookmark As BOOKMARK, inStrCustName As String)
SaveBookmarkToRingBuffer inBookmark, inStrCustName
End Property

I am trying to pass these to the class with the following line of code in the form addressing the class:

clsRingBuff.SaveToRingBuffer Me.BOOKMARK, "FredSmith"

I face 2 problems first the intellisense only shows the first parameter (ie BOOKMARK).

Secondly if I rmove the second string to get the bookmark working I get an “invalid use of property” error.

I am instantiating the class in the FormOpen subroutine with
Set clsRingBuff = New clsRingBuffer

The declaration of the class is at the top of the form as:
Private clsRingBuff As clsRingBuffer

Any help would be greatly appreciated.

Many thanks
 
You are getting [blue]"invalid use of property"[/blue] because the correct form of a property is
Code:
Public Property Let myProperty(PropertyValue As ...)

Only one argument (the value) is allowed so you cannot have more than one argument in a Property Let. If you change it to a Sub (i.e. a Method in Class terminology) then it should work.

Code:
Public Sub SaveToRingBuffer(inBookmark As BOOKMARK, inStrCustName As String)
 
I face 2 problems first the intellisense only shows the first parameter (ie BOOKMARK)"

I have never seen this before and your instantiation sounds correct. There is likely something else going on. My first guess is that you have a duplicate name, procdure, or module somewhere.
1) Ensure you have "Option Explicit" on all modules
2) I would Compile and see if there is any identified problems.
3) Then I would use the find. And check
SaveToRingBuffer
clsRingBuff
clsRingBuffer

Make sure you did not mistakenly give two procedures the same name, procedure and object the same name, double procedure, etc.
 
Golom,
Good catch I do not know what I was thinking.

Cloverdog,
Can you explain the purpose of what you are doing? Saving bookmarks can be very problematic since every instantiation of a recordset creates a unique set of bookmarks and these are unique every time you requery the data. You are much better off saving the primary key.
 
Thank you for your quick reply. I understand the single argument and wonder if a custom data type will get round that.

More pressing is that I cannot see where I have breached the correct form of the property?

MAny thanks
 
I cannot see where I have breached the correct form of the property

Here!
Code:
Public Property Let SaveToRingBuffer( _
inBookmark As BOOKMARK, _       [blue]<-- First Argument[/blue]
inStrCustName As String)        [red]<-- Second Argument ILLEGAL![/red]
 
"I understand the single argument and wonder if a custom data type will get round that."

No do what Golom said. Class modules can have properties, procedures, and functions.
This looks like a procedure. If you need to return a value then make it a function.
 
Majp

Thank you for that. It may well be that I have used a similar name as I have hacked the changes into a form which ran its own version of the code and commented that out to try and cludge it together to debug the code class.

The purpose of the class is that I have a form for client records,for example, and when working it is easy to get lost as calls come in from different clients. The aim of the class is to allow the user to step back between the most recent clients using 2 navigation buttons for forwards and backwards between the most recent clients.

I use a ring buffer which when full overwrites the oldest. Using the back or forwards button the user can then navigate through the most recent clients. Clients are initially selected using a standard combo box as a record selector.

I have other forms where a working ring buffer would be useful but it would be neater to re-utilise the code. Hence a class seems ideal.

Thank you
 
I'm with MajP on this.

Presumably you are retrieving the clients into a recordset and, I would assume, the ClientIDs are unique. Retaining bookmarks has all the disadvantages that MajP described. If you instead retain unique keys then you can use the FindFirst / FindLast / FindNext / Seek methods of the recordset in DAO or the Find / Seek methods in ADO.

In addition to avoiding the problems of bookmarks, that approach gives you a way of displaying what the last or next clientIDs are. With the bookmark approach you must reposition the recordset to get that information.
 
Golom

Thank you I see you mean: don't put the second arg in. I was running the code without the second arg but there is a typo in my original post where I was saying that ("Secondly if I rmove the second string") so it wasn't clear. Appologies for that.

A thing that I am a bit worried about is that the db regularly opens up various instantiations of the basic client form, ie: all clients; current clients only; cancelled clients only. I thought by having the properties and methods Private then there would be less chance of problems caused by multiple forms which each open a copy of the class. As I say they are new to me.

Thank you for your time.
 
We may be suffering a terminology problem here.

The definition of class properties and methods happens inside the class. If you want those properties and/or methods visible outside the class then they must be declared Public.

In contrast, an instance of a class is created outside the class (i.e. in a form, module, another class, etc.). For example
Code:
Private myInstance As Class1
If that statement appeared in the General Declarations section of a form then myInstance would be visible only to routines in that form.

Thus each instance of a form can have its own instance(s) of a class without the risk of different forms getting confused about which instance they are using.

As MajP and I said before, you can use a method (i.e. Sub) in place of the property and the code inside the Sub would be the same as if it had been a property except that you can now have two arguments.

You can still have Property Get calls for each of those arguments even though there is no Property Let corresponding to them.
 
Golom and Majp

Thank you for your help and advice. I have re-written the class to use a public function to handle the ringbuffer and return the next record to set the form to. I have also replaced the use of bookmarks with using the uniqueID of each forms underlying recordset and it works really well.

Once again thanks for the help and the tutorial which was really helpful:)

CloverDog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top