Connect with Expertise | Find Experts, Get Answers, Share Insights

Use SQL in VBA to pass a number into a MsgBox

 
Join Date: Dec 2009
Posts: 2
#1: Dec 18 '09
Sorry, if covered before but I'm struggling with something I think should be very easy.

I have a form which exports data from a query. I just want to run an SQLstr that counts record in the query so as to present a Msgbox saying "x number of records exported"

The code below just returns a zero records even though it is 250!?


Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Command261_Click()
  3.  
  4.     Dim SQLStr As String
  5.     Dim NoOfDataRows As Integer
  6.  
  7.     SQLStr = "SELECT Count(Qry_Dist1_Z_Desp_Total_Cost_2.Sub_Cat) AS CountOfSub_Cat " & _
  8.              "FROM Qry_Dist1_Z_Desp_Total_Cost_2;"
  9.  
  10.     Export_No.Value = SQLStr
  11.     Export_No.Requery
  12.  
  13.     NoOfDataRows = Export_No.Value
  14.     MsgBox "Export Complete...." & vbCrLf & NoOfDataRows & " Rows Exported", vbOKOnly, "Computer Says...."
  15.  
  16. End Sub
  17.  

MMcCarthy's Avatar
E
A
C
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 11,332
#2: Dec 18 '09

re: Use SQL in VBA to pass a number into a MsgBox


This sql string doesn't do anything. If you need a count of rows in a query something like the following should work.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command261_Click()
  2. Dim db As DAO.Database
  3. Dim rs As DAO.Recordset
  4. Dim SQLStr As String
  5. Dim NoOfDataRows As Integer
  6.  
  7.     SQLStr = "SELECT Count(Qry_Dist1_Z_Desp_Total_Cost_2.Sub_Cat) AS CountOfSub_Cat " & _
  8.              "FROM Qry_Dist1_Z_Desp_Total_Cost_2;"
  9.  
  10.     Set db = CurrentDb
  11.     Set rs = db.OpenRecordset(SQLStr)
  12.  
  13.     rs.MoveLast
  14.     rs.MoveFirst
  15.  
  16.     NoOfDataRows = rs.RecordCount
  17.  
  18.     Export_No.Value = NoOfDataRows
  19.  
  20.     MsgBox "Export Complete...." & vbCrLf & NoOfDataRows & " Rows Exported", vbOKOnly, "Computer Says...."
  21.  
  22. End Sub
  23.  
By the way to enclose your code in a code box just put [code] before the start of your code and [ /CODE] (remove space) after your code.
E
C
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 409
#3: Dec 18 '09

re: Use SQL in VBA to pass a number into a MsgBox


Hi

Without knowing how you exported the records, based on your code posted, I would suggerst this as the simplest solution without using a recordset.

Expand|Select|Wrap|Line Numbers
  1. Dim NoOfDataRows As Integer
  2.  
  3. NoOfDataRows = DCount("Sub_Cat","Qry_Dist1_Z_Desp_Total_Cost_2")
  4.  
  5. MsgBox "Export Complete...." & vbCrLf & NoOfDataRows & " Rows Exported", vbOKOnly, "Computer Says...."
??

MTB
MMcCarthy's Avatar
E
A
C
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 11,332
#4: Dec 18 '09

re: Use SQL in VBA to pass a number into a MsgBox


Even simpler Mike :)
 
Join Date: Dec 2009
Posts: 2
#5: Dec 18 '09

re: Use SQL in VBA to pass a number into a MsgBox


Mike, thanks a lot, that's amazingly simple...........when you know how !

MSquared, thanks for posting tip.

Will no doubt be on again soon
Reply