Code problem

Nero

New Member
Joined
Mar 5, 2002
Messages
27
Hi,
I have a command button with code behind it which retrieves data from an MS Access database and returns it to a worksheet using MS Query.The query retrieves data that is relevent to a specific date. If I edit my code and add the date that I want to use the query works fine.
What I am trying to do is by means of an input box the user to enter the date they wish to use in the query.
The code will then use the date entered into the variable to run the query. The code keeps crashing when it uses the variable.
When I revert to entering the date directly into the code it works fine again.
Could anyone tell me what I am doing wrong??
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Nero,
I'm really shooting in the dark, here. Here's one thought: Could you be mixing types of variables? ie, is date in your query statement part of a string, while you are trying to input a Date variable? If that's the case, declaring your input box variable as a string may solve your problem.
Tom
 
Upvote 0
Thanks for your reply Tom.
I Have tried declaring it as a date and as a string but it still keeps crashing.
 
Upvote 0
Hi

You may have to use the DateSerial function to get Excel to recognise your date:

Dim MyDate as Date

MyDate =Range("A1")

MyDate = dateserial(Year(MyDate),Month(MyDate),Year(MyDate))


It would also help if you could paste the bit of code where you are parsing your date.
 
Upvote 0
Dave,
Here is the code that works fine.
What I want to do is replace the date (2002-02-27)with the variable.
Sorry it looks very confusing when I pasted it here.
___________________________________________

Dim MyDate As Date

Sheets("McMon Data").Select
MyDate = InputBox("Enter the date for the day of the data you are trying to retrieve", "Message")
Range("A2").Select
With Selection.QueryTable
.Connection = Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=M:DatabaseMachMon.mdb;DefaultDir=M:Database;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTime" _
), Array("out=5;"))
.CommandText = Array( _
"SELECT ProdDetails.MC, ProdDetails.Shift_ID, ProdDetails.Shift_Pat, ProdDetails.Date, ProdDetails.T5, ProdDetails.T6, ProdDetails.T7, ProdDetails.T8, ProdDetails.T15, ProdDetails.T16, ProdDetails.T17" & Chr(13) & "" _
, _
"" & Chr(10) & "FROM `M:DatabaseMachMon`.ProdDetails ProdDetails" & Chr(13) & "" & Chr(10) & "WHERE (ProdDetails.Date={ts'2002-02-27 00:00:00'}) AND (ProdDetails.Shift_Pat=1) AND (ProdDetails.Shift_ID In ('A','B'))" & Chr(13) & "" & Chr(10) & "ORDER BY ProdDetails.Shift_ID, ProdDetails.Shift_Pat, ProdDetails.MC")
.BackgroundQuery = True
End With
 
Upvote 0
Not too sure but try:

MyDate = _
Application.InputBox("Enter the date for the day of the data you are trying to retrieve", "Message", , , , , , 1)
If Not IsDate(MyDate) Then Exit Sub
MyDate = DateSerial(Year(MyDate), Month(MyDate), Day(MyDate))
 
Upvote 0
Dave,
I have tried your suggetion.
The code does not crash now but is exiting after the date has been entered via the exit sub.
Does this mean the date format is wrong?

Thanks for your help.
 
Upvote 0
Yes, I would say so!

Let's try this instead

MyDate = _
Application.InputBox("Enter the date for the day of the data you are trying to retrieve. Format should be: yy/mm/dd", "Message", "yy/mm/dd", , , , , 1)
MyDate = DateSerial(Year(MyDate), Month(MyDate), Day(MyDate))
If Year(MyDate) = 1900 Or MyDate = "12:00:00 AM" Then Exit Sub



I have purposely used the Application.InputBox with Type 1 as this will only accept a number.
 
Upvote 0
Dave,
It is still the same as before.
The code does not exit where I thought.The debug goes through all of the code but does not run the query.
 
Upvote 0
Nero,
Okay, here's another stab. Insert code such as:
Dim dayte As Date, mydate As String
Dim datinput As String
mydayte = InputBox("Enter your date, pulease.")
mydate = Format(mydayte, "YYYY-MM-DD HH:MM:SS")
datinput = "{ts'" & mydate & "'}"

'and then, modify your line of query string as follows..
"WHERE (ProdDetails.Date=" & datinput & ") AND


Tom
 
Upvote 0

Forum statistics

Threads
1,214,395
Messages
6,119,265
Members
448,881
Latest member
Faxgirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top