Select between two dates - I get always Runtime Error 3464 - sql

I'm using Access and VBA to read some tables via DAO. I would like to read everything in my table that is between date1 and date2. I have something like this:
Dim date1 As Date
Dim date2 As Date
'...
Dim strSQL As String
Dim rs As DAO.Recordset
strSQL = "SELECT * FROM Table1 WHERE ImportData between '" & date1 & "' AND '" & date2 & "'"
Set rs = CurrentDb.OpenRecordset(strSQL) 'Runtime Error 3464
I just get a Runtime Error 3464 Data type mismatch Not sure why, if I put strSQL in a MsgBox the query I get is this:
SELECT * FROM Table1 WHERE ImportData between '15.03.2014' AND '11.03.2014'
The column 'ImportData' in my Table is also Date/Time Type, so why is this not working? Also tried without ' or using <> and so on, that is not helping. Any ideas? Thank you
Edit:
If I would use:
"SELECT * FROM Table1 WHERE ImportData between #" & date1 & "# AND #" & date2 & "#"
Then I get Runtime Error 3075 - Syntaxerror in Date

For date values, use # instead of the single quote:
strSQL = "SELECT * FROM Table1 WHERE ImportData between #" & date1 & "# AND #" & date2 & "#"

Related

Updating a date/time field in MS Access using VBA

I'm trying to update a date/time field when a new file is imported through vba.
It works for the first file, but for subsequent imports I get a type mismatch error.
If I delete the 'FileDate' field and re-insert it, it works for the first time, but after I get the error again.
Dim db As DAO.Database
Dim str_table As String
Dim str_sql As String
Dim dt As Date
str_table = "Items"
dt = CDate(Format(FileDateFromPath(file_path), "MM/DD/YYYY"))
Set db = CurrentDb()
str_sql = "UPDATE [" & str_table & "] SET [FileDate] = #" & dt & "# " & _
"WHERE [FileDate] Is Null OR [FileDate]='';"
DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=str_table, _
FileName:=file_path, _
HasFieldNames:=True ' or False if no headers
db.Execute str_sql, dbFailOnError '<-- error here's
I've tried different formats for the date (DD/MM/YYYY, changed to MM/DD/YYYY), I've included # around the date...
Deleting the field and re-inserting it works, but only for the first time...
Is there something I'm missing?
Your dt variable is a Date, not a String. Therefore the Format you apply has no effect, and when you concat the variable, it will use your local date format.
Use Gustav's CSql() function when concatenating variables with SQL.
Also, a date column can not have the value '', so you can omit that.
str_sql = "UPDATE [" & str_table & "] SET [FileDate] = " & CSql(dt) & _
" WHERE [FileDate] Is Null;"
should work.

Date filter returns an invalid character error

