Get Current

mizzourob

New member
Local time
Today, 13:48
Joined
Dec 19, 2016
Messages
7
I have a table named "General" which contains fields "CONTRACT" and "EXTENSION" I am trying to find the last entered EXTENSION when viewing any of the CONTRACT record value in the table using some VBA in a on click event button in a form that uses the General table as it's record source. The EXTENSION value is in the third column in the General data table (array element 2).

But I keep getting a 3075 error code. Here is my code...
Code:
Dim SQLstring As String
SQLstring = "SELECT * WHERE General.[CONTRACT] = '" & Me.CONTRACT& "' ORDER BY EXTENSION DESC ;"
MsgBox SQLstring
Recordset = CurrentDb.OpenRecordset(SQLstring)
MsgBox "EXTENSION = " & Recordset.Fields(2)
The first message box generates what appears to be a correct SQL string but the second message box fails to appear and I only get the 3075 error. Any thoughts would be great!
 
You don't have a FROM clause. ;)

I'd also use the TOP predicate so only the desired record is returned.
 
Your sql statement is incorrect as it doesn't include the table the data is from.
The first part should be

Code:
SELECT * FROM General WHERE Contract = .....
 
Thanks for the tip on adding the From clause. When I tried adding that I end up with a run-time error 3131 Syntax error in 'From' Clause. Still not sure what I'm missing...
 
Pro tip: We can't troubleshoot your SQL if you don't show it to us.
Mark

If I modify my code with the FROM clause as suggested and as shown below error 3131 pops up

Code:
Dim SQLstring As String
SQLstring = "SELECT * FROM General WHERE General.[CONTRACT] = '" & Me.CONTRACT& "' ORDER BY EXTENSION DESC ;"
MsgBox SQLstring
Recordset = CurrentDb.OpenRecordset(SQLstring)
MsgBox "EXTENSION = " & Recordset.Fields(2)
 
I wonder if the issue has to do with the table name being GENERAL which is a reserved word?
 
If so, bracketing it should work around the problem: [General]
 
Also, if Me.Contract is text, you need a quote character around it:
Code:
Dim Quot_Char as String
Quot_Char = chr(34)
SQLstring = "SELECT * FROM [General] WHERE [General].[CONTRACT] = '" & Quot_Char & Me.CONTRACT & Quot_Char & "' ORDER BY EXTENSION DESC ;"
 
It already had single quotes around it. ;)
 
It already had single quotes around it. ;)

You're right! I've had problems using single quotes from time to time (sometimes ACCESS seems to demand a double quote! :banghead:), so I've given up and just use a quote character. I've just found it's less likely to lead to problems.
 
The singles should work unless the text has one in it, like O'Shea. You're safer with the double.
 
Re: Get Current [Solved]

To finally solve this, I had to alter my OpenRecordset command by appended dbOpenDynaset to it.
Code:
Set Recordset = CurrentDb.OpenRecordset(SQLstring, dbOpenDynaset)

Full code is now
Code:
Dim SQLstring As String
SQLstring = "SELECT * FROM General WHERE General.[CONTRACT] = '" & Me.CONTRACT& "' ORDER BY EXTENSION DESC ;"
MsgBox SQLstring
Set Recordset = CurrentDb.OpenRecordset(SQLstring, dbOpenDynaset)
MsgBox "EXTENSION = " & Recordset.Fields(2)
 
I wouldn't expect the absence of that to cause the syntax error you were getting, but glad you got it sorted.
 
That Baldy guy is an idiot, but luckily the FAQ was written by Jason. :p
 

Users who are viewing this thread

Back
Top Bottom