VBA using function return values instead of variables - vba

I've tried searching the internet for a definitive answer to this two-part scenario but couldn't find anything conclusive. I've been writing VBA procedures for sometime now in both Access and Excel and while trying to streamline some code I came across something of a dilemma
The first part is about using functions to return objects. The example below is generally what I've seen on the web for a function to return an ADODB.Recordset (I've simplified the code so no error handling etc.).
Public Function CreateADORecordset(SQL As String, Connection As ADODB.Connection) As ADODB.Recordset
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Call rst.Open(SQL, Connection)
Set CreateADORecordset = rst
End Function
The first part of the question is, why do I need a variable called rst when I could rewrite the function as this:
Public Function CreateADORecordset(SQL As String, Connection As ADODB.Connection) As ADODB.Recordset
Set CreateADORecordset = New ADODB.Recordset
Call CreateADORecordset.Open(SQL, Connection)
End Function
Is there anything fundamentally wrong with the above rewrite of the function? As the function returns an ADO recordset, why declare a variable separately?
I can take this a step further:
Public Function CreateADOConnection(ConnectionString As String) As ADODB.Connection
Set CreateADOConnection = New ADODB.Connection
Call CreateADOConnection.Open(ConnectionString)
End Function
Public Function CreateADORecordset(SQL As String, ConnectionString As String) As ADODB.Recordset
Set CreateADORecordset = New ADODB.Recordset
Call CreateADORecordset.Open(SQL, CreateADOConnection(ConnectionString))
End Function
Again, is this a particularly bad thing to do by using function return objects over declaring objects within the procedures via Dim?
In the grand scheme of things, I've been writing VBA code to transfer the contents of a recordset via GetRows into an Excel range. The function declaration line is:
Public Sub TransferRecordsetArray(GetRows As Variant, Destination As Range)
As TransferRecordsetArray works correctly, I've not included the code.
The dilemma I'm in now is in this scenario, I've reached a point where I don't need to declare any variables for the code to run correctly and I'm unsure how much of a good or bad thing this in terms of functions returning objects, scope and variables, etc.
In order to run the code correctly, I only need one of two lines without variables:
Call TransferRecordsetArray(CreateADOConnection(ConnectionString).Execute(SQL).GetRows, Target)
or
Call TransferRecordsetArray(CreateADORecordset(SQL, CreateADOConnection(ConnectionString)).GetRows, Target)
Any advice/recommendations on this way of writing VBA code would be greatly appreciated. I have used the task manager to keep an eye on memory usage on both methods and it doesn't seem to greatly differ and, it appears that VBA destroys the function return objects after a while, despite them not being explicitly destroyed by setting them to Nothing.
Many thanks.

The first part of the question is, why do I need a variable called rst when I could rewrite the function as this
Public Function CreateADORecordset(SQL As String, Connection As ADODB.Connection) As ADODB.Recordset
Set CreateADORecordset = New ADODB.Recordset
Call CreateADORecordset.Open(SQL, Connection)
End Function
You don't need a separate variable. Your code is perfectly fine.
I can take this a step further:
Public Function CreateADOConnection(ConnectionString As String) As ADODB.Connection
Set CreateADOConnection = New ADODB.Connection
Call CreateADOConnection.Open(ConnectionString)
End Function
Public Function CreateADORecordset(SQL As String, ConnectionString As String) As ADODB.Recordset
Set CreateADORecordset = New ADODB.Recordset
Call CreateADORecordset.Open(SQL, CreateADOConnection(ConnectionString))
End Function
Yes, absolutely. Nothing wrong with that.
I've reached a point where I don't need to declare any variables for the code to run correctly
Congratulations, keep it up. :)
Further reading: Is there a need to set Objects to Nothing

Related

VB .NET NullReferenceException, "Object reference not set to an instance of an object" when it seemingly is

as the title reads, I am getting a NullReferenceException that says "Object reference not set to an instance of an object". This is a legacy WinForms app that I am working on so keep in mind the structure of the code is not my doing. I am trying to compare a recordset before and after a change is made to it. I am hitting my issue in trying to make a copy of the old recordset.
Here is my code to populate the copy of the old data, which is in a module:
Public Function Prevdata(ByRef table As ADODB.Recordset) As ADODB.Recordset
Dim i As Integer
Dim pData As New ADODB.Recordset
table.MoveFirst()
If Not table.BOF And table.EOF Then
While Not table.EOF
For i = 0 To table.Fields.Count - 1
pData.AddNew()
pData(table.Fields(i)).Value = table.Fields(i).Value
Next
pData.Update()
table.MoveNext()
End While
End If
PrevData = pData
End Function
And here is my code in one of the forms. These are just with my other variable declarations at the top of the file. GetData just simply creates a recordset:
Public pData As ADODB.Recordset = GetData(tableName)
Public pDataC As ADODB.Recordset = PrevData(pData)
I get the error in the module in PrevData anywhere where "table" is referenced. I appreciate any help.

Optimization for recursive SQL Data reading