I want to execute a query with a date filter in Access VBA.
My first issue was inconsistent datatypes: expected DATE got NUMBER.
I assume Access stores dates as numbers just like Excel.
I found I had to use "#" between the date for the query to recognize it as a DATE. Now I am getting an INVALID CHARACTER error which means the hash # is an invalid character.
I have to pull in the negotiated costs with our vendors that don't have more than 60 days of being expired.
There is also a tblVendors where the user selects the vendor ID (ORDID) they want to pull the info from. That's why I do a loop to pull in every ORDID in that table. I don't push in all the ORDID in one try because it brings a lot of data and doing it in batches runs faster.
Sub GetMaterialCost()
Dim db As Database
Dim rsData As ADODB.Recordset, rsVendor As DAO.Recordset, rsItemCost As DAO.Recordset
Dim strQuery As String
Dim vendorNO As Long, dtDate As Date
Set db = CurrentDb
dtDate = Format(Now() - 60, "m/d/yyyy")
Set rsVendor = db.OpenRecordset("SELECT ORDID, VEN_NAME, USER_ID FROM tblVendors WHERE ACTIVE = TRUE ORDER BY VEN_NAME, ORDID")
Set rsItemCost = db.OpenRecordset("tbl_ItemCost")
ConnectBILL
rsVendor.MoveFirst
Do Until rsVendor.EOF
strQuery = "SELECT MASID, LOCATION, ITEM, ITEM_QTY, ITEM_UOM, ITEM_COST, EXP_DT " _
& "FROM ITEMMASTER INNER JOIN ORDDETAIL ON (ITEMMASTER.ITEM = ORDDETAIL.ITEM) " _
& "WHERE (LOCATION IN (AS1,AS3,AS6) AND TRIM(MASID) = '" & Cstr(rsVendor.Fields("ORDID")) & "' AND EXP_DT >= #paramDate)"
With ComBill
.CommandText = strQuery
Set rsData = .Execute(, Array(dtDate))
end with
'clears previous instance of vendor data by vendor_no if it exists
db.Execute "DELETE * FROM tbl_ItemCost WHERE MASID LIKE '*" & rsVendor.Fields("ORDID") & "*'"
'starts inserting queried data
rsData.MoveFirst
Do Until rsData.EOF
With rsItemCost
.AddNew
.Fields("MASID") = rsData!MASID
.Fields("LOCATION") = rsData!LOCATION
.Fields("ITEM") = rsData!ITEM
.Fields("ITEM_UOM") = Trim(rsData!ITEM_UOM)
.Fields("ITEM_COST") = rsData!ITEM_COST
.Fields("EXP_DT") = rsData!EXP_DT
.Update
End With
rsData.MoveNext
Loop
rsVendor.MoveNext
Loop
End Sub
Correct these two lines:
dtDate = DateAdd("d", -60, Date)
& "WHERE LOCATION IN (AS1,AS3,AS6) AND TRIM(MASID) = '" & CStr(rsVendor.Fields("ORDID") & "' AND EXP_DT >= #" & Format(dtDate, "yyyy\/mm\/dd") & "#) "
Note, that for ADO, string expressions for date values must be formatted using the ISO sequence.
If the field EXP_DT is Text:
& "WHERE LOCATION IN (AS1,AS3,AS6) AND TRIM(MASID) = '" & CStr(rsVendor.Fields("ORDID") & "' AND EXP_DT >= '" & Format(dtDate, "m\/d\/yyyy") & "') "

Query MS Access using Excel VBA, SQL BETWEEN dates query

all--
I'm attempting to use an SQL query to pull records from an Access db into an Excel VBA userform listbox using the following code:
Sub FillLBBillIDs()
'build bill ID list box with bill IDs available in database, based on client and/or date range
'<---------------------------------------------------Dimension all variables
Dim con As Object, cmd As Object, rst As Object
Dim Path As String, CName As String
Dim FromDate As Date, ToDate As Date
Dim X As Long, Y As Long
'<---------------------------------------------------Define Default Variables
X = 0
CName = AuditParametersFRM.CBOCxName.Value
FromDate = AuditParametersFRM.DTPFrom.Value
ToDate = AuditParametersFRM.DTPTo.Value
'<---------------------------------------------------Define Access connection
Set con = CreateObject("ADODB.Connection"): Set cmd = CreateObject("ADODB.Command"): Set rst = CreateObject("ADODB.RecordSet"):
Path = Sheets("AuditTool").Range("B2").Value
'<---------------------------------------------------Open Access connection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Path & ";Persist Security Info=False;"
con.ConnectionTimeout = 0: con.CommandTimeout = 0: con.Open: cmd.CommandTimeout = 0: Set cmd.ActiveConnection = con
'<---------------------------------------------------Find all bill IDs in the database which match the selected client and
'<---------------------------------------------------are within the consolidated date range
rst.Open "SELECT DISTINCT AdHocReport.[BillID] FROM AdHocReport WHERE AdHocReport.[CxName] = '" & CName & "' AND AdHocReport.[ConsolidationDate] BETWEEN #" & FromDate & "# AND #" & ToDate & "#", con, 1, 3
On Error Resume Next
rst.MoveLast
rst.MoveFirst
Y = 0
Y = rst.RecordCount
AuditToolFRM.LBBillIDs.Clear
If Not Y = 0 Then
Do Until rst.EOF
'<---------------------------------------------------Build the listbox with the acquired information
With AuditToolFRM.LBBillIDs
.AddItem
.List(X, 0) = rst![BillID]
X = X + 1
End With
rst.MoveNext
Loop
End If
rst.Close
On Error GoTo 0
con.Close
End Sub
This code works just fine if I use a greater than argument, thusly:
rst.Open "SELECT DISTINCT AdHocReport.[BillID] FROM AdHocReport WHERE AdHocReport.[CxName] = '" & CName & "' AND AdHocReport.ConsolidationDate > #" & FromDate & "#", con 1, 3
I've gone through all the pertinent questions on this site and can't find anything that works. Any ideas?
Thanks in advance!
12/08/2017 12:54
I've done more testing and it appears that the greater than query isn't working either; it's pulling all records that meet the first criteria whilst ignoring the second, even when using parentheses to enclose the second. This tells me that the issue is definitely in the date portion of the query somehow. Any help is appreciated greatly!
In Access
DATE_FIELD BETWEEN #2/2/2012# AND #2/4/2012#
is the same as
DATE_FIELD >=#2/2/2012# AND <=#2/4/2012#
When you have another AND put parathesis around the date range syntax.
rst.Open "SELECT DISTINCT AdHocReport.[BillID] FROM AdHocReport WHERE AdHocReport.[CxName] = '" & CName & "' AND (AdHocReport.[ConsolidationDate] BETWEEN #" & FromDate & "# AND #" & ToDate & "#)", con, 1, 3
In ADO you must use the ISO sequence in string expressions for date values:
... "' AND AdHocReport.[ConsolidationDate] BETWEEN #" & Format(FromDate, "yyyy\/mm\/dd") & "# AND #" & Format(ToDate, "yyyy\/mm\/dd") & "#" ...

How do you find if a record exists?

Dim mydb As DAO.Database
Dim myrst As DAO.Recordset
Dim Date1, Date2 As Date
Dim mysql As String
Dim qdf As QueryDef
Dim EmployeeID As String
EmployeeID = DLookup("lngEmpID", "tblEmployees", "Forms!frmEmployeeOrderForm.cboEmployeeName.Value = EmployeeName")
Me.EmpID = EmployeeID
PickupTime = DLookup("Pickup", "tblEmployees", "Forms!frmEmployeeOrderForm.cboEmployeeName.Value = EmployeeName")
Me.PickupDay = PickupTime
Set mydb = CurrentDb
Date1 = Me.dtmGiveAwayDate1
Date2 = Me.dtmGiveAwayDate2
mysql = ("Select tblOrders.lngEmpID, tblOrders.dtmGiveAwayDate1, tblOrders.dtmGiveAwayDate2 FROM tblOrders WHERE tblOrders.lngEmpID =" & EmployeeID & " AND tblOrders.dtmGiveAwayDate1 =" & Date1 & " AND tblOrders.dtmGiveAwayDate2 =" & Date2)
MsgBox mysql
Set myrst = mydb.OpenRecordset(mysql)
If myrst.EOF = False Then
MsgBox "You have already ordered for this PFG." & vbCrLf & "Please see Jody for help."
cmdCancel_Click
Exit Sub
End If
When I run the program I am trying to prevent employees from entering their name twice for the same dates. To test I tried to duplicate a record for the same employee and it will not trigger the myrst.eof = false and display the message box that says "You have already ordered for this PFG". Any help is much appreciated.
Thank you in advance.
Use the FindFirst method :
myrst.FindFirst "field = " & value
If myrst.NoMatch Then
//code when record doesn't exist
End If
Checking for equality with dates can be tricky. Since VBA dates can include time to the nanosecond, could it be that one side (SQL/code) has a real time, while the other has the default midnight value?
Set mydb = CurrentDb
Date1 = Format(Me.dtmGiveAwayDate1, "Short Date")
Date2 = Format(Me.dtmGiveAwayDate2, "Short Date")
mysql = ("Select tblOrders.lngEmpID, tblOrders.dtmGiveAwayDate1, tblOrders.dtmGiveAwayDate2 FROM tblOrders WHERE tblOrders.lngEmpID =" & EmployeeID & " AND tblOrders.dtmGiveAwayDate1 =" & "#" & Date1 & "#" & " AND tblOrders.dtmGiveAwayDate2 =" & "#" & Date2 & "#")
MsgBox mysql
Set myrst = mydb.OpenRecordset(mysql)
This was the code I used and it works now. Thank you.

Write date to SQL using VBA

Till today I used this function to write dates from my access form to SQL server. Today I noticed that now() function is not working anymore. I tried date() - still not working. Getdate cannot use in VBA. I need to have exact date with minutes. Any suggestions? Thank you in advance!
CurrentDb.Execute ("UPDATE CI SET D_Date = now() WHERE ID = " & Id & ";"), dbSeeChanges
Concatenation of Variables/Constants in VBA is usually performed by using the Ampersand operator. However, there a few variations depending on the data types.
If the field for which the value supplied is String they need to be enclosed within, the String literal (single or double quotes).
Dim myName As String
myName = "Paul"
strSQL = "UPDATE tableName SET TextFieldName = '" & myName & "'"
The above is not always safe, errors happen when using names with a single quotes in them. So, this is always a better way.
Dim myName As String
myName = "Paul O'Connor"
strSQL = "UPDATE tableName SET TextFieldName = " & Chr(34) & myName & Chr(34)
For Dates, they need to be enclosed in between #,
strSQL = "UPDATE tableName SET DateFieldName = #" & Date() & "#"
It is only numeric values that need no specific enclosures.
Dim myNumber As Long
myNumber = 55
strSQL = "UPDATE tableName SET NumberFieldName = " & myNumber
So in your query it needs to be,
CurrentDb.Execute "UPDATE CI SET D_Date = #" & Now() & "# WHERE ID = " & Id, dbSeeChanges