run time error 3464 mismatch in crietria expression - ms-access

I am trying to verify if a site selected from a cascading combobox (named cboSite) matches any of the various site names in the column "within 10" in the table named SLA. if so i want it to calculate a function i have come up with the code. The first bit of the expression is executed however if i choose a site that is not in the within 10 column in SLA then my elseif statement is not excuted.
Private Sub txtRTF_Click()
If Not ISNull (DLookup("Within10", "SLA", "Within10 = '" & Me.cboSite.Value & " '")) Then
Me.txtRTF = DateAdd("h", 2, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("10_50", "SLA", "10_50 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("h", 4, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("50_80", "SLA", "50_80 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("h", 8, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("80_100", "SLA", "80_100 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("d", 2, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("Over100", "SLA", "Over100 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("d", 10, [Date Fault Lodged])
End If
End Sub
What i am trying to achieve is very similar to when creating login access in ms access. when someone enters the username and password it checks in the table to see if the username and password are correct then allow use of forms etc.
I am not trying to check if the value is Null or empty or incorrect. I am trying to verify that the value selected is in the table SLA in the column Within10 if so execute Me.txtRTF = DateAdd("h", 2, [Date Fault Lodged])or else check in the next column in the table SLA.

Check for Null:
Private Sub txtRTF_Click()
If Not IsNull(DLookup("Within10", "SLA", "Within10 = '" & Me.cboSite.Value & "'")) Then
Me.txtRTF = "Something"
End If
End Sub
Your:
DateDiff("hm", 0, 8, [Date Fault Lodged])
doesn't make sense at all, so read up on the syntax; it is not to guess what you are trying to do with this.

Regardless of what you want to display in txtRTF, the current type mismatch error is in the IF statement: you are checking a Variant/String, which is not a Boolean. What you want is to check if the result of the lookup is empty:
If DLookup("[Within10]", "SLA", "[Within10] = '" & Me.cboSite.Value & "'") <> Empty Then
Me.txtRTF.Value = "..."
End If

Related

VB6 and MS Access, save time and date if user is true

--
I badly need your help guys. I'm stuck with these problems for a almost a month . I need to get the time and date if the user tap on the scanner.
Private Sub Command1_Click()
Adodc1.RecordSource = "select * from tbl_login where user = '" + Text1.text + "'"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "Login Failed", vbCritical + vbOKOnly, "Error"
Else
Adodc1.RecordSource = "update tbl_login set dte = '" & Label1.Caption & "' set tme '" & Label2.Caption & "' where user = '" + Text1.text + "'"
MsgBox "Login Successful!", vbExclamation + vbOKOnly, "Welcome"
Text1.text = ""
Text2.text = ""
End If
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Format(Now, "dddd mmmm dd, yyyy")
Label2.Caption = Format(Now, "hh: mm: ss ampm")
End Sub
It shows the Login Successful, but it doesn't record the time and date of the user.
Help please, help me fix this.
Simply insert date and time:
Adodc1.RecordSource = "update tbl_login set dte = Date(), tme = Time() where user = '" & Text1.text & "'"
Firstly, I would suggest always using the ampersand operator (&) for string concatenation, not the addition operator (+).
Assuming that your fields dte and tme are datetime fields, then the reason that your UPDATE query fails is because you are not providing the date & time data in an appropriate format required for SQL.
However, rather than using concatenation to insert the data into your UPDATE query, I would instead suggest parameterising the query and passing the date, time, and username to the parameters, for example:
With CurrentDb.CreateQueryDef("", "update tbl_login set tbl_login.dte = #dte, tbl_login.tme = #tme where tbl_login.user = #usr")
.Parameters(0) = Date
.Parameters(1) = Now
.Parameters(2) = Text1.Text
.Execute
End With
You can find out more about parameterised queries from this excellent answer.

MS Access DLOOKUP with text and nested DLOOKUP for criteria

I have a combobox on a form that contains search terms. The user chooses a search term and this looks up to a table containing the number X. The RVU (a number) of X is looked up in another table given the category is equal to the string 'PHYS'. I was using nested DLOOKUP statements to look up the number X and then use that number X and the string criteria to look up the RVU. Here's my code:
FH_array(0) = Val(Nz(DLookup("[RVU]", "[FORES IP Picker]", "[IP]= " & Val(Nz(DLookup("[FORES]", "[IP Number Xwalk]", "[Reference Name] = '" & Me.Ref_Name & "'"), 0))), ""))
I wasn't having luck so I broke it down to debug:
a = Val(Nz(DLookup("[FORES]", "[IP Number Xwalk]", "[Reference Name] = '" & Me.Ref_Name & "'"), 0))
Debug.Print "a:"; a 'returns value 279
aa = Val(nz(DLookup("[RVU]", "[FORES IP Picker]", "[IP] = " & a & " and [Cost Category] = 'PHYS')))
Debug.Print "aa:"; aa
I'm getting a syntax error on the line for variable aa. if I changed the code from
aa = DLookup("[RVU]", "[FORES IP Picker]", "[IP] = " & a & " and [Cost Category] = 'PHYS')
to
aa = DLookup("[RVU]", "[FORES IP Picker]", "[Cost Category] = 'PHYS'" And "[IP] = " & a)
I get a run-time error 13 type mismatch
All the variables are declared as variant and called properly. The array FH_array is sized correctly. I copied this code from another database that does the same type of nested DLOOKUP but it has only one criteria and therefore works. I can't figure out what syntax I'm missing or where the type mismatch is to get it to work.
You need a single valid string as the DLookup criteria option. Use the Immediate window to examine what you have for the criteria in the final DLookup example.
Debug.Print "[Cost Category] = 'PHYS'" And "[IP] = " & a
That is actually a "logical conjunction" of two strings. (That will make more sense if you review the Access help topic for the And Operator.)
And since you're using And with two string expressions, Access complains about type mismatch, the same as it does with this simpler example:
Debug.Print "a" And "b"
So you need to create a single valid string for the criteria option ...
a = 279
Debug.Print "[Cost Category] = 'PHYS' And [IP] = " & a
[Cost Category] = 'PHYS' And [IP] = 279
Translating that back to the last DLookup in your question ...
Dim strCriteria As String
strCriteria = "[Cost Category] = 'PHYS' And [IP] = " & a
Debug.Print strCriteria ' <- just to make sure you got what you need
aa = DLookup("[RVU]", "[FORES IP Picker]", strCriteria)

Moving Dates to Access Table Fields - Incorrect Dates Printed

I'm having some issues with moving the CurrentDate and LastDayOfMonth to a table in Access for data processing
Dim CD As Date
Dim LDOM As Date
CD = DateSerial(Year(Date), Month(Date), Day(Date))
'Format(Now(), "mm-dd-yyyy")
LDOM = DateSerial(Year(Date), Month(Date) + 1, 0)
'Add Dates
CurrentDb.Execute "UPDATE tblProcess " & _
"SET tblProcess.[CurrentDate] = " & CD
CurrentDb.Execute "UPDATE tblProcess " & _
"SET tblProcess.[DueDate] = " & LDOM
Debug.Print CD
Debug.Print LDOM
Everytime I Debug.Print - either the formula or the variable - it ALWAYS comes out correct.
But what ends up on my table for both fields is "12/30/1899" Can anyone help?
Test simply:
CurrentDb.Execute "UPDATE tblProcess" _
& " SET tblProcess.[CurrentDate] = #" & Format(CD, "yyyy-mm-dd") & "#;"
Your original code uses SQL like this:
UPDATE tblProcess SET tblProcess.[CurrentDate] = 12/03/2013
that is BAD for Access DATETIME field.
Instead we need in final for Accesss SQL string:
UPDATE tblProcess SET tblProcess.[CurrentDate] = #2013-12-03 22:00:13#;
Please stop voting up for such a small contribution, I have not said the last word, for SQL Server, we must use:
UPDATE tblProcess SET CurrentDate = '2013-12-03T22:00:13';
Although Access and SQL Server are both Microsoft of Bill Gates.

syntax error, I don't see one :S

Apparently i'm getting a syntax error, but I don't see any issues with my code!
if rs.EOF then
'User doesn't exist, create the record
query = currentDate&",'"
query = query + password & "','"
query = query + "user', '"
query = query + email & "', '"
query = query + fname & "', '"
query = query + sname & "'"
handle = add_to_database("users","jdate,password,perms,email,fname,sname",query)
response.write handle
else
response.write "Error: User already exists, please use a different email address"
end if
Here is the add_to_database function
function add_to_database(where,column,values)
'Create query
sql = "INSERT INTO " & where & " (" & column & ") VALUES (" & values & ")"
on error resume next
'Add images to database
conn.Execute sql,recaffected
if err<>0 then
add_to_database = err.description &":"&sql&"<br />"
end if
'Destroy connection
sql = ""
end function
This is the error that is written to the page
Syntax error in INSERT INTO statement.:INSERT INTO users (jdate,password,perms,email,fname,sname) VALUES (#2013/8/2#,'af453d19feb2520c8c0d30fb39ebd211','user', 'martynleeba#gmail.com', 'Martyn', 'Ball')
The users table includes a field named password. But password is a reserved word, so can confuse the db engine when it finds it in a query. Rename that field if possible. If you must keep the name as is, enclosed it in square brackets to signal the db engine it is an object name.
handle = add_to_database("users","jdate,[password],perms,email,fname,sname",query)

Invalid Argument Error: MSAccess and SQL

I am trying to access certain lines from my SQL database from MSAccess and I keep getting an Invalid Argument Error on this line:
Set rs = CurrentDb.OpenRecordset("SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = prodSelect AND EndTime is NULL AND StartTime < dateAdd('h', 3, NOW())", [dbSeeChanges])
Is something not right in this?
Private Sub cmdClockEnd_Click()
'Check if a group has been selected.
If frmChoice.value = 0 Then
MsgBox "Please select a production line."
End
End If
'Setup form for user input.
lblEnd.Visible = True
'Save end of lunch value.
lblEnd.Caption = Format(Now, "MMM/DD/YY hh:mm:ss AMPM")
'Declare database variables.
Dim dbName As DAO.Database
Dim strValuesQuery As String
Dim rs As DAO.Recordset
Dim prodSelect As String
Dim sSQL As String
Dim timeValue As String
Set dbName = CurrentDb
'Get values of Production Line.
If frmChoice.value = 1 Then
prodSelect = "L2"
ElseIf frmChoice.value = 2 Then
prodSelect = "L3"
End If
'Get the last TimeID with the following parameters.
sSQL = "SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = prodSelect AND EndTime is NULL AND StartTime < #" & DateAdd("h", 3, Now()) & "#"
Set rs = dbName.OpenRecordset(sSQL, dbSeeChanges)
strValuesQuery = _
"UPDATE tblLunchTime " & _
"SET EndTime = '" & Now & "'" & _
"WHERE TimeID = " & rs![TimeID] & " "
'Turn warning messages off.
DoCmd.SetWarnings False
'Execute Query.
DoCmd.RunSQL strValuesQuery
'Turn warning messages back on.
DoCmd.SetWarnings True
End Sub
You need to put prodSelect outside the quotes:
"WHERE ProductionID = " & prodSelect & " AND ...
It is nearly always best to say:
sSQL="SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = " & prodSelect & _
" AND EndTime is NULL AND StartTime < dateAdd('h', 3, NOW())"
''Debug.print sSQL
Set rs = CurrentDb.OpenRecordset(sSQL)
You can see the advantage in the use of Debug.Print.
AHA prodSelect is text! You need quotes!
sSQL="SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = '" & prodSelect & _
"' AND EndTime is NULL AND StartTime < dateAdd('h', 3, NOW())"
There appears to be confusion about tblLunchTime ... whether it is a native Jet/ACE table or a link to a table in another database. Please show us the output from this command:
Debug.Print CurrentDb.TableDefs("tblLunchTime").Connect
You can paste that line into the Immediate Window and press the enter key to display the response. (You can open the Immediate Window with CTRL+g keystroke combination.)
Just in case the response starts with "ODBC", suggest you try this line in your code:
Set rs = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)
Update: Now that you're past that hurdle, suggest you change your approach with the UPDATE statement. Don't turn off warnings; try something like this instead:
'Execute Query. '
CurrentDb.Execute strValuesQuery, dbFailOnError
And add an error handler to deal with any errors captured by dbFailOnError.
I think I would do the date criterion concatenation client-side, too, since it's one more thing that could go wrong:
"...StartTime < #" & DateAdd("h", 3, Now()) & "#"
I don't know that SQL Server doesn't have DateAdd() and Now() function nor that they don't behave exactly the same as in Access, but I wouldn't take the chance -- I'd do this calculation on the client instead of handing it off to the server.