473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

simple function record into array

Hi,
Im a rookie in vba, and therefor got a problem.
i do have folwing records
F G
---------------
2 3
2 5
6 7
2 3
and those i realy wanto plug into a array, but cant remember how to do
this in VBA
can any one help me ? plzz.

Jan 10 '06 #1
11 6107
rkc
Geagleeye wrote:
F G
---------------
2 3
2 5
6 7
2 3
and those i realy wanto plug into a array, but cant remember how to do
this in VBA
can any one help me ? plzz.


Both DAO and ADO have a GetRows method of the recordset object.
GetRows retrieves a specified number or rows from a recordset
and places them into an 2-D array.
Jan 10 '06 #2
"Geagleeye" <Ur********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi,
Im a rookie in vba, and therefor got a problem.
i do have folwing records
F G
---------------
2 3
2 5
6 7
2 3
and those i realy wanto plug into a array, but cant remember how to do
this in VBA
can any one help me ? plzz.



I assume you want to get this data from a table in the database and that you
are not asking about how to get those 'hard-coded' values into a
2-dimensional array.
As rkc, points out you can use DAO or ADO coding. The ADO version is shown
here since I do not have to specify the number of rows returned - by default
all records are retrieved:
Public Sub TestArray()

On Error GoTo Err_Handler

Dim avar As Variant
Dim lngCount As Long

avar = CreateArray("SELECT F, G FROM MyTable")

For lngCount = LBound(avar) To UBound(avar)
MsgBox CStr(avar(0, lngCount))
Next lngCount

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
Public Function CreateArray(strSQL As String) As Variant

On Error GoTo Err_Handler

Dim rst As ADODB.Recordset

Set rst = New ADODB.Recordset

rst.Open strSQL, CurrentProject.Connection

CreateArray = rst.GetRows()

Exit_Handler:

If Not rst Is Nothing Then
If rst.State <> adStateClosed Then
rst.Close
End If
Set rst = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
Jan 10 '06 #3
For lngCount = LBound(avar, 2) To UBound(avar, 2)

?

Jan 10 '06 #4
"Lyle Fairfield" <ly***********@aim.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
For lngCount = LBound(avar, 2) To UBound(avar, 2)

?

When I saw that you had replied to this post I was so sure you were going to
dispute the need to explicitly close and set to nothing the ADO recordset -
I hadn't spotted that somewhat more fundamental mistake.
Even worse, I had tried the function with, not one, but two test rows in my
table (just to double check it) and under those conditions it performed
great, but I now concede that if the table has more than two rows, your
version would be better.
Jan 10 '06 #5

"Lyle Fairfield" <ly***********@aim.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
For lngCount = LBound(avar, 2) To UBound(avar, 2)

?

When I saw that you had replied to this post I was so sure you were going to
dispute the need to explicitly close and set to nothing the ADO recordset -
I hadn't spotted that somewhat more fundamental mistake.
Even worse, I had tried the function with, not one, but two test rows in my
table (just to double check it) and under those conditions it performed
great, but I now concede that if the table has more than two rows, your
version would be better.
Jan 10 '06 #6
Hi guys. Thanky for all help :)

But i need som explanation to understand all this. :(

this is the code :

avar = CreateArray("SELECT X, A, B, C, D, E, F, G Tmp")

For lngCount = LBound(avar, 2) To UBound(avar, 2)
' MsgBox CStr(avar(0, lngCount))
Next lngCount


And how can i read data from the B234 ? or c234 and how can i write
into F234 ?

Jan 11 '06 #7
"Geagleeye" <Ur********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi guys. Thanky for all help :)

