473,406 Members | 2,369 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.

sql string with cell value from excel

374 256MB
Hi all,

I use the following string to get data from an access database within excel.

Expand|Select|Wrap|Line Numbers
  1. Set RS = Db.OpenRecordset("Select tblCheck.lTableID, tblCheck.sStaffNumber, tblError.sStaffNumber FROM tblCheck LEFT OUTER JOIN tblError ON tblCheck.lTableID = tblError.lCheckID WHERE dteCheckCompletedDate BETWEEN date() AND date()-30")
From the above sql string i want to change "date()" to reflect the date specified within an excel cell.

The cell is located at: worksheet "overall performers" cell K5 and is formatted as dd/mm/yyyy


How would the sql string look to achieve this?

Thanks
Mar 3 '10 #1

✓ answered by TheSmileyCoder

Its a regional settings thing. Im guessing your from a place that doesn't use US dates (like myself).

Try this:
Expand|Select|Wrap|Line Numbers
  1. Function MakeUSDate(x As Variant)
  2.     If Not IsDate(x) Then Exit Function
  3.     MakeUSDate = "#" & Month(x) & "/" & Day(x) & "/" & Year(x) & "#"
  4. End Function

3 4918
TheSmileyCoder
2,322 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. Dim myDate as Date
  2. myDate=Worksheets("overall performers").Range("K5")
  3. Set RS = Db.OpenRecordset("Select tblCheck.lTableID, tblCheck.sStaffNumber, tblError.sStaffNumber " & _
  4.      "FROM tblCheck LEFT OUTER JOIN tblError " & _
  5.      "ON tblCheck.lTableID = tblError.lCheckID " & _
  6.      "WHERE dteCheckCompletedDate BETWEEN " & _
  7.      "#" & myDate & "# AND #" & dateadd("m",-1,myDate) & "#") 
  8.  
A bit of explanation. The Hash # is used to denote date literals, like " is used to denote string literals.

The dateAdd will add (-1) month to myDate. If you want it to add (-30) days instead use: dateadd("d",-30,myDate)
Hope it made sense.
Mar 3 '10 #2
munkee
374 256MB
Thank you for the reply however I have hit an issue. Which I tried to explain on another forum but seems to have got everyone stuck also. What i've found is the sql statement is basically spewing out the wrong records whenever the month/day are below 12. This is basically saying the statement can not differentiate between the month and day if for example it is 01/03/2010. Here is an explenation of what I am actually doing and how I have replicated this error:


A cell looks up a value from a list box which contains "text" dates. These are produced from looking up folder names (which are named as week commencing dates) within a directory.

The cell containing the "text" date is:
01.03.2010

I convert this "text" date which is held in cell K1 in to a real date with the following procedure (split over 2 macros hence why the code doesnt stay within the same with statement) to K2:

Expand|Select|Wrap|Line Numbers
  1. With Sheets("overall performers")
  2.  .Range("k2").Value = CDate(Replace(.Range("k1"), ".", "/"))
  3.  End With
  4.  
  5.  
  6. Dim myDate As Date
  7.  myDate = Format(Worksheets("overall performers").Range("K2"), "dd/mm/yyyy")
So I have converted the "text" date in to a real date to give 01/03/2010 for the value in K2 from K1.

This value is then passed to my sql string as myDate:

Expand|Select|Wrap|Line Numbers
  1.  Set RS = Db.OpenRecordset("Select tblCheck.lTableID, tblCheck.sStaffNumber, tblError.sStaffNumber, tblcheck.dteCheckCompletedDate " & _
  2.      "FROM tblCheck LEFT OUTER JOIN tblError " & _
  3.      "ON tblCheck.lTableID = tblError.lCheckID " & _
  4.      "WHERE dteCheckCompletedDate BETWEEN " & _
  5.      "#" & myDate & "# AND #" & myDate - 30 & "#")
This string queries an Access DB for an Accuracy Check ID number, the staff number the check was done on and the staffnumber if an error was found in the check. These are then selected via their date that the check was completed on (dteCheckCompletedDate) from between myDate and myDate - 30 days.

