473,508 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to embed and format sql in VBA

subedimite
21 New Member
This is the first time I am trying to call SQL in VBA. I am trying to read the result of the SQL in a recordset.

My Code Looks like this:
Expand|Select|Wrap|Line Numbers
  1. Dim dbs As Database
  2. Dim rs As DAO.Recordset
  3. Dim strSQL As String
  4. Set dbs = CurrentDb
  5.  
  6.  
  7. strSQL = "SELECT MyTests.Preconditions FROM QryTestFailure, MyTests WHERE ((([qryTestFailure]![TestID])=[MyTests]![RecNum]));"
  8.  
  9. Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)
  10. NoOfRecords1 = rs.RecordCount
  11.  
I have directly copied SQL from the Query design, with the double quotes added at the start and the end. The code compiles but does not work. The SQL itself is working while called from Access query object. The query selects data from another query ‘queryTestFailure’ and table ‘MyTests’ (I should have called this ‘tblMytests’:) ). Reading some articles in the web it seems like I need to modify the SQL string inserting the symbols: doublequotes, ampersand and single quotes. Why do I need to do that, Could I please have some help on it how is done?


Thanks
Jun 2 '10 #1
5 2638
Delerna
1,134 Recognized Expert Top Contributor
Your code looks OK to me

Why do you say it doesn't work ?
I have a feeling I know why but I will wait for you to answer
Jun 2 '10 #2
subedimite
21 New Member
Well, I am saying it does not work because when I run the query seperately, I end up with 12 records. In the above code rs.RecordCount is returning 1. Also when I use GetRows method to read the recordset ,I am ending up with nothing. Am I doing something silly:) or missing something obvious?
Jun 2 '10 #3
subedimite
21 New Member
Hi, When I restarted the database it was working.


Just was not confident as it was pretty much the first time, I did this.

However when I reset the code that’s where it seems to be the problem. The 'rs.RecordCount ' now returns 6 which is a right number; However NoOfRecords does not get assigned that value. It is showing the value of 1 (Very wierd). It seems, this all is due to lack of ' error handling'

I need to have a look at it bit deeper.
Jun 2 '10 #4
Delerna
1,134 Recognized Expert Top Contributor
Thought that would be the answer

The thing to be aware of here is, immediately after a recordset has been opened rs.recordcount will not return the number of records

it only returns 1.
I am not sure why but a little research would reveal that

Try this
Expand|Select|Wrap|Line Numbers
  1. Dim dbs As Database 
  2. Dim rs As DAO.Recordset 
  3. Dim strSQL As String 
  4. Set dbs = CurrentDb 
  5.  
  6.  
  7. strSQL = "SELECT MyTests.Preconditions FROM QryTestFailure, MyTests WHERE ((([qryTestFailure]![TestID])=[MyTests]![RecNum]));" 
  8.  
  9. Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset) 
  10. rs.MoveLast
  11. NoOfRecords1 = rs.RecordCount 
  12. msgbox NoOfRecords1 
  13.  
Jun 2 '10 #5
subedimite
21 New Member
@Delerna
Hi Thanks for that:

I got the answer; RecordCount Method can only count all the records when it fully moves to the last Record. I hope I am right.

But this happened only when I read the recordset form SQL. When I read it directly from the table or Query it has no problem.
Jun 3 '10 #6

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

Similar topics

0
3098
by: Wolfgang Schwanke | last post by:
Dear usenet, I'm having the following small problem. I've been ask to add some Quicktime panoramas to a website. The author of the panoramas has made two versions of each: One in MOV format,...
10
6288
by: Anna | last post by:
Hi all. I don't have a very deep understanding of CSS and this question might sound stupid for you. Sorry for that. Can the overflow CSS property be applied to the embed tag? I have an embed...
0
1377
by: Jin P via .NET 247 | last post by:
how you guys embed an audio file (let say wav format) in a resx file? I can embed images but somehow when I try to embed audios, it said out of memory. -------------------------------- From: Jin...
8
3070
by: Cowan Services | last post by:
My apologies if this has been covered before, but I was unable to download messages before 11/23/05. I am trying to bring some HTML pages w/embedded .WMV files into W3C compliance. Right now,...
24
6230
by: Manuel | last post by:
Is it possible to embed an image, like a company logo in a CDOSYS generated message? If yes, I´ll apreciate some code sample. I´ve been able to format messages in html the way I like, but I...
6
1477
by: emorgoch | last post by:
Hi there. I'm currently working on a project involving rebuilding our local intranet site. The site was originally developed using frames, defining a header, sidebar, and content area, which...
8
13650
by: CodeLeon | last post by:
Hi, All. I am creating a setup program. The way it works is that the user creates their setup info, my program generates the C# code for a setup executable, embeds the xml file containing the info...
0
3930
by: =?Utf-8?B?Tml5YXpp?= | last post by:
Hi all, Does anyone know how to embed the Excel file in VB.NET 2005 Resources? Here is what I am doing at the moment: 1) In my VB.NET 2005 project code I load the excel with it path into...
2
8047
dream party
by: dream party | last post by:
Inserting a Flash (SWF, FLV) file into HTML web page is already an old and familiar thing to all of us. It is a rather non-flexible thing that just to edit some options in the template. However, I...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7383
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...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
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
5627
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,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.