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

Assigning the output of a query to a variable for use in VBA

jeremystocker
In the code below I am attempting to use the result of SQL or query to check if the value exists in a table.

Me.CaseLogIDTXT is the input field on the form
[Case Log ID] is the field being checked

thanks in advance for any help
Expand|Select|Wrap|Line Numbers
  1. Dim SearchID As String
  2. Dim QRY_ID As String
  3. Dim RST As Dao.Recordset
  4. Dim db As Database
  5. Dim product As String
  6.  
  7. Set RST = CurrentDb.OpenRecordset("SELECT [Outreach Data].[Case Log ID],
  8. [Outreach Data].[First Name], [Outreach Data].[Last Name] " & _
  9. "FROM [Outreach Data] " & _
  10. "WHERE ((([Outreach Data].[Case Log ID]) Like
  11. [Forms]![Import_FRM]![CaseLogIDTXT]));")
  12. SearchID = Me.CaseLogIDTXT
  13.  
  14. 'DoCmd.OpenQuery ("CID_QRY")
  15. QRY_ID = RST![Case Log ID]
  16.  
  17. If QRY_ID = SearchID Then
  18.   MsgBox ("Found")
  19. Else
  20.   MsgBox ("Not Found")
  21. End If
  22.  
  23. End Sub
Nov 25 '19 #1

✓ answered by ADezii

The Code can be made to work, that is not a problem. Unless I am missing something, wouldn't the following Code accomplish the same goal with only a couple Lines of Code?
Expand|Select|Wrap|Line Numbers
  1. If DCount("*", "Outreach Data", "[Outreach Data].[Case Log ID] Like '" & _
  2.           [Forms]![Import_FRM]![CaseLogIDTXT] & "'") > 0 Then
  3.   MsgBox ("Found")
  4. Else
  5.   MsgBox ("Not Found")
  6. End If

8 1925
NeoPa
32,556 Expert Mod 16PB
Hi Jeremy.

Welcome to Bytes.com.

Do you have a question in there anywhere. It's hard to know how to help without one ;-)
Nov 26 '19 #2
When I attempt to run this procedure it simply does not work. I get an error message that states "Too few Parameters" for the SQL in lines 7 -11. However, if I create an actual query with the same SQL the query will run albeit without passing the result into the procedure. I am just attempting to get this bit of code to work correctly with my database.

Thank you.
Nov 26 '19 #3
MikeTheBike
639 Expert 512MB
Hi Jeremy

I assume that [Case Log ID] is a text field.

as [Forms]![Import_FRM]![CaseLogIDTXT] will be trated as a string literal in VBA and not recongnised as the for control value, replace this
Expand|Select|Wrap|Line Numbers
  1. "WHERE ((([Outreach Data].[Case Log ID]) Like[Forms]![Import_FRM]![CaseLogIDTXT]));""
with this. Note, text criteria need delimiting with either quotes or an apostrophies.
Expand|Select|Wrap|Line Numbers
  1. "WHERE ((([Outreach Data].[Case Log ID]) Like ‘“ & me.CaseLogIDTXT & “’));"
Also if [Case Log ID] is the primary key, and the complete primary key is entered in CaseLogIDTXT, then this would be better.

Expand|Select|Wrap|Line Numbers
  1. "WHERE ((([Outreach Data].[Case Log ID]) = ‘“ & me.CaseLogIDTXT & “’));";"
Just as an aside, it's not a good idea to have spaces in field names.

HTH
Nov 26 '19 #4
ADezii
8,834 Expert 8TB
The Code can be made to work, that is not a problem. Unless I am missing something, wouldn't the following Code accomplish the same goal with only a couple Lines of Code?
Expand|Select|Wrap|Line Numbers
  1. If DCount("*", "Outreach Data", "[Outreach Data].[Case Log ID] Like '" & _
  2.           [Forms]![Import_FRM]![CaseLogIDTXT] & "'") > 0 Then
  3.   MsgBox ("Found")
  4. Else
  5.   MsgBox ("Not Found")
  6. End If
Nov 26 '19 #5
Thank you. your solution works perfectly.
Nov 26 '19 #6
Thank you for your efforts.
Nov 26 '19 #7
ADezii
8,834 Expert 8TB
You are quite welcome, good luck with your Project.
Nov 26 '19 #8
NeoPa
32,556 Expert Mod 16PB
Hi Jeremy.

It looks like there's very little left for me to say. I get tied up with work for a few hours and people come in and just answer all the questions. What can you do? Perhaps there are a couple of comments I can make just to pad out already adequate answers.
  1. I would normally use DLookup() here rather than DCount(). They both determine the answer for you but the former can stop and return the answer as soon as any matching record is dicovered whereas the latter must process through the whole table before finishing. In most cases this won't be noticeable but can be sometimes.
  2. "Like" and "=" only behave differently when wildcard characters are used. This can be useful in some situations. Here it seems clear that the "=" would be more appropriate.
  3. Your original use of [Forms]![Import_FRM]![CaseLogIDTXT] is actually perfectly appropriate. It's perfectly valid to convert it to a string literal in the code - but it isn't necessary. The Expression Service, which interprets and executes the SQL code, is perfectly capable of handling both approaches.
  4. Generally speaking we encourage our members and experts to avoid the use of word processors when working with code. However, be prepared to convert quote characters of the wrong form when you see them. It does mean that it's dangerous to use copy and paste - which is why we discourage that in the first place, but even experienced experts like MikeTheBike here, slip up sometimes. Just be prepared. Showing the code using the [CODE] tags at least makes such things easily visible so you can spot them.
Just for interest, I'll post the solution that incorporates these comments :
Expand|Select|Wrap|Line Numbers
  1. If IsNull(DLookup("*" _
  2.                 , "[Outreach Data]" _
  3.                 , "[Case Log ID]=[Forms]![Import_FRM]![CaseLogIDTXT]")) Then
  4.     Call MsgBox("Not Found")
  5. Else
  6.     Call MsgBox("Found")
  7. End If
Nov 26 '19 #9

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...
6
by: ambika | last post by:
Hello, Hello, When the below pgm is executed The output is "1"..How is that?How does "i" get that value "1"..Only one condition is true here(x<y)but z is not greater than x or y,then how does it...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
2
by: Brett Romero | last post by:
I have methods such as the following: void GetA20(somevar) { A20 = ONE } void GetA30(somevar) { A30 = ONE
5
by: Muffinthief | last post by:
Is there any way to output a variable name, without knowing the variable in advance. (Ex. Myvar = 0, could i output "Myvar" )?
6
by: Mike1961 | last post by:
Hello everyone. I have this query and this output: SELECT * , COUNT(STATE) AS strSTATE, COUNT(TYPESTATE) AS strTYPESTATE FROM tbl_MYSQL WHERE STATE = 'ABSENT'
1
by: hingwah | last post by:
Consider the following code:: void log_function(const char *str); // 3rd party logging function which don't use const std::string & as input class Test { public: std::string...
3
by: Bre-x | last post by:
I would like to output a odbc query to a text file. I have the following php code but it isnt working. <? //conneccion $conn=odbc_connect('DBA','',''); if (!$conn) {exit("Connection...
4
by: sonu2die4 | last post by:
Hi, I have the following perl script working for me.I am accesing the database from my perl script using use Net::Telnet(); package. I am not using DBI package.as I stated earlier the following...
1
by: geevim | last post by:
Hi, I am trying to pass the output of java executable to a variable. After executing the java using system command in PHP, it produces two ouput files. I just need one of them with the extension...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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
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
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...

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.