This produces the following table:
Expand|Select|Wrap|Line Numbers
  1. lTableID tblCheck.sStaffNumber tblError.sStaffNumber dteCheckCompletedDate 63945 57222309  30/01/2010 63846 57222309 00000001 30/01/2010 
  2.  
  3. Down to:
  4.  
  5. 70806 79902612  03/01/2010 73224 87504104  03/01/2010 
As you can see, I passed myDate as being 01/03/2010 the 1st of March 2010. But my results have returned dteCheckCompletedDate 's between 30/01/2010 and 03/01/2010 .. January...

However.. if I select a different date for "myDate" such as 22/02/2010 so 22nd of February 2010 I get the following correct output:
Expand|Select|Wrap|Line Numbers
  1. lTableID tblCheck.sStaffNumber tblError.sStaffNumber dteCheckCompletedDate 53277 43721306  22/02/2010 55273 44614811  22/02/2010 
  2.  
  3. Down to:
  4.  
  5. 74642 37089111  23/01/2010 75981 83918005  23/01/2010 
So from 22/02/2010 to 23/01/2010.

I really do not understand why this is happening. The only thing I can think of is that somewhere along the line excel or sql can not differentiate between a month and a day if the month and day are sub 12.

For example:

01/03/2010 if you were not told that this was in UK or USA format it could be either march or january.

But..

If you were told 22/02/2010 or 02/22/2010 you could work out that it has to be february as months can't be greater than 12.

Any help with this is appreciated I've tried everything.
Mar 5 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Its a regional settings thing. Im guessing your from a place that doesn't use US dates (like myself).

Try this:
Expand|Select|Wrap|Line Numbers
  1. Function MakeUSDate(x As Variant)
  2.     If Not IsDate(x) Then Exit Function
  3.     MakeUSDate = "#" & Month(x) & "/" & Day(x) & "/" & Year(x) & "#"
  4. End Function
Mar 5 '10 #4

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

Similar topics

2
by: Mike Strieder | last post by:
Hi, thx for reading this entry and for your help I select a cell in Excel (C#) and want to set the Text "ABC123". But in this Cell the Text "123" should appear in Bold! How can this work ???...
6
by: XxLicherxX | last post by:
Hello everyone - new to VB.net working with Excel I am trying to populate data into cells in an Excel spreadsheet using Range.Value. No matter what cell I give as an argument to the Range...
1
by: JaxDawg | last post by:
Hey folks... I've got an interesting problem. I'm updating and displaying an excel spreadsheet (using com) from a php file. Works great in the debugger (PhpEd 4.6), but not otherwise. I assume...
1
by: Terry | last post by:
Hi Language: VBA (Excel) I am wanting to execute different functions/subs dependent on a value picked up in a cell on Excel. For example if Cell A1 has the value "name" then I want to be able...
1
by: ashok0866 | last post by:
I had created a macro to read data from an excel sheet and write the values to a text file. I had used "ActiveSheet.Range("GB" & k).Value" command to read the values from the excel. The issue...
0
by: aotemp | last post by:
Hi! Im having a bit of trouble... im trying to convert an excel spreadsheet cell value to a string! But all I get is "System.__COM" in my string variable... this.openFileDialog1.FileName =...
1
by: aotemp | last post by:
Hi, Im having a reaaally hard time with something... Im trying to read a cell of data into a String variable. It seems like such a simple task too... Get the excel spreadsheet, get the...
0
by: suresh_punniyakkodi | last post by:
Hellow Friends, I have one doubt, please help me... In Excel, i have lot of rows and coloumns, i need to read all cell values with in rows and coloumn limit... At the time...
1
by: Tony Bansten | last post by:
As fas as I heard I can write to Excel cell e.g. E53 from outside with VBS. How could such a code look like? Assume I have an Excel file aaa.xls and a worksheet "ws777" inside: How can I wread...
4
by: ravir81 | last post by:
Hi, I am currently working on excel validation using Perl. I am new to Excel validation but not for Perl. I have a question regarding one of the validation. Could anyone please tell me how to get...
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
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
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
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
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.