storing SQL results in a var

shrive22

Board Regular
Joined
Jun 10, 2002
Messages
120
Does anyone know how to store the results from an sql statement in vb code in a variable. The resluts from the query are going to change based upon what the user chooses on my form??

thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You may shoot me for offering, but I'd (mis)read this original message and proposed a quick little reply for a completely different situation.

But, since I went and spent the 10 minutes, I'll throw it down right here in case anybody wants to use something similar.

The question I tried to answer was, how would you read the contents of an entire table into a variable via SQL and VBA. This is pretty basic stuff but like I said, since I just wrote it down from scratch, and *somebody* might need it, here ya go.

Code:
Public Function Readtbl()
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim astrStore() As String
Dim x, intMax As Long    ' Counter

Set dbs = CurrentDb()

strSQL = "Select * from tblName"

Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)

With rs
  .MoveLast
  intMax = .RecordCount
  ReDim astrStore(intMax)
  .MoveFirst
  x = 1
  Do Until rs.EOF
    astrStore(x) = .Fields(0).Value            'First Column is Zero
    'strarrStore(x) = !SpecificFieldName       'Normal way of referencing fields
    x = x + 1
    .MoveNext
  Loop
End With

Set rs = Nothing
Set dbs = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,221,532
Messages
6,160,381
Members
451,643
Latest member
nachohoyu

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