?
Solved

Data structure in VB

Posted on 2003-03-25
13
Medium Priority
?
249 Views
Last Modified: 2012-05-04
Hello All,
I am very new to VB.
I have a situation where I need something like a Hashtable.
Is there an equivalent to Hashtable in VB?

Loop through the resultset and load all the information in a data structure.

while not resultSet.EOF
   account = rsExist("account")
    pcfile = rsExist("pcfile")
    dateValue = rsExist("date")
    timeValue = rsExist("time")
    'Store this information in a collection
    filesInDB As New Collection

I thought I could create a new collection for each iteration and add it to the collection.
But, it doesnt let me create a new collection with-in an if or while block.
What is the solution?

Thanks in advance
smovva

0
Comment
Question by:smovva
[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
13 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8202619
1) you can create a User Defined Type (a Structure) in VB to hold the Data.  The create a Varibale of THAT user-defined typoe, to be added to the collection:

in the General Section of your code(at the VERY top of the code module)

Private Type MyData
   Account as String
   pcFile as String
   DateValue as Date
   TimeValue as Date
End Type
Private FilesInDB As Collection

then, where you are reading the data from the database:

Dim MyValues as MyType

Set FilesInDB = New Collection

Do While Not rsExist.EOF
  Set MYvalues = New MyData
  With MyValues
     .account = rsExist("account")
     .pcfile = rsExist("pcfile")
     .dateValue = rsExist("date")
     .timeValue = rsExist("time")
   'Store this information in the collection
     FilesInDb.Add MyValues
  End With
  rsExist.MoveNext
Loop

you will of course need to provide the code to load the data into the rsExist recordset.

Arthur Wood
0
 

Author Comment

by:smovva
ID: 8202822
Thanks for the info.
It gives a compile Error:"Invalid use of New Keyword" at
set mYValues = New MyData
I am assuming u made a typo in
Dim MyValues as MyType

I declared it as
Dim MyValues as MyData

Thanks
smovva
0
 

Author Comment

by:smovva
ID: 8202950
If I comment that line and move on (Just to compile)
it gave me another compile error
"Only User-defined types defined in public object modules can be coerced to or from a variant or passed to late bound functions" at FilesInDB.Add MyValues

Thanks
smovva
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8203046
ok, so declare :

Public Type MyData
  Account as String
  pcFile as String
  DateValue as Date
  TimeValue as Date
End Type


and yes, that is correct (my error--Set MyValues = New MyData was WRONG, and that line Should NOT be in the code, and YES, it should have read :

Dim MyValues as MyData  )


AW
0
 

Author Comment

by:smovva
ID: 8203091
still gives the same error:
"Only User-defined types defined in public object modules can be coerced to or from a variant or passed to late bound functions" at FilesInDB.Add MyValues

I am not doing anything different. I copied your code into a test project and trying to compile.

Thanks
smovva
0
 
LVL 1

Expert Comment

by:DeepBlueInVB
ID: 8203270
you can also use a disconnected recordset.

create a Recordset at the Client side and populate it with the data of rsExist.


0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8203284
ok, then we'll take another appraoch>>
Add a Class Module to your project,with thisd code:

Option Explicit
Dim mAccount As String
Dim mFile As String
Dim mDateValue As Date
Dim mTimeValue As Date

Public Property Get Account() As String
    Account = mAccount
End Property

Public Property Let Account(strValue As String)
    mAccount = strValue
End Property

Public Property Get pcFile() As String
    pcFile = mFile
End Property

Public Property Let pcFile(strValue As String)
    mFile = strValue
End Property

Public Property Get DateValue() As Date
    DateValue = mDateValue
End Property

Public Property Let DateValue(dtValue As Date)
    mDateValue = dtValue
End Property

Public Property Get TimeValue() As Date
    TimeValue = mTimeValue
End Property

Public Property Let TimeValue(dtValue As Date)
    mTimeValue = dtValue
End Property

Change the Properties of the Class module, to give it the name MyCLass (defaults to Class1)


Then change the calling code like this:


Dim MyValues As MyClass
Dim rsExist As Recordset
Set FilesInDB = New Collection

Do While Not rsExist.EOF
 Set MyValues = New MyClass
 With MyValues

    .Account = rsExist("account")
    .pcFile = rsExist("pcfile")
    .DateValue = rsExist("date")
    .TimeValue = rsExist("time")
  'Store this information in the collection
    FilesInDB.Add MyValues
 End With
 rsExist.MoveNext
Loop





0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8203293
and welcome to the world of Object Oriented programming.  

;-)

AW
0
 

Author Comment

by:smovva
ID: 8203400
It compiles now.
I will let you know when I am done.
Thanks alot for your help!!
smovva
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 100 total points
ID: 8203575
what you have created here is a VERY simple example of a Class.  At its simplest, a Class is a somewhat mode powerful version of a User Defined Type.

A class is in fact a NEW type (like an Integer, or a Form), which can have data, and can, in a more extended version, have 'methods' - operations that an object created from the class, can do.  This example simply allows you to store and retrieve some values (Account, pcFile etc).  Once you create an Object based on the class (called an INSTANCE of the class) then you can save THAT object, as we are doing here, in a Collection.  When you need the object, you can then get it from the Collection (the first object added to the collection can be retrieved using

    Dim MyObject as MyClass
    Set MyObject = FilesinDB.Item(1)  'the first item in the collection - the FIRST one that was Added originally


0
 

Expert Comment

by:CleanupPing
ID: 8531644
Hi smovva,
This old question (QID 20562286) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 

Author Comment

by:smovva
ID: 8532925
Good info for a beginer like me.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8544103
thank you
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

752 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