473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query returning value in Access query builder but null in VBA

171 New Member
Hi I am running the following query in access query builder. The name of the query is "qryTempRecEFTB ankedMaxDate":

Expand|Select|Wrap|Line Numbers
  1. SELECT Max(qryTempRecEFTBanked.Dt) AS MaxOfDt
  2. FROM qryTempRecEFTBanked;
  3.  
it returns a date which exactly what I wanted, but when I run this code in VBA:

Expand|Select|Wrap|Line Numbers
  1. strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
  2.     rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
  3.     With rst
  4.     If rst.EOF And rst.BOF Then MsgBox "The recordset is blank"
  5.    dtBankMax = !MaxOfDt
  6.     End With
!MaxOfDt returns as Null, even though rst.EOF = false and also rst.BOF = False. I don't know whether it helps but "qryTempRecEFTB ankedMaxDate" is based on the query "qryTempRecEFTB anked" the Sql for "qryTempRecEFTB anked" is as follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT Sum(tblBank.Amount) AS SumOfAmount, tblBank.Dt, Val(Right(nz([SerialID],0),1)) AS ConsID, nZ([RecID],0) AS ReconID, tblBank.Desc
  2. FROM tblBank LEFT JOIN tblReconciled ON tblBank.BankID = tblReconciled.BankID
  3. GROUP BY tblBank.Dt, Val(Right(nz([SerialID],0),1)), nZ([RecID],0), tblBank.Desc
  4. HAVING (((Sum(tblBank.Amount))<>0) AND ((tblBank.Dt)>#6/30/2008#) AND ((Val(Right(nz([SerialID],0),1)))=4 Or (Val(Right(nz([SerialID],0),1)))=5 Or (Val(Right(nz([SerialID],0),1)))=7) AND ((nZ([RecID],0))=0) AND ((tblBank.Desc) Like "*HANDYWAY*"))
  5. ORDER BY tblBank.Dt;
  6.  

Many Thanks
Jan 6 '09 #1
14 3781
NeoPa
32,567 Recognized Expert Moderator MVP
Can't see much wrong there :(

Check out the rst.Open (line #2) for correct parameters, and make sure that rst is defined as DAO.Recordset if you need that or ADODB.Recordset otherwise.

It could simply be the .Open line not working as expected. Check it out in the debugger (Debugging in VBA).
Jan 7 '09 #2
ADezii
8,834 Recognized Expert Expert
@iheartvba
Try:
Expand|Select|Wrap|Line Numbers
  1. Dim strSqlMazdate As String
  2. Dim rst As ADODB.Recordset
  3. Dim cnn As ADODB.Connection
  4.  
  5. Set rst = New ADODB.Recordset
  6. Set cnn = CurrentProject.Connection
  7.  
  8. strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
  9.  
  10. With rst
  11.   .Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
  12.     If .BOF And .EOF Then
  13.       MsgBox "The recordset is blank"
  14.     Else
  15.       MsgBox !MaxOfDt
  16.   End If
  17. End With
  18.  
  19. rst.Close
  20. Set rst = Nothing
Jan 7 '09 #3
iheartvba
171 New Member
I have tried all of the above suggetions. Here are some snipets of my code (if the following code is run in my opinion the code should still work)

Expand|Select|Wrap|Line Numbers
  1. Public cnn As ADODB.Connection
  2. Public rst As New ADODB.Recordset
  3. Private Sub cmdMaxDate_Click()
  4. Set cnn = CurrentProject.Connection
  5. Dim strSqlMaxDate As String
  6. strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
  7.     rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
  8.     With rst
  9.     If rst.EOF And rst.BOF Then MsgBox "Empty Recordset"
  10.     dtBankMax = !maxofdt
  11.     End With
  12. End Sub
  13.  
Jan 7 '09 #4
ADezii
8,834 Recognized Expert Expert
@iheartvba
Expand|Select|Wrap|Line Numbers
  1. If rst.EOF And rst.BOF Then MsgBox "Empty Recordset"
  2.   'You are attempting to retrieve a Field from a Field in an
  3.   'Empty Recordset, move to Else Clause
  4. Else
  5.   'You've assigned the Value of !maxofdt to dtBankMax, but where
  6.   'is it Declared, and how is it displayed/utilized?
  7.   dtBankMax = !maxofdt
  8.     MsgBox dtBankMax        'Verify?
  9. End If
Jan 7 '09 #5
FishVal
2,653 Recognized Expert Specialist
Just a thought.

Could it be synchronization problem?
Did you try to use MoveLast method to force record fetch in recordset object?
Did you try to break the code execution after recordset has been opened and:
a) run code in step mode?
b) inspect rst variable in Watch window?

Regards,
Fish.
Jan 7 '09 #6
NeoPa
32,567 Recognized Expert Moderator MVP
@NeoPa
I don't know if you overlooked my earlier post, but checking up from work (where I now have easier access to the information required) it seems that the Open method is a specifically ADODB.Recordset method. This means that unless your rst variable has been defined as ADODB.Recordset (We can't tell as you haven't shared this information) this code will not work (as Access uses DAO generally by default - at least in the older versions). DAO & ADODB do behave differently. ADODB will have different characters for wildcards for instance. This will behave in a different way from the way you would expect Access to behave (For the standard Access ways use DAO).

When I checked the Help system for the Open method of an ADODB recordset the first parameter (Source) did not list a QueryDef as a possible value. Do you have any reason to suppose this is working as you intend?
Jan 7 '09 #7
iheartvba
171 New Member
Hi
Okay there are 2 posts I would like to attention Post 7 and Post 6:

Post 7:
"...This means that unless your rst variable has been defined as ADODB.Recordset (We can't tell as you haven't shared this information)... "

A: Please see line 2 of the code in Post 4, it shows that the rst variable has been defined as New ADODB.Recordset .

Post 6:
Q:Could it be synchronization problem?
Did you try to use MoveLast method to force record fetch in recordset object?

A: I used the query which "qryTempRecEFTB ankedMaxDate" is based on. The name of the query I used is "qryTempRecEFTB anked" (see post 1 for the Sql) but even before I could use the .movelast function, it was coming up as an empty recordset I.E. rst.bof =True and rst.eof =True.

Then I tried to use the actual Sql code but it is giving me the error "Query does not include xxx as part of an aggregate function"
My code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Public cnn As ADODB.Connection
  2. Public rst As New ADODB.Recordset
  3. Private Sub cmdMaxDate_Click()
  4.  strSqlMaxDate = "SELECT Sum(tblBank.Amount) AS SumOfAmount, tblBank.Dt, " & _
  5. "Val(Right(nz([SerialID],0),1)) AS ConsID, nZ([RecID],0) AS ReconID, tblBank.Desc " & _
  6. "FROM tblBank LEFT JOIN tblReconciled ON tblBank.BankID=tblReconciled.BankID " & _
  7. "GROUP BY tblBank.Dt, Val(Right(nz([SerialID],0),1)), nZ([RecID],0), tblBank.Desc " & _
  8. "HAVING ((((Sum(tblBank.Amount)) <> 0) And ((tblBank.Dt) > #6/30/2008#) And " & _ 
  9. "((Val(Right(nZ([SerialID], 0), 1))) = 4 Or (Val(Right(nZ([SerialID], 0), 1))) = 5 " & _
  10. "Or (Val(Right(nZ([SerialID], 0), 1))) = 7) And ((nZ([RecID], 0))=0) And ((tblBank.Desc) Like '* HANDYWAY *'))) " & _
  11. "ORDER BY tblBank.Dt;"
  12.     rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
  13.     With rst
  14.         If rst.EOF And rst.BOF Then
  15.         MsgBox "empty recordset"
  16.         Else
  17.         dtBankMax = !Dt
  18.         MsgBox dtBankMax
  19.         End If
  20.     End With
  21. End Sub
  22.  
Jan 7 '09 #8
ADezii
8,834 Recognized Expert Expert
Would it be possible to E-Mail a subset of the Database as an Attachment where we can visually see what is going on? The data would not have to be real, just representative.
Jan 7 '09 #9
iheartvba
171 New Member
yes whats your e-mail address?
Jan 8 '09 #10

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

Similar topics

8
3368
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date order. So I need to write a query that retrieves all the information for one record, lets say I want to query on the main table any entry that is for the 2004-01-06 and this date is also in a field called 'Date' in the other tables, how do I go...
2
9620
by: Eric J Owens | last post by:
TIA for any help! I have an a2k mdb front end using sql server 2k on backend. my form show router port assignments, when an assignment is deleted, there is a button that is supposed to set the fields to null. All my text fields, though, end up being updated to a '0' or '-1' instead of a null value. The sql server is set to allow nulls in these fields. here is my code: (each docmd statement is on a single line)
1
1942
by: Ike | last post by:
I have a simple query of joined tables that is failing to give me any rows of data (though, in checking by hand, it certainly should). Essentially, I am trying to return all rows from `ups` that have `floattime` not equal to it's default value of '0' (please not, in this db dates and times are saved as varchars - so essentially I am comparing strings) SELECT ups.date,associates.branch ,associates.username ,ups.time ,ups.floattime...
4
1697
by: Jim | last post by:
I have a form named FindData that has a field on it named txtUID. If a person types in an ID and clicks the search button, I want the query to find all the records in the table MAIN that have the matching ID in a column named "UserID"... no problem there. If they don't key in an ID on the form, I want it to find all the records in the table MAIN that have something entered into the "UserID" field, but omit the records that have blank...
5
2403
by: geotso | last post by:
Here is the scenario: 1. I have a table (tblCalendar) with the following fields: caldID caldDate caldTitle caldInfo nWinW nWinH
3
17037
by: sbowman | last post by:
I'm trying to get a query in access to give me a parsed string if the field contents are not null and the value from another field if the field contents are null. I have the following in the expression builder: ADID: iff(<>"", (Left$(,InStr(1,,"@")-1)), ()) I keep getting the error "undefined function 'iff' in expression"
2
2307
by: me | last post by:
I have written a query that joins four or five tables. One table has 30,000 rows. Another table has only 200. I want to only return the 200 or so rows in the smaller table and columns from the other tables where data in the surrogate keys match, without returning the other data. It is an outer join, (for four tables, which one is the left and which one is the right?) and how do you define it in design mode in Msft Access Queries, as...
4
1772
by: Stan | last post by:
I am running in ACCESS 2003 a database with a single table. It records service rendered to clients of a food pantry. As each client is served the date is entered into 1-6 fields SvcDate1, SvcDate2 etc. I would like to query certain data for monthly periods. The date of the month may appear in any one of the six fields. I now am running the query for each of the fields separately. Is there any way I can write a query that will respond...
2
5592
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT cal_date, holiday_ID from Calendar, holiday_tbl WHERE (((calendar.cal_Date) Between . And .)) And Email_sent=0 AND Staff_ID=" & Staff_ID This works fine.
0
8266
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8505
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.