I have a function that needs to check the current database values against the live data. Due to the way the database is set up, I need to run the function recursively, and it creates a new local ADO connection each time then deletes it when its finished. it is definitely slowing down my page. Assuming the SQL command is not the problem, is there a better way to obtain the database record (Faster) without establishing a new ADO record set connection each time?
Here is my code/pseudo sample:
Sub recursiveFunction()
~ run data validation ~
Dim newObjRecordSet As Object = SysGlobals.CreateLocalObject()
newObjRecordSet.Open(SQL_COMMAND,objConn, adOpenStatic ,adLockBatchOptimisti)
Do Until newObjRecordSet.eof
If (some condition) Then
write()
Else
recursiveFunction()
End If
newObjRecordSet.MoveNext()
Loop
SysGlobals.DeleteLocalObject(newObjRecordSet)
End Sub
Here are the global functions:
Public Shared Function CreateLocalObject() As Object
Dim localObj As New Object
localObj = CreateObject("ADODB.Recordset")
localObj.CursorLocation = ADORef.ADOVBS.adUseClient
Return localObj
End Function
Public Shared Sub DeleteLocalObject(ByRef localObj As Object)
If localObj.State() Then localObj.Close()
localObj = Nothing
End Sub
Any inputs would be appreciated!

Constructor in VBA - Runtime error 91 "Object variable not set"

I am trying to write some code in excel VBA using the Object Oriented Concept. Therefore I wanted to initialize my objects with constructors, like we usually do in Java. However I discovered that the default Class_Initialize() Sub that is offered in VBA does not take arguments. After searching a bit, I found that the answer for this Question proposed a pretty good alternative.
Here is a sample of my Factory Module (I Named it Creator):
Public Function CreateTool(ToolID As Integer) As cTool
Set CreateTool = New cTool
CreateTool.InitiateProperties (ToolID) '<= runtime error 91 here
End Function
The class cTool:
Private pToolID As Integer
Private pAttributes As ADODB.Recordset
Private pCnn As ADODB.Connection
Public Sub InitiateProperties(ToolID As Integer)
Dim sSQL As String
Set pCnn = connectToDB() 'A function that returns a connection to the main DB
pToolID = ToolID
sSQL = "SELECT Tool_ID, Status, Type, Tool_Number " _
& "FROM Tool WHERE Tool_ID = " & pToolID
pAttributes.Open sSQL, pCnn, adOpenKeyset, adLockOptimistic, adCmdText
End Sub
This is how I call the constructor:
Dim tool As cTool
Set tool = Creator.CreateTool(id)
My issue is that when I run the code, I get the following error:
Run-Time error '91' : Object Variable or With Block Variable not Set
The debug highlights the CreateTool.InitiateProperties (ToolID) line of my CreateTool Function.
I know that this usually happens when someone is setting a value to an object without using the keyword Set but it does not seem to be my case.
Any help, advice to resolve this issue would be greatly appreciated!
Thanks.
Might not be the cause of your error, but this:
Public Function CreateTool(ToolID As Integer) As cTool
Set CreateTool = New cTool
CreateTool.InitiateProperties (ToolID) '<= runtime error 91 here
End Function
Is problematic for a number of reasons. Consider:
Public Function CreateTool(ByVal ToolID As Integer) As cTool
Dim result As cTool
Set result = New cTool
result.InitiateProperties ToolID
Set CreateTool = result
End Function
Now, looking at the rest of your code, you're doing the VBA equivalent of doing work in the constructor, i.e. accessing database and other side-effects to constructing your object.
As #Jules correctly identified, you're accessing the unitialized object pAttributes inside InitiateProperties - that's very likely the cause of your problem.
I'd strongly recommend another approach - if you come from Java you know doing work inside a constructor is bad design... the same applies to VBA.
Get your code working, and post it all up on Code Review Stack Exchange for a full peer review.

Invalid Use of Property?

I am working with an Access database and it has a form and VBA behind it. It has been quite a while since I dabbled in VBA and my google-fu is failing me right now so bear with me.
I created a simple class, and I am getting a compile error:
Dim oRecordSet As ADODB.RecordSet
Public Property Get RecordSet() As ADODB.RecordSet
RecordSet = oRecordSet '' error here
End Property
Public Property Let RecordSet(ByVal val As ADODB.RecordSet)
RecordSet = val
End Property
I have a couple other identical properties (different names/variables, obviously) that compile just fine; their types are String and Integer.
What am I missing? Thanks!
Also a side note, when I am coding the intellisense shows ADODB.Recordset, but on autoformat (carriage return, compile, etc) it changes it to ADODB.RecordSet. Need I be worried?
It should be:
Public Property Get RecordSet() As ADODB.RecordSet
Set RecordSet = oRecordSet '' error here
End Property

Variable not declared before it is used with DAO

I'm using DAO (been asked not to use ADO.NET) to update an Access database. I'm currently thus far, however, VB2008 is telling me that the variable "daoengine" is not declared before it is used. What am I doing wrong in the following code?
Function update_db()
Dim daoengine As DAO.DBEngine
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
dbs = daoengine.OpenDatabase("Project.mdb")
rst = dbs.OpenRecordset("Log", dao.RecordsetTypeEnum.dbOpenDynaset)
End Function
You should have
Dim daoengine As New DAO.DBEngine
instead of
Dim daoengine As DAO.DBEngine
When you say
Dim daoengine As DAO.DBEngine
you're just creating a variable, daoengine, but it doesn't actually point to anything. You need to do this afterwards:
Set daoengine = New DAO.DBEngine
You can also type Dim daoengine As New DAO.DBEngine, but it's better to do it in the two lines above. If you put the New in the Dim line, you create what's called an "auto-instancing" variable which can magically come to life again after you thought you'd disposed of it.
For more details see the Don't Use Auto-Instancing Object Variables here: Declaring Variables (in VBA)