Get Current (1 Viewer)

mizzourob

New member
Local time
Today, 02:57
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!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:57
Joined
Aug 30, 2003
Messages
36,125
You don't have a FROM clause. ;)

I'd also use the TOP predicate so only the desired record is returned.
 

isladogs

MVP / VIP
Local time
Today, 08:57
Joined
Jan 14, 2017
Messages
18,211
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 = .....
 

mizzourob

New member
Local time
Today, 02:57
Joined
Dec 19, 2016
Messages
7
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...
 

mizzourob

New member
Local time
Today, 02:57
Joined
Dec 19, 2016
Messages
7
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)
 

mizzourob

New member
Local time
Today, 02:57
Joined
Dec 19, 2016
Messages
7
I wonder if the issue has to do with the table name being GENERAL which is a reserved word?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:57
Joined
Aug 30, 2003
Messages
36,125
If so, bracketing it should work around the problem: [General]
 

Jeffr.Lipton

Registered User.
Local time
Today, 00:57
Joined
Sep 14, 2018
Messages
31
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 ;"
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:57
Joined
Aug 30, 2003
Messages
36,125
It already had single quotes around it. ;)
 

Jeffr.Lipton

Registered User.
Local time
Today, 00:57
Joined
Sep 14, 2018
Messages
31
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:57
Joined
Aug 30, 2003
Messages
36,125
The singles should work unless the text has one in it, like O'Shea. You're safer with the double.
 

mizzourob

New member
Local time
Today, 02:57
Joined
Dec 19, 2016
Messages
7
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)
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:57
Joined
Aug 30, 2003
Messages
36,125
I wouldn't expect the absence of that to cause the syntax error you were getting, but glad you got it sorted.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:57
Joined
Aug 30, 2003
Messages
36,125
That Baldy guy is an idiot, but luckily the FAQ was written by Jason. :p
 

Users who are viewing this thread

Top Bottom