Newbie has an Object question???

greenguy

Registered User.
Local time
Today, 14:29
Joined
Oct 30, 2007
Messages
36
Howdy,

I've been teaching myself VBA and I have a question about what I do with an Object after the Sub. I'm wondering if I should always set the Object to Nothing. Below are my two examples. Thanks for any help

Public Sub MakeFormIconsListView()

Dim lstview As ListView
Dim strSQL As String

Set lstview = Form_frmicons.lview.Object

With lstview
.View = lvwReport
.ListItems.Clear
.ColumnHeaders.Clear
.AllowColumnReorder = True
.FullRowSelect = True
End With

With lstview.ColumnHeaders
.Add , , "Form", 2000
.Add , , "Location", 6000
End With

strSQL = "SELECT * FROM QRYicons;"
Set lstview = Nothing
Call FillIconListView(strSQL)
End Sub

Public Sub FillIconListView(strSQL)

Dim rst As DAO.Recordset
Dim db As Database
Dim LstItem As ListItem
Dim lstview As ListView

Set lstview = Form_frmicons.lview.Object
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL)
rst.MoveFirst
Do Until rst.EOF
Set LstItem = lstview.ListItems.Add()
LstItem.Text = Nz(rst![FrmName])
LstItem.SubItems(1) = Nz(rst![Location])
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set LstItem = Nothing
Set lstview = Nothing
DoCmd.Echo True


End Sub
 
It is good coding practice to release object you no longer need. So set all your object to NOTHING and close object when your done.

JR
 
It is also good practice when posting code to enclose it in Code tags. You can generate these by enclosing the word code in square brackets at the start and closing them at the end by enclosing /code in the brackets.

It makes your code much easier to read as it preserves the indents etc.
 
Thanks for the advise, and my apologies for the hard to read code. I'll make sure to include the brackets in the future.
 

Users who are viewing this thread

Back
Top Bottom