SQL Related Question

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I know very little about SQL so pardon my ignorance here.

I know that I can create an action query SQL statement and run it using RunSQL e.g.

DoCmd.RunSQL strSQL

How do create a SQL statement for a select query? Here's what I am trying to do: -

Forms![frmData Comparison].txtMatchedSTB_BE = DCount("TestString", strSQL)

where strSQL comes from a select query. The select query SQL code from the query SQL window is

SELECT tblSTB.TestString FROM tblSFF INNER JOIN tblSTB ON tblSFF.TestString = tblSTB.TestString;

In the past I have been trying to enter the query name in the DCount function, but I would like to gently begin doing things with SQL if I can.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
RunSQL can only be used for Action Queries, as you have discovered. To Select data, you will need to use an ADO or DAO recordset. An example of a DAO recordset would be:
Code:
Public Function fGet_Records()
Dim db as DAO.Database
Dim rs as DAO.Recordset
Set db=CurrentDB
Set rs=db.OpenRecordset("SELECT Count(*) as Record_Count FROM YourTable")
You can then do something with the record(s) which are now in this recordset object. For example, place the Record_Count field into a textbox on a form:
Code:
Me.MyTextbox=rs!Record_Count
both the DAO.Recordset and DAO.Database objects take up memory, so you need to destroy them once you've finished:
Code:
Set rs=Nothing
set db=Nothing
End Function
 
Upvote 0
Great, it worked. Which is the more efficient way of doing a count in terms of speed i.e. your method or creating a query and using it in a DCount function.
 
Upvote 0
I really don't use the Domain functions that much, but from what I've read, they are the most costly way of retrieving data (stored queries are the quickest, DAO/ADO recordsets are 2nd, Domain functions are last).
 
Upvote 0

Forum statistics

Threads
1,221,631
Messages
6,160,942
Members
451,679
Latest member
BlueH1

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