Excel VBA Connection to Access

Twi78

New Member
Joined
Mar 27, 2012
Messages
18
Hi All. I currently have a workbook that has VBA functions that calculates values by looping through large amounts of data in a few large excel sheets. I want to transfer the data from these sheets into Access and then bring data into the VBA code via an array from the data in Access rather than the array being populated from the data in excel as it currently is. However I know this is possible but have no idea how to create the connection. The access DB will be situated in a public directory on a server. Can anyone show me how to create the connection string and then how I call the data from Access? Or point me to some tutorials. I have just downloaded Office 2013 in case you need to know what version I am working in.
 
Can I still create a recordset in the module and point each function to the recordset?
Not without changing the functions to look at a recordset. And if you are looping through it then sure I think that the array is quicker. It also seems like you have done all the work to process arrays so in this instance I take back what I said and suggest you continue with the array route. Perhaps you can narrow down the recordset first using the recordset Filter function.

Anyway I think you've been given some good suggestions on how to populate an array with a recordset. Rory's "obviously a MSForms.Listbox" ;) method is cracking and no doubt the best way forward.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Maybe to add data from recordset in such a manner is simple a little
oRs.Open sSQL, oConn, adOpenStatic, adLockBatchOptimistic, adCmdText

Sheets("Sheet2").Range("A1").CopyFromRecordset oRs</pre>
 
Upvote 0
Given a variant array, you can also use something like this to transpose it:
Code:
    With CreateObject("New:{8BD21D20-EC42-11CE-9E0D-00AA006002F3}")
        .Column = vData
        vData = .List
    End With

Unlike Application.Transpose it will accept text longer than 911 characters and arrays larger than 65536 rows.

OK, I think this will need breaking down for me here. Going bald from scratching my head so much! :)
 
Upvote 0
So you populate the array as you did, using GetRows(). Say your array is called vData, the code Rory provided will transpose it so now you can pass it to your function for processing.
 
Upvote 0
Thanks, I tried that and it worked apart form I have some issues with values, converting them from decimal to single so all my values are rounded?

Can I ask, If I opened a connection to Access in the module, and used the Sql string to sum up the values where it met a certain criteria defined in each of the 120 functions, would there be any performance improvements? That is instead of calling all the data into an array, then looping through the data with 120 functions?

I am also thinking that instead of having 120 functions (10 measurs for 12 machines), I can have 10 functions and introduce an extra paramter for machine.
 
Upvote 0
Thanks, I tried that and it worked apart form I have some issues with values, converting them from decimal to single so all my values are rounded?
I'm not sure what the culprit is here. It may be that you need to use a coercion function in your SQL to pull back data per the decimal data type. Are you sure it is a double/decimal data type in the database?

Can I ask, If I opened a connection to Access in the module, and used the Sql string to sum up the values where it met a certain criteria defined in each of the 120 functions, would there be any performance improvements? That is instead of calling all the data into an array, then looping through the data with 120 functions?
I reckon that would be a BIG improvement since you won't be having to loop thousands upon thousands of rows.

I am also thinking that instead of having 120 functions (10 measurs for 12 machines), I can have 10 functions and introduce an extra paramter for machine.
This also sounds like a sensible approach to me.
 
Upvote 0

Forum statistics

Threads
1,216,040
Messages
6,128,454
Members
449,455
Latest member
jesski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top