But i need som explanation to understand all this. :(

this is the code :

avar = CreateArray("SELECT X, A, B, C, D, E, F, G Tmp")

For lngCount = LBound(avar, 2) To UBound(avar, 2)
' MsgBox CStr(avar(0, lngCount))
Next lngCount


And how can i read data from the B234 ? or c234 and how can i write
into F234 ?

We are happy to help but you need to explain something:
Should the data come from database tables or will you write the values in
code?
Are you sure you need to use arrays? Perhaps if you explained what you are
doing - the answer might be to use recordsets not arrays.

Jan 11 '06 #8
first of all, i would like say thanks to everyone for helpin me (a
rookie in vb)

the problem is that i have som code where im using to many
rec.movelast, rec.moveprevius, do until rec.eof and so on for only one
table.
my table is 8 * 1100 and it takes 4 min to get true the code.

Therefor i supposed that the problem is im reading and witting to much
to the table in my database, so why get the data into a array and do
everything there
and when finnished i could get i all into the table again.

my opinion is that a array is much faster end a recordset ? is it ?????
if you like to look at the code wil glady post it in here , and maybe
you could see if you could transfer to a array

Jan 11 '06 #9
Private Sub btn_interpolere_Click()
Dim x As Double
Dim rec As Recordset
Dim qry As QueryDef
Dim strSQL As String
Set dbs = CurrentDb
strSQL = "SELECT * FROM TmpGraf "
Set qry = dbs.CreateQueryDef("", strSQL)
Set rec = qry.OpenRecordset()
For i = 2 To 8
rec.MoveFirst
Do Until Not IsNull(rec.Fields("mpa" & i))
rec.MoveNext
Loop
Do Until rec.Fields("mpa" & i) = 0
If IsNull(rec.Fields("mpa" & i)) Then
x = rec.Fields!Procent
DeltaY = fmmDeltaY(y1, x1, x, i)
DeltaX = fmmDeltaX(x2, y2, x, i)
res = (y2 - y1) / (x2 - x1) * (x - x1) + y1
rec.MoveFirst
While Not rec.Fields!Procent = x
rec.MoveNext
Wend
rec.Edit
rec.Fields("mpa" & i) = Format(res, "#0.000")
rec.Update
End If
rec.MoveNext
Loop
Next i
MsgBox "Interpolering er fuldført "
rec.Close
Set qry = Nothing
End Sub
Function fmmDeltaY(y1, x1, x, i)
Dim rec As Recordset
Dim qry As QueryDef
Dim strSQL As String
Dim dbs As Database
Set dbs = CurrentDb
strSQL = "SELECT * FROM TmpGraf "
Set qry = dbs.CreateQueryDef("", strSQL)
Set rec = qry.OpenRecordset()
While Not rec.Fields!Procent = x
rec.MoveNext
Wend
While IsNull(rec.Fields("mpa" & i))
rec.MovePrevious
Wend
y1 = rec.Fields("mpa" & i)
x1 = rec.Fields!Procent
rec.Close
Set qms = Nothing
End Function
Function fmmDeltaX(x2, y2, x, i)
Dim rec As Recordset
Dim qry As QueryDef
Dim strSQL As String
Dim dbs As Database
Set dbs = CurrentDb
strSQL = "SELECT * FROM TmpGraf "
Set qry = dbs.CreateQueryDef("", strSQL)
Set rec = qry.OpenRecordset()
rec.MoveFirst
While Not rec.Fields!Procent = x
rec.MoveNext
Wend
While IsNull(rec.Fields("mpa" & i))
rec.MoveNext
Wend
x2 = rec.Fields!Procent
y2 = rec.Fields("mpa" & i)
rec.Close
Set qms = Nothing
End Function

Jan 11 '06 #10
"Geagleeye" <Ur********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Private Sub btn_interpolere_Click()
Dim x As Double
Dim rec As Recordset
Dim qry As QueryDef
Dim strSQL As String
Set dbs = CurrentDb
strSQL = "SELECT * FROM TmpGraf "
Set qry = dbs.CreateQueryDef("", strSQL)
Set rec = qry.OpenRecordset()
For i = 2 To 8
rec.MoveFirst
Do Until Not IsNull(rec.Fields("mpa" & i))
rec.MoveNext
Loop
Do Until rec.Fields("mpa" & i) = 0
If IsNull(rec.Fields("mpa" & i)) Then
x = rec.Fields!Procent
DeltaY = fmmDeltaY(y1, x1, x, i)
DeltaX = fmmDeltaX(x2, y2, x, i)
res = (y2 - y1) / (x2 - x1) * (x - x1) + y1
rec.MoveFirst
While Not rec.Fields!Procent = x
rec.MoveNext
Wend
rec.Edit
rec.Fields("mpa" & i) = Format(res, "#0.000")
rec.Update
End If
rec.MoveNext
Loop
Next i
MsgBox "Interpolering er fuldført "
rec.Close
Set qry = Nothing
End Sub
Function fmmDeltaY(y1, x1, x, i)
Dim rec As Recordset
Dim qry As QueryDef
Dim strSQL As String
Dim dbs As Database
Set dbs = CurrentDb
strSQL = "SELECT * FROM TmpGraf "
Set qry = dbs.CreateQueryDef("", strSQL)
Set rec = qry.OpenRecordset()
While Not rec.Fields!Procent = x
rec.MoveNext
Wend
While IsNull(rec.Fields("mpa" & i))
rec.MovePrevious
Wend
y1 = rec.Fields("mpa" & i)
x1 = rec.Fields!Procent
rec.Close
Set qms = Nothing
End Function
Function fmmDeltaX(x2, y2, x, i)
Dim rec As Recordset
Dim qry As QueryDef
Dim strSQL As String
Dim dbs As Database
Set dbs = CurrentDb
strSQL = "SELECT * FROM TmpGraf "
Set qry = dbs.CreateQueryDef("", strSQL)
Set rec = qry.OpenRecordset()
rec.MoveFirst
While Not rec.Fields!Procent = x
rec.MoveNext
Wend
While IsNull(rec.Fields("mpa" & i))
rec.MoveNext
Wend
x2 = rec.Fields!Procent
y2 = rec.Fields("mpa" & i)
rec.Close
Set qms = Nothing
End Function


OK - first thing to say is what you are trying to do - and judging from
previous posts it is this:
"Calculate from a set of data stored in a table, some missing elements by
using interpolation and then write these values back to the table"

Now I don't know whether using an array will be faster in the end. Sure,
they are faster to work with generally but you have to load it from the
table, edit the data and then write it back to the table. It might make
little difference to use a recordset which you can edit directly - I suppose
it depends on your algorithm. One thing is for sure, there is definitely
too much moving around the recordsets and opening and closing new recordsets
based on the whole table. This must be wrong!

If I knew exactly what the interpolation was supposed to be for for example
a data table with 3 columns to identify measurement number, x value, y value
and that you wish to find missing y values. Perhaps also you can be sure
that there are never 2 or more consecutive missing values and that you wish
to use simple linear interpolation between the two points either side of the
missing value. Perhaps then we could offer a solution.

However, you don't make it that clear - your recordset has 8 or more
fields??? Secondly, it is a while since I have done any interpolation of
data (I seem to remember fitting straight lines, curves, polynomials, etc
while I was in college, but that was a while ago)

I thought this was a straight-forward array question which I could answer,
but I'm not sure I have the time at the minute for anything harder. Perhaps
someone else will.

Jan 11 '06 #11
The end and the sollution.

I made all my code inside an array instead of records.

Before the changes it toke about 4 min, now 3 sec.
so there is loth diffrence, because we are not writting to disk, and
instead we are
writting to memory.

so would to thank all

Jan 12 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
5
by: sj | last post by:
Hi, I am new to C and learning it. could someone help me to understand the following code's output? I am having hard time understanding how n2 values like that?. To me all n2 values are equal to...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
2
by: Bowlderster | last post by:
Hi,all. I produce a function to analysis the test data,which is wave signal, and stored as array a. I want to figure out how many times it upcrosses zero,which means that when a<0,and a>0, it...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
0
rnd me
by: rnd me | last post by:
Purpose: Allows you to create "presets" for text form inputs. "Lightweight and simple to setup, it adds a lot of convenience for ~1kb of code." Only one function, two parameters: First...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.