473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Use SQL in VBA to pass a number into a MsgBox

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.  
Dec 18 '09 #1
4 5299
MMcCarthy
14,534 Expert Mod 8TB
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.
Dec 18 '09 #2
MikeTheBike
639 Expert 512MB
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
Dec 18 '09 #3
MMcCarthy
14,534 Expert Mod 8TB
Even simpler Mike :)
Dec 18 '09 #4
Mike, thanks a lot, that's amazingly simple...........when you know how !

MSquared, thanks for posting tip.

Will no doubt be on again soon
Dec 18 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mark Dicken | last post by:
Hi All, I am trying to Pass A Collection To A FUNCTION ??? (Access 2000) I have a Class called BO (for Business Objects) Within BO I have a Function called ShowCollection Public Sub...
3
by: Don Seckler | last post by:
I have a data entry form called Draw. This form is used to enter data in the table called Draw. The table has the following fields: WholesalerID, MagID, IssueID, CopiesDist, and the index is...
3
by: Mark Horton | last post by:
I thought would be a super simple project, but I was wrong. I need Help!!!! I am using the Word.Application.Mailmerge.OpenDataSource to try and send a Dynamic Query to my stated Document.Open...
1
by: jj | last post by:
Ugh, I had to change the parameters of an ODBC connection and refreshed my linked tables which isn't so bad. But I noticed the pass-through queries didn't refresh and I've got like 20 of them. So...
2
by: Barry Wright | last post by:
Hi All, The error I am getting is 3265: "Item not found in this collection." How should I be passing in the vReturnFieldName so it will be recognized as a value in the collection? Thanks for...
0
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
3
by: ILCSP | last post by:
Hello, I'm fairly new to the concept of running action pass through queries (insert, update, etc.) from Access 2000. I have a SQL Server 2000 database and I'm using a Access 2K database as my...
9
by: skinnybloke | last post by:
Hi - I have 3 access queries which I run via 1 macro. Each of the queries now requires 2 parameters when they the run. The parameters are start and end dates. I have built the parameters...
9
by: Greg Strong | last post by:
Hello All, What is the maximum length of an ODBC pass through query? Things work fine with the code except when I try to create a view which is pretty complex in Oracle. I'm using a DSN...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.