PC Review


Reply
Thread Tools Rate Thread

Array Compartments

 
 
Brad
Guest
Posts: n/a
 
      21st Aug 2008
Thanks for taking the time to read my question.

I am defining an array at the start of my Function. My problem is telling
Access how many compartments to reserve, it's a floating number. I tried
making a variable but the error msg I'm getting says I need a static number.

Is there a way around this?

Brad


Here is my code. I thought that I could use Excels ability to calculate
Median instead of reprogramming this. If it's harder to do this than
reprogram it, please let me know.

Function GetMedian()

Dim rst As DAO.Recordset
Dim dbs As DAO.Database
Dim objExcel As Object
Dim x As Integer
Dim NumOfRec As Integer

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tbl_Results")

NumOfRec = rst.RecordCount





Set objExcel = CreateObject("Excel.Application")

Dim dblData(NumOfRec) As Double

rst.MoveFirst
x = 0
Do Until rst.EOF
dblData(x) = rst!Protein
x = x + 1
Loop

GetMedian = objExcel.WorkSheetFunction.Median(dblData)

rst.Close

Set rst = Nothing
Set objExcel = Nothing

End Function

 
Reply With Quote
 
 
 
 
Michel Walsh
Guest
Posts: n/a
 
      21st Aug 2008
You should dim without dimension and use Redim:


Dim x() AS Double
...
Redim x( intVar ) AS Double



If you want to keep the values previously in your variable, use


Redim Preserve x( intVar )


as example.

Note that OPTION EXPLICIT DOES NOT COVER YOU on Redim variables, so,
be careful about typos.


You can also use GetRows on your recordset, rather than looping over it and
storing the values in an array:


Dim x() As Variant
x=rst.GetRows()




Vanderghast, Access MVP



"Brad" <(E-Mail Removed)> wrote in message
news:9D6496A3-2E4D-43AA-AB6A-(E-Mail Removed)...
> Thanks for taking the time to read my question.
>
> I am defining an array at the start of my Function. My problem is telling
> Access how many compartments to reserve, it's a floating number. I tried
> making a variable but the error msg I'm getting says I need a static
> number.
>
> Is there a way around this?
>
> Brad
>
>
> Here is my code. I thought that I could use Excels ability to calculate
> Median instead of reprogramming this. If it's harder to do this than
> reprogram it, please let me know.
>
> Function GetMedian()
>
> Dim rst As DAO.Recordset
> Dim dbs As DAO.Database
> Dim objExcel As Object
> Dim x As Integer
> Dim NumOfRec As Integer
>
> Set dbs = CurrentDb
> Set rst = dbs.OpenRecordset("tbl_Results")
>
> NumOfRec = rst.RecordCount
>
>
>
>
>
> Set objExcel = CreateObject("Excel.Application")
>
> Dim dblData(NumOfRec) As Double
>
> rst.MoveFirst
> x = 0
> Do Until rst.EOF
> dblData(x) = rst!Protein
> x = x + 1
> Loop
>
> GetMedian = objExcel.WorkSheetFunction.Median(dblData)
>
> rst.Close
>
> Set rst = Nothing
> Set objExcel = Nothing
>
> End Function
>



 
Reply With Quote
 
Brad
Guest
Posts: n/a
 
      21st Aug 2008
Very awesome!

Thanks Michel,

Brad

"Michel Walsh" wrote:

> You should dim without dimension and use Redim:
>
>
> Dim x() AS Double
> ...
> Redim x( intVar ) AS Double
>
>
>
> If you want to keep the values previously in your variable, use
>
>
> Redim Preserve x( intVar )
>
>
> as example.
>
> Note that OPTION EXPLICIT DOES NOT COVER YOU on Redim variables, so,
> be careful about typos.
>
>
> You can also use GetRows on your recordset, rather than looping over it and
> storing the values in an array:
>
>
> Dim x() As Variant
> x=rst.GetRows()
>
>
>
>
> Vanderghast, Access MVP
>
>
>
> "Brad" <(E-Mail Removed)> wrote in message
> news:9D6496A3-2E4D-43AA-AB6A-(E-Mail Removed)...
> > Thanks for taking the time to read my question.
> >
> > I am defining an array at the start of my Function. My problem is telling
> > Access how many compartments to reserve, it's a floating number. I tried
> > making a variable but the error msg I'm getting says I need a static
> > number.
> >
> > Is there a way around this?
> >
> > Brad
> >
> >
> > Here is my code. I thought that I could use Excels ability to calculate
> > Median instead of reprogramming this. If it's harder to do this than
> > reprogram it, please let me know.
> >
> > Function GetMedian()
> >
> > Dim rst As DAO.Recordset
> > Dim dbs As DAO.Database
> > Dim objExcel As Object
> > Dim x As Integer
> > Dim NumOfRec As Integer
> >
> > Set dbs = CurrentDb
> > Set rst = dbs.OpenRecordset("tbl_Results")
> >
> > NumOfRec = rst.RecordCount
> >
> >
> >
> >
> >
> > Set objExcel = CreateObject("Excel.Application")
> >
> > Dim dblData(NumOfRec) As Double
> >
> > rst.MoveFirst
> > x = 0
> > Do Until rst.EOF
> > dblData(x) = rst!Protein
> > x = x + 1
> > Loop
> >
> > GetMedian = objExcel.WorkSheetFunction.Median(dblData)
> >
> > rst.Close
> >
> > Set rst = Nothing
> > Set objExcel = Nothing
> >
> > End Function
> >

>
>
>

 
Reply With Quote
 
Tom van Stiphout
Guest
Posts: n/a
 
      22nd Aug 2008
On Thu, 21 Aug 2008 16:21:09 -0400, "Michel Walsh"
<vanderghast@VirusAreFunnierThanSpam> wrote:

When using Redim it is very cool to also use its counterpart: Erase.
See help file.

-Tom.
Microsoft Access MVP


>You should dim without dimension and use Redim:
>
>
>Dim x() AS Double
> ...
> Redim x( intVar ) AS Double
>
>
>
>If you want to keep the values previously in your variable, use
>
>
> Redim Preserve x( intVar )
>
>
>as example.
>
>Note that OPTION EXPLICIT DOES NOT COVER YOU on Redim variables, so,
>be careful about typos.
>
>
>You can also use GetRows on your recordset, rather than looping over it and
>storing the values in an array:
>
>
>Dim x() As Variant
> x=rst.GetRows()
>
>
>
>
>Vanderghast, Access MVP
>

<clip>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prevent cell/array references from changing when altering/moving thecell/array nme Microsoft Excel Misc 1 19th Sep 2008 01:53 PM
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R Microsoft Excel Programming 3 13th Nov 2007 04:08 PM
meaning of : IF(Switch; Average(array A, array B); array A) =?Utf-8?B?RFhBVA==?= Microsoft Excel Worksheet Functions 1 24th Oct 2006 06:11 PM
Pass from C# (framework 1.1) array of delegates to unmanaged DLL as array of function pointers verpeter@gmail.com Microsoft C# .NET 0 23rd Aug 2006 02:20 PM
select variables ranges, copy to array, paste the array in new workbook Mathew Microsoft Excel Worksheet Functions 1 1st Apr 2005 09:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 PM.