views:

106

answers:

3

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
+4  A: 

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())"
Remou
That's right. I changed it and still got the error though.
BioXhazard
See edit. Check the resulting SQL string, you can even paste it into the query design window, SQL view.
Remou
What is sSQL? What should I declare it as?
BioXhazard
It is a string to hold your sql string.
Remou
I got invalid argument for Set rs = CurrentDb.OpenRecordset(sSQL, [dbSeeChanges])
BioXhazard
Did you check the immediate window (ctrl+G) for the sSQL string? You will need to uncomment Debug.Print ...
Remou
I would also get rid of [dbSeeChanges] http://msdn.microsoft.com/en-us/library/bb243019(office.12).aspx
Remou
It wouldn't let me run without having dbSeeChanges.
BioXhazard
It is in the wrong place: expression.OpenRecordset(Name, Type, Options, LockEdit). I am not sure what it sees as Type = 512
Remou
Try Set rs = CurrentDb.OpenRecordset(sSQL, ,dbSeeChanges)
HansUp
This seems to work, too: Set rs = CurrentDb.OpenRecordset(sSQL, options:=dbSeeChanges)
HansUp
@HansUp Why do this?
Remou
@HansUp why add dbSeeChanges? If it did not run without it, it is for some other reason, because it is in the wrong place. I think this is confusing the issue.
Remou
@Remou Ensure dbSeeChanges is passed as Options rather than Type.
HansUp
@HansUp It is not needed. If something is needed it is a Type, because that is what it was in as. This is just confusing the whole thing, i reckon.
Remou
@Remou In my testing CurrentDb.OpenRecorset(sSQL, dbSeeChanges) gave me the invalid argument error same as BioXhazard. The changes I suggested make my error go away.
HansUp
@HansUp Please see comments above: I would also get rid of [dbSeeChanges] msdn.microsoft.com/en-us/library/… – Remou 43 mins ago ::: It wouldn't let me run without having dbSeeChanges. – BioXhazard 38 mins ago It wouldn't let me run without having dbSeeChanges.
Remou
Like I said, this is confusing the issue. It should run without *any* additon to the SQL string.
Remou
@Remou I do not understand why dbSeeChanges should be required. However, BioXhazard claims it is. **IF** he is going to include dbSeeChanges, make sure to pass it as options rather than type.
HansUp
@HansUp It *cannot* be required, because it was in the wrong place.
Remou
I have been informed when programming Access using SQL Server via ODBC that my recordsets needed to be opened with the dbSeeChanges option. I don't know why, or what it does, but my recordsets would not work without it. I've made it a normal part of creating recordsets against SQL Server. The newer SQL Server native client may eliminate the need for it -- I don't know, because all my SQL Server projects are older versions.
David-W-Fenton
Thanks for clearing that up, David. With an ODBC-linked SQL Server table, I couldn't make dbSeeChanges work without explicitly passing a value for type, so will suggest OP try:Set rs = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)
HansUp
BioXHazard has explicitly stated in other post that he or she is using MS Access, not SQL Server.
Remou
Access says it's necessary to have when using a table with a primary key. Anyway, it still doesn't work. It can't even find dbSeeChanges when you use options:=. I'm adding all of my code to the question. Maybe there's something wrong in what I'm doing.
BioXhazard
Still got invalid argument for the line: Set rs = dbName.OpenRecordset(sSQL, dbSeeChanges)
BioXhazard
Does it make a difference if prodSelect is a string and ProductionID is nvarchar?
BioXhazard
nvarchar needs quotes, too.
Remou
Just checking. What do you mean is dbSeeChanges is in the right place?
BioXhazard
Check HansUps answer.
BioXhazard
You mean you did not read my initial comments and links?
Remou
I did. But the first time I used dbOpenDynaset my MSAccess crashed.
BioXhazard
+1  A: 

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.

David-W-Fenton
A: 

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.

HansUp
Awesome. That worked everything out. Thanks!
BioXhazard
@BioXHazard Had you read the links and comments made by me 18 hours ago, it would have been easier.
Remou
Like I said, I did. But I removed it after it crashed Access and when things started getting changed in the SQL statement. Sorry.
BioXhazard