I am working on designing a asp form which will query an Access database (mdb). The outcome of the form is to query data results between a date range (date from and date to), and output the results on the asp page below. When I try and run the query I receive the following error:
Microsoft JET Database Engine error '80040e07'
Syntax error in date in query expression 'name = 'firstname.lastname' And date = ##'.
Note: firstname.lastname correspnds to the real person.
The code it points to two lines which appear as follows:
rstDATA.open "Select * From xxx Where name = '" & username & "' And date = #" & request.Form("ddDate") & "#",cnn, adOpenKeyset, ,adLockReadOnly %>
rstDATA.open "Select * From xxx Where name = '" & username & "' And date = #" & request.Form("ddDate") & "#",cnn, adOpenKeyset, ,adLockReadOnly %>
where xxxx corrsponds to the table.
It seems there is no value for ddDate provided. Are you sure you have a html element in your form that has a name attribute with "ddDate" as it's value?
Related
I have FrmDt and ToDt as datetime column in sql server2000. I am comparing dates in these columns with two datetimepicker like this-
SQL query :
SqlCmd = New SqlCommand("SELECT DISTINCT AllwnsDedId FROM vwGetEmpCntrctWisePaymnt
WHERE Type=1 AND Emp_ID='" & txtEmpID.Text & "'
AND FrmDt>='" & dtp1dtp1.Value.Date & "' AND ToDt<='" & dtp2dtp1.Value.Date & "'", Sqlconn)
In Query Analyzer, I tried :
FrmDt>='01/21/2015' AND ToDt<='01/21/2016'
Its giving me correct output. But I am not getting the output in Vb.net? Anything missing?
In order for this to work reliably you need to use proper Date datatypes. Change your SQL to something like;
WHERE FrmDt>=#FrmDt AND ToDt<=#ToDt
then add 2 paramater objects to the command, one for each date. Your code should end up something like this;
SqlCmd = New SqlCommand("SELECT DISTINCT AllwnsDedId FROM vwGetEmpCntrctWisePaymnt
WHERE Type=1 AND Emp_ID='" & txtEmpID.Text & "'
AND FrmDt>=#FrmDt AND ToDt<=#ToDt", Sqlconn)
SqlCmd.Parameters.Add("#FrmDt", SqlDbType.DateTime).Value = dtp1dtp1.Value.Date
SqlCmd.Parameters.Add("#ToDt", SqlDbType.DateTime).Value = dtp2dtp1.Value.Date
I'm using ADO in Excel 2007 VBA to return data from SQL Server. I'm aggregating lots of Select statements using Union All.
It's working fine for up to 110 aggregated Select statements, but at 115 of them I get the ADO error "Unspecified Error". At 110 Select statements it's returning about 7,000 rows and 255 columns, so its not an unusually large ADO recordset.
I thought ADO was probably passing on an error from SQL Server just as Excel VBA passed it on from ADO (I know that the error is from ADO, not VBA, because the error is shown in the ADO Connection object's Errors collection), so I sent the problem SQL statement with the 115 Select statements to my colleague who owns the SQL Server database being queried and asked her if she could tell me what SQL Server didn't like. However, it ran fine for her in the SQL Server query tool, and returned the expected data.
So it seems it's just ADO that doesn't like it when I go to the 115 Select statements; the SQL Server database itself is fine with it.
BTW I haven't tested with 111 - 114 Select statements; I've just narrowed it down to working fine with 110 and breaking on 115.
Any idea what the problem might be and what I can do about it?
For now I'm going to chunk it into sets of 50 Select statements as a workaround, but if it's possible to send it all at once I'd like to.
Here's the relevant code:
Public Const SQL_UNION_ALL_OPERATOR As String = " UNION ALL "
(the loop that aggregates the Select statements starts here)
stSQL = _
"select * from LikeItemExtract" & _
" where Dept = " & varLikeDept & "" & _
" and sup_name = '" & varLikeSupp & "'" & _
" and VPN = '" & varLikeVPN & "'" & _
" and Loc_Key = " & varCADLoc & "" & _
" and SUPP_COLOR = '" & varLikeColor & "'"
stSQLAll = stSQLAll & stSQL & SQL_UNION_ALL_OPERATOR
(end loop here)
'remove the final " UNION ALL ":
If Right(stSQLAll, Len(SQL_UNION_ALL_OPERATOR)) = SQL_UNION_ALL_OPERATOR Then
stSQLAll = Left(stSQLAll, Len(stSQLAll) - Len(SQL_UNION_ALL_OPERATOR))
End If
If CBool(rs1.State And adStateOpen) = True Then rs1.Close
rs1.Open stSQLAll, con, adOpenStatic, adLockReadOnly, adCmdText
iRecordsetRowCount = shAggData.Cells(1).CopyFromRecordset(rs1)
Thanks,
Greg
Ive looked around Google and can't find any working code. I want to query my database to return just results from a certain date. But the datetime column in my database has both the Date and the Time.
How would I go about doing this.
today = 15/08/2013
An example of one of the database values = 15/08/2013 02:13:18 PM
And here is my code at the moment
'Connect to database
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.ACE.OLEDB.12.0"
conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")
'Get the current date
today = Request.Cookies("sdate")
response.write today
sql = "SELECT * FROM sales WHERE saleDate =" & today
Set rs = conn.Execute(sql)
if rs.EOF then
response.write "Error!"
end if
Dates are passed as strings. Wrap them in single quotes.
sql = "SELECT * FROM sales WHERE saleDate = '" & today & "'"
Hmm, I used this in some other code which seemed to work.
DATEVALUE(columnName)
If you know that today consists of only the date, you could concatenate the time and do something like this:
sql = "SELECT * FROM sales WHERE saleDate >= '" & today & " 00:00:00' AND today <= '" & today & " 23:59:59'"
You could use one of VBScript's date functions to guarantee the contents of today are only the date if you want to be sure. Something like:
today = FormatDateTime(today,vbShortDate)
I've been working on this script for the last week or so and i'm having major problems trying to understand why it's not working right.
I've got some checkboxes link to email address and all i need is for the corresponding username to go with that email. This script is for users that have registered for different newsletters.
Here is my coding
Set conn = Server.CreateObject("ADODB.Connection")
conn.open connStr
emails = Request("emaillist")
emails = Replace( emails, "'", "''" )
emails = Replace( emails, ", ", "','" )
strSQL = "SELECT name, email FROM emails WHERE email IN ('" & emails & "')"
Set rs = conn.Execute(strSQL)
namesAndEmails = rs.GetRows()
rs.close
for lnRowCounter = 0 To Ubound(namesAndEmails,2)
For lnColumnCounter = 0 To Ubound(namesAndEmails,1)
Response.Write namesAndEmails(lnColumnCounter, lnRowCounter)
Response.write "</P>"
Next
Next
This is part of the whole script, i've changed it around a bit and included the for...next for debugging.
Now for the problem, as shown in the SELECT statement 'name, email', the result completely ignores the email and give me the names only.
I've tried the SQL statement direct and it works perfect showing both name and email together but not in my ASP page. I even tried putting a * in it's place.
strSQL = "SELECT * FROM emails WHERE email IN ('" & emails & "')"
Will return the users id, name, and a few other item's from the DB but not the name and emails together, why?????
It's asp with a SQL Server database
Regards
Rick
Test Results
The values from strSQL when it's set as this:
SELECT name, email
FROM emails
WHERE email IN ('test#test.com','test1#test1.com')
This in SQL will give me the following answer
name | email
jo | test#test.com
fred | test1#test1.com
In asp the answer will be
test#test.com
test1#test1.com
I can't figure out why in SQL it will show the name and email but in ASP it will only show the email and NOT the name.
I've even tried
strSQL = "SELECT * FROM emails WHERE email IN ('test#test.com','test1#test1.com')
and this will produce in ASP all the results EXCEPT name!!!!!
I might not be understanding this completely, but if all you want to do is to output a list of names with their respective email addresses, you could try simplifying to this:
name = rs("name")
email = rs("email")
do while rs.eof <> true
response.write(name & " " & email & "<br />")
rs.movenext
loop
…at least for testing purposes. Alternatively, you could concatenate the name and email in the SQL statement into one column:
SELECT name + '|' + email as nameemail FROM emails WHERE email IN ('" & emails & "')
...will give you "name|email" that you can easily string manipulate.
I'm not 100% sure, but I guess you must swap the rank parameter of the ubound()
for lnRowCounter = 0 To Ubound(namesAndEmails,1)
For lnColumnCounter = 0 To Ubound(namesAndEmails,2)
Try
For i = LBound(namesAndEmails, 2) To UBound(namesAndEmails , 2)
Name = namesAndEmails(0, i)
Email = namesAndEmails(1, i)
Response.Write Name & " " & Email
Next
What if you "View Source" of the output webpage. Sometimes I've had errors that I couldn't see because it came out looking like an invalid tag that the renderer would silently ignore.
I have a database (MDB, Access) and I connect it to my program using an OLE object,
now I have in the db a column filled with dates (ddmmyy),
I want to search and view (in Data grid view) all the fields that has a date before a Specific Date that I define .
the code of search that I used is :
SQLstr = "SELECT * FROM tb WHERE anomber = '" & TextBox1.Text & "'"
What do I have to do?. Thanks.
use the parameters to pass the date to the query, it's more saver(no sql injection) and more perfect(it will convert the date format to the correct format)
SQLstr = "SELECT * FROM tb WHERE anomber < ?"
Command.Parameters.Add(New OleDbParameter("#anomber", TextBox1.Text))
Command.CommandText = SQLstr
Edit:
if the anomber field is the date field so the user can use < instead of =.
the OP question not clear about what he wants.
Edit2:
after executing the command you should assign the results to the grid that you are using to display the data.