In my Access database project, SQL server as backend, there're several functions have this repeated piece of code. I wonder if it's OK to declare the two variables (one for ADODB command, another is for RecordSet) as global? will that cause any potential problems? Thanks very much.
Dim cmd As ADODB.Command, rst As ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
9 12265
And these two lines are redundant everywhere. Is there a better way to work around them? Thanks again.
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
@anniebai
Global Variables can be very tricky and problematic. It is very hard to say whether or not you should Declare them as such without seeing the entire code context. What you possibly can do is to Declare a Public Function, and pass to it certain Arguments eliminating some duplication. In the following code segment, you can pass the name of a View to the Function and it will execute it eliminating the need for duplicate Command and Recordset Object Declarations while displaying all Field Names and their Values for each Record. Again, we would have to see the entire code context to see if something such as this is a viable option. - Public Function fTestFunction(strQueryName As String)
-
Dim cmd As ADODB.Command
-
Dim rst As ADODB.Recordset
-
Dim intFieldCounter As Integer
-
-
Set cmd = New ADODB.Command
-
-
With cmd
-
.ActiveConnection = CurrentProject.Connection
-
.CommandType = adCmdTable
-
.CommandText = strQueryName
-
End With
-
-
Set rst = cmd.Execute
-
-
With rst
-
Do While Not .EOF
-
For intFieldCounter = 0 To .Fields.Count - 1
-
Debug.Print .Fields(intFieldCounter).Name & " - " & .Fields(intFieldCounter).Value
-
Next
-
.MoveNext
-
Debug.Print "------------------------------------------------"
-
Loop
-
End With
-
-
rst.Close
-
Set rst = Nothing
-
Set cmd = Nothing
-
End Function
Sample Call: - Call fTestFunction("Invoices")
Thanks ADezii. It seems a safe plan. I'll give it a try.
My code is like below. There're several Subs function similarly, the differences are the stored procedure names, procedure parameters, and the control name (which is to receive the RecordSet) -
Private Sub LectureMaintSubFormRequery()
-
Dim cmd As ADODB.Command, rst As ADODB.Recordset
-
Set cmd = New ADODB.Command
-
cmd.ActiveConnection = CurrentProject.Connection
-
cmd.CommandText = "EXEC dbo.spLectures " & Me.WkOfferingId
-
cmd.CommandType = adCmdText
-
Set rst = cmd.Execute
-
Set Me![frmSubLectureMaint].Form.Recordset = rst
-
Set cmd = Nothing
-
Set rst = Nothing
-
End Sub
-
I tried to create a function like
Public Function ExecuteSP(spName As String) As ADODB.Recordset
But problems are:
1. How to set the return value, which is ADODB.RecordSet, to nothing?
2. Where and how to pass the stored procedures' parameters? the stored procedures have various number and type of parameters.
Thanks very much for any inputs.
@anniebai
1. How to set the return value, which is ADODB.RecordSet, to nothing?
The way I see it, you have 2 Options here, either create the Recordset and destroy it within the context of the Function: - Dim rst As ADODB.Recordset
-
-
Set rst = New ADODB.Recordset
-
-
rst.Open "qryEmployees", CurrentProject.Connection, _
-
adOpenStatic, adLockOptimistic
-
-
'Process the Recordset
-
'...Yada,Yada,Yada
-
-
'Close the Recordset in Memory & claim resources
-
rst.Close
-
Set rst = Nothing
OR Create/Set a Recordset = the Return Value of the Function, then deal with it -
Dim rst As ADODB.Recordset
-
Set rst = fFunctionReturningADODBRecordset()
-
-
'Process the Recordset
-
'...Yada,Yada,Yada
-
-
'Close the Recordset in Memory & claim resources
-
rst.Close
-
Set rst = Nothing
2. Where and how to pass the stored procedures' parameters? the stored procedures have various number and type of parameters.
You can pass an Array containing the Parameters to the Function, use the ParamArray Keyword, or my favorite: pass a Comma-Delimited String containing the Parameters to the Function which will then Split the String and place the Parameters into a Variant Array. I made some sample code for you to view below: - Public Function fSomeFunction(dteSomeDate As Date, strParams As String, lngBigNum As Long)
-
'This Function will accept 3 Parameters: a Date, Delimited String, and a Long
-
Dim varValues As Variant
-
Dim intCounter As Integer
-
-
varValues = Split(strParams, ",")
-
-
Debug.Print "The Date is: " & dteSomeDate
-
Debug.Print "A BIG Number is: " & lngBigNum
-
Debug.Print
-
-
Debug.Print "=========================================="
-
Debug.Print "Number of Parameters: " & UBound(varValues) + 1
-
Debug.Print "=========================================="
-
-
For intCounter = LBound(varValues) To UBound(varValues)
-
Debug.Print " Parameter #" & Format$(intCounter + 1, "00") & ": " & _
-
varValues(intCounter)
-
Next
-
End Function
Sample Call to Function: - Dim dteDate As Date
-
Dim strParameter1 As String
-
Dim strParameter2 As String
-
Dim strParameter3 As String
-
Dim strAllParameters As String
-
-
dteDate = Date
-
-
strParameter1 = "Parameter One"
-
strParameter2 = "Parameter Two"
-
strParameter3 = "Parameter Three"
-
-
strAllParameters = strParameter1 & "," & strParameter2 & "," & strParameter3
-
-
Call fSomeFunction(dteDate, strAllParameters, 9876599)
OUTPUT: - The Date is: 7/24/2009
-
A BIG Number is: 9876599
-
-
==========================================
-
Number of Parameters: 3
-
==========================================
-
Parameter #01: Parameter One
-
Parameter #02: Parameter Two
-
Parameter #03: Parameter Three
-
Dim rst As ADODB.Recordset
-
Set rst = fFunctionReturningADODBRecordset()
-
-
'Process the Recordset
-
'...Yada,Yada,Yada
-
-
'Close the Recordset in Memory & claim resources
-
rst.Close
-
Set rst = Nothing
Since fFunctionReturningADODBRecordset() is also a recordset, Do I need to do this at the end of the calling procedure?
Set fFunctionReturningADODBRecordset() = nothing
You can pass an Array containing the Parameters to the Function, use the ParamArray Keyword, or my favorite: pass a Comma-Delimited String containing the Parameters to the Function which will then Split the String and place the Parameters into a Variant Array. I made some sample code for you to view below
I can see that the example takes more than one string parameters. In the case of more than one integer pars, is ParamArray then the only option? do you have some examples of ParamArray?
Thanks a lot for your kind reply and helps.
anniebai;3501479]I can see that the example takes more than one string parameters. In the case of more than one integer pars, is ParamArray then the only option? do you have some examples of ParamArray?
Thanks a lot for your kind reply and helps.
http://bytes.com/topic/access/insigh...ing-paramarray Sign in to post your reply or Sign up for a free account.
Similar topics
by: David T. Ashley |
last post by:
Hi,
In my project, I typically declare and define variables in the .H
file, i.e.
DECMOD_MAIN UINT8 can_message_201_status_global
#ifdef MODULE_MAIN
= HAS_NEVER_BEEN_RECEIVED
#endif
;
|
by: rick |
last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file.
I am currently loading the external '.js' file from the header. Problem is I
would like to declare
a global variable in the...
|
by: Miguel Dias Moura |
last post by:
Hello,
I am working on an ASP.NET / VB page and I created a variable "query":
Sub Page_Load(sender As Object, e As System.EventArgs)
Dim query as String = String.Empty
...
query =...
|
by: Nick N. |
last post by:
Hi all,
I just started to play around with VB.Net.
I currenlty do Powerbuilder work and was wondering how database connection
management works in VB.net.
My applications typically pop-up a...
|
by: Brian Kitt |
last post by:
I frequently get the above error on my website. It happens only for short
periods of times, then the error goes away. I cannot recreate this. I have
an error trap that picks up this error. When...
|
by: Robin Haswell |
last post by:
Hey people
I'm an experience PHP programmer who's been writing python for a couple of
weeks now. I'm writing quite a large application which I've decided to
break down in to lots of modules...
|
by: Terry Jolly |
last post by:
Web Solution
Goal: Have a global database connection
Why: (There will be 30+ tables, represented by 30+ classes) I only want to
reference the database connection once.
I put the connection...
|
by: frothpoker |
last post by:
Guys,
I'm sure this has been asked a million times but I can't seem to
formulate a google search that returns what i'm looking for.
I've go a dev and live environment. Currently the DB...
|
by: Slant |
last post by:
Here's a question that most will have different answers to. I'm just
dying to find a solution that seems halfway automated!! There really
are two seperate issues which might be answered by the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |