473,386 Members | 1,864 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,386 software developers and data experts.

MS Access query shows "INVALID USE OF NULL" error when I call using VBA

66 64KB
MS Access query shows "INVALID USE OF NULL" error when I call using VBA, but when I run directly it works.

I tried calling the parameters value i.e. form field value before calling the query, it shows the correct data, but not sure why the error pops up. Please help me to debug.

Expand|Select|Wrap|Line Numbers
  1. MsgBox Form_frmDBC.txtProdDt.Value
  2. MsgBox Form_frmDBC.Logidas.Value
  3.  
  4. Debug.Print DLookup("[Overall]", "QryProPerc")
  5. MsgBox DLookup("[Overall]", "QryProPerc", "[ProcessorID] = '" & Form_frmDBC.Logidas.Value & "'")
My Query's SQL view is as below.

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS [Forms]![frmDBC]![Logidas] Text ( 255 ), [Forms]![frmDBC]![txtProdDt] DateTime;
  2. SELECT QryPro_Npro.ProcessorID, QryPro_Npro.ProductionDt, TimeValue(Format(([QryPro_Npro].[Productive]),"hh:nn:ss"))/TimeValue("08:00:00") AS Productive, TimeValue(Format(([QryPro_Npro].[Non-Productive]),"hh:nn:ss"))/TimeValue("08:00:00") AS NonProductive, TimeValue(Format(([QryPro_Npro].[Overall Time Spent]),"hh:nn:ss"))/TimeValue("08:00:00") AS Overall, 1-[Productive] AS ProdBalTime, 1-[QryPro_Npro].[Non-Productive] AS NPBalTime, 1-[QryPro_Npro].[Overall Time Spent] AS OABalTime
  3. FROM QryPro_Npro
  4. GROUP BY QryPro_Npro.ProcessorID, QryPro_Npro.ProductionDt, TimeValue(Format(([QryPro_Npro].[Productive]),"hh:nn:ss"))/TimeValue("08:00:00"), TimeValue(Format(([QryPro_Npro].[Non-Productive]),"hh:nn:ss"))/TimeValue("08:00:00"), TimeValue(Format(([QryPro_Npro].[Overall Time Spent]),"hh:nn:ss"))/TimeValue("08:00:00"), 1-[Productive], 1-[QryPro_Npro].[Non-Productive], 1-[QryPro_Npro].[Overall Time Spent]
  5. HAVING (((QryPro_Npro.ProcessorID) Like [Forms]![frmDBC]![Logidas]) AND ((QryPro_Npro.ProductionDt) Like [Forms]![frmDBC]![txtProdDt]));
Jan 26 '15 #1

✓ answered by twinnyfo

If you have a Global Variable, the only way to use it in a Query is to also create a Public Function that calls it. Example:

Public Standalone Module:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public gstrMyText as String
  5.  
  6. Public Function GetMyText()
  7.     GetMyText = gstrMyText
  8. End Function
Then, in your forms:

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtMyText_AfterUpdate()
  2.     gstrMyText = Me.txtMyText
  3. End Sub
In your query:

Expand|Select|Wrap|Line Numbers
  1. SELECT GetMyText() AS MyTextValue ...
This is where a TempVars might be more useful.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtMyText_AfterUpdate()
  2.     TempVars.Add "MyTextString", Me.txtMyText.Value
  3. End Sub
and then in your query:

Expand|Select|Wrap|Line Numbers
  1. SELECT TempVars("MyTextString") AS MyTextValue ...
You must be sure to use the .Value property of your Controls, as TempVars (for some reason) wants to assign the control itself to your variable if not.

Hope this hepps!

9 2225
twinnyfo
3,653 Expert Mod 2GB
johny,

For whatever reason, Access is not a fan of using references to Forms when it calls Queries in VBA (I've run across your same error!).

One way to do it is to gather the values on the form using variable in your procedure first, then create the query using those variables. Another is to use global variables, which are assigned when the values on the form are updated. You could also make use of the TempVars variables, which have some usefulness in such situations.

Hope this hepps!
Jan 26 '15 #2
johny6685
66 64KB
Thanks Twinnyfo, let me try with and get back.
Jan 26 '15 #3
johny6685
66 64KB
Hi Twinnyfo, I have quick question here, Please confirm how to call the variable in Query?
Jan 26 '15 #4
NeoPa
32,556 Expert Mod 16PB
Have you checked references in the SQL to the parameters? Is there any such reference which would fail if it were replaced by Null?

If so then that is an error and needs to be rectified. Failing isn't aberrant behaviour at all but exactly how it's supposed to work.
Jan 26 '15 #5
twinnyfo
3,653 Expert Mod 2GB
If you have a Global Variable, the only way to use it in a Query is to also create a Public Function that calls it. Example:

Public Standalone Module:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public gstrMyText as String
  5.  
  6. Public Function GetMyText()
  7.     GetMyText = gstrMyText
  8. End Function
Then, in your forms:

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtMyText_AfterUpdate()
  2.     gstrMyText = Me.txtMyText
  3. End Sub
In your query:

Expand|Select|Wrap|Line Numbers
  1. SELECT GetMyText() AS MyTextValue ...
This is where a TempVars might be more useful.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtMyText_AfterUpdate()
  2.     TempVars.Add "MyTextString", Me.txtMyText.Value
  3. End Sub
and then in your query:

Expand|Select|Wrap|Line Numbers
  1. SELECT TempVars("MyTextString") AS MyTextValue ...
You must be sure to use the .Value property of your Controls, as TempVars (for some reason) wants to assign the control itself to your variable if not.

Hope this hepps!
Jan 26 '15 #6
johny6685
66 64KB
Yes NeoPa, i check before the query executes the parameters does have the values but don't know how it gets nul value while executes.
Jan 26 '15 #7
johny6685
66 64KB
hi Twinnyfo, I will try this one
Jan 26 '15 #8
johny6685
66 64KB
Thanks Twinnyfo, I tried declaring the global variable and it worked.. thanks Bro... :-)
Jan 26 '15 #9
twinnyfo
3,653 Expert Mod 2GB
Glad I could be of some help!
Jan 27 '15 #10

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

Similar topics

0
by: Mara | last post by:
Hi, I have very serious problem with query in Access. I have, in my DB, a lot of forms with some buttons that I use to do query. When I press that buttons and query start I have "Invalid...
1
by: Wayne Aprato | last post by:
I have a couple of Access 2000 and 2002 databases which have exhibited the following behaviour. The mdb works fine but the mde causes access to shut down on an error when a particular query is...
0
by: Denile | last post by:
HI...everyone I meet a problem when I call .net assembly in ATL control. It's works well when I call ATL Control in IE of my computer, but when I copy this ATL Control to others computer.It's pop...
1
by: Tsair | last post by:
Excel.ApplicationClass oxl = new Excel.ApplicationClass(); oxl.Version The exception error message "old format or invalid type library. (Exception from HRESULT : 0x80028018...
4
by: wqyuwss | last post by:
Hi, We have several core dumps in our product. These core dump can be reproduced in the same place. That is system function call std::basic_istream<char,std::char_traits<char>>::getline. The...
1
by: suslikovich | last post by:
Hi all, I am getting this error when insert values from one table to another in the first table the values are varchar (10). In the second they are datetime. The format of the data is mm/dd/yyyy...
0
by: jtsl | last post by:
I have a project where I need to save the form details into Ms Access database. Form consists of text box, select box, and files selection to upload to a table in the database. In understood that in...
1
by: AccessBeetle | last post by:
SELECT DISTINCT .Contact1Index, Contacts., Contacts., Contacts., Contacts., Contacts.prefix, Contacts.Suffix, Contacts., Contacts., Contacts.city, Contacts.state, Contacts.zip, ., tblStatusCloseout.,...
1
by: janiecarter | last post by:
Hi all, I have a query called and I want to rank each customer grouped on the field and then by descending order on . I have found many solutions where I can use a subquery to do this eg: ...
4
by: peter Lu | last post by:
Hi all I would like to remove weekend and holidays from my access query: Here is the function i'm using. I tried using a exclude 1&7 for sat and sunday but it doesn't work... Days...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.