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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,223,549
Messages
6,172,988
Members
452,496
Latest member
VC777

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