473,473 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Writting SQL including VBA variables

14 New Member
Hello,

I have a valid recordset VBA code with the following string variables (EmpNumber, RegularHours, OTHours, DTHours, RBAmount, qryName, tblName) and I am trying to create a SELECT query via db.CreateQueryDef.

My issue is writing the SQL Code incorporating these string variables as the name of the fields and tables that the query has to go searching for.

Here is the line of code I have written from how I thought it should have been written but I do not have advanced knownledge in the syntax of both languages.

PS. This is all 1 line in my VBA code, I only split it for viewing purposes

Expand|Select|Wrap|Line Numbers
  1. Set qd = db.CreateQueryDef( qryName,
  2.  
  3. "SELECT" & tblName.EmpNumber & " AS EmpNumb," & tblName.RegularHours & " AS TotalRegularHours," & tblName.OTHours & " AS TotalOTHours," & tblName.DTHours & " AS TotalDTHours," & tblName.RBAmount & " AS TotalRBAmount, IIf([" & RegularHours & "] Is Null,0,Val([" & RegularHours & "])) + IIf([" & OTHours & "] Is Null,0,Val([" & OTHours & "])) + IIf([" & DTHours & "] Is Null,0,Val([" & DTHours & "])) AS ActualHours 
  4.  
  5. FROM " & tblName & " 
  6.  
  7. WHERE (((" & tblName.EmpNumber & ") Is Not Null) AND ((" & tblName.RegularHours & ") Is Not Null))")
Jun 23 '14 #1
3 1080
twinnyfo
3,653 Recognized Expert Moderator Specialist
Beaudry93,

You mention in your question that your variables are all strings. If this is the case, your statement builder should look something like this:

Expand|Select|Wrap|Line Numbers
  1. "SELECT " & tblName & "." & EmpNumber & " AS EmpNumb, " & tblName & "." & RegularHours ...
  2.  
  3. FROM " & tblName & "  
  4.  
  5. WHERE " & tblName & "." & EmpNumber & " Is Not Null AND ...
However, I am not sure your planning is correct, as the variable "EmpNumber" implies that it is "an employee ID Number" and not the name of a field name.

Please tell us what is actually being stored in your variables first, so that we can be sure we are building your query correctly.

Also, it may be wise to step through building your query more slowly, since you say you don't have a lot of experience. This will make sure you are building correctly from the beginning, rather than building a huge query that "doesn't work" and then have no way to begin troubleshooting.
Jun 23 '14 #2
Beaudry93
14 New Member
Twinnyfo, glad I can always count on you.

Your alteration worked. The reason for text is that the query was reading the values from a linked excel table which had different value types in the same comlunm, forcing me to keep the EmpNumber as text.

And I had initially built the query through the query wizard, which is where I took the SQL text from the SQL view.

Thanks again, your help is VERY appreciated

Beaudry93
Jun 24 '14 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
Beaudry93,

Glad I could help. There are plenty of bigger brains on this forum, but I try to help where I can. Good luck on the rest of your project!
Jun 24 '14 #4

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

Similar topics

2
by: windandwaves | last post by:
Hi Folk Is it possible to get the complete address in the address bar? "http://".$_SERVER.$HTTP_SERVER_VARS; will give me the location, but not the variables after the ? Is it possible to...
2
by: Robizzle | last post by:
I'm working on a very simple script that logs the ip address, time of hit, and os/browser information and currently it works every time. The problem is getting the script included into my html...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
2
by: John Carnahan | last post by:
I have a problem writing the text from a multi-line textbox to the Sql Server db. If the user sends a vbcrlf (enter key) in the middle of the entered text, the text gets truncated at the vbcrlf...
42
by: Dooglo | last post by:
I'm new VB and programming all together, but I'm getting he hang of it. My question is; I'm writting a program to figure square feet and yards when the user inputs "Length in feet and inch (...
6
by: noridotjabi | last post by:
Is there any way in C to write source which would make an executable that excepted modifications. For instance say we have modable program that excepts plugins. That starts with a menu. 1)Add...
1
by: Santosh | last post by:
Dear All i am writting a code sending mail with attachement. i am writting code for sending mail in one page and code for attaching a file in the next page. aftet attaching a file i am taking...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
6
by: JRough | last post by:
I hate to ask an obvious question but I'm trying not to mess up anything worse than it is already. I have all these variables in- line. Can I do an include and take them off the file into an...
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...
1
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...
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
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.