473,406 Members | 2,352 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,406 software developers and data experts.

Assigning SQL result to a variable

3
Hello

I am trying to assign the result of a SQL statement (Count of recordset) to a integer variable. I am getting a type mismatch on the assignment. The SQL statement is good. What am I missing. Code below:

Dim intPMCount As Integer
Dim rcdPMCount As Recordset
Dim mySQL As String

mySQL = "SELECT Count(tblPMTemplate.PMTID) AS CountOfPMTID" & _
"FROM ((tblPMTemplate LEFT JOIN tblAsset ON tblPMTemplate.AssetID = tblAsset.AssetID) " & _
"LEFT JOIN tblAssetType ON tblAsset.AssetTypeID = tblAssetType.AssetTypeID) " & _
"LEFT JOIN tblAssetState ON tblAsset.AssetStateID = tblAssetState.AssetStateID " & _
"GROUP BY tblAssetType.AssetType, tblPMTemplate.Archive, tblAssetState.AssetState " & _
"HAVING (((tblAssetType.AssetType)=[Forms]![frmGenerateSchedule]![GetAssetType]) " & _
"AND ((tblPMTemplate.Archive)=False) " & _
"AND ((tblAssetState.AssetState)='In Use'));"

Set rcdPMCount = CurrentDb.OpenRecordset("mySQL")
intPMCount = rcdPMCount
Oct 8 '07 #1
4 2965
FishVal
2,653 Expert 2GB
Hi, Mark.

I guess no way to assign result of SQL to variable.
Better look at DCount function.
Oct 8 '07 #2
nico5038
3,080 Expert 2GB
To get the number of records from a recordset you need to use:

Expand|Select|Wrap|Line Numbers
  1.  Set rcdPMCount = CurrentDb.OpenRecordset("mySQL")
  2.   if  rcdPMCount.eof and rcdPMCount.BOF then
  3.      ' no records found
  4.      intPMCount = 0
  5.    else
  6.      rcdPMCount.movelast
  7.      intPMCount = rcdPMCount.recordcount
  8.    endif
  9.  
When you only need the value from a field in the recordset use:
Expand|Select|Wrap|Line Numbers
  1.  Set rcdPMCount = CurrentDb.OpenRecordset("mySQL")
  2.   if  rcdPMCount.eof and rcdPMCount.BOF then
  3.      ' no records found
  4.      intPMCount = 0
  5.    else
  6.      intPMCount = rcdPMCount!CountOfPMTID
  7.    endif
  8.  
Getting the idea ?

Nic;o)
Oct 8 '07 #3
Mark77
3
Thanks FishVal

I was really trying to generate the recordset with a coded recordset and then do a dLookup off the recordset. I am having trouble with the syntax on this. Can you help.
Oct 8 '07 #4
FishVal
2,653 Expert 2GB
Thanks FishVal

I was really trying to generate the recordset with a coded recordset and then do a dLookup off the recordset. I am having trouble with the syntax on this. Can you help.
Hi, Mark.
  • if you want iterate through recordset you may do it using EOF and or BOF property
    Expand|Select|Wrap|Line Numbers
    1. 'forward iteration
    2. With RS
    3.     .MoveFirst
    4.     While Not .EOF
    5.         ........
    6.         .MoveNext
    7.     Wend
    8. End With
    9.  
    10. 'backward iteration
    11. With RS
    12.     .MoveLast
    13.     While Not .BOF
    14.         ........
    15.         .MovePrevious
    16.     Wend
    17. End With
    18.  
  • if you want to retrieve recordcount for other purposes then look at Nico's suggestion

Regards,
Fish
Oct 9 '07 #5

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

Similar topics

2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
2
by: Hans-Michael Rupp | last post by:
Hello! I need to a assign a node(-set) conditionally to a varialbe like: <xsl:variable name="foo"> <xsl:if test="$bar=''"> <!-- how can I access a node here ?--> </xsl:if> <xsl:if...
16
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
1
by: Jesper DK | last post by:
Hi, I'm making a 'settings dialog' for my program with a seperate form and the ShowDialog(). However, initializing the controls with values, writing eventhandlers to store changes when e.g a...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
5
by: Manuel Graune | last post by:
Hello, while trying to learn how to program using objects in python (up to now simple scripts were sufficient for my needs) I stumbled over the a problem while assigning values to an object. ...
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.