473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Help with runtime error due to incorrect VBA SQL statement syntax

Hoping an expert can help me fix an error that I'm getting while trying to execute an SQL statement in Access Visual Basic.

Please note I have limited programming knowledge.

Here's the background of my problem:

I'm trying to create a list of the last ten records visited by the database user. As a record is visited, I want to capture in another table its [Docket_ID], a field that is located on a subform [Material Name], and the date and time the record is visited.

My ultimate goal is to create a drop down or continuous form which lists the last 10 records and the user will be able to click on one in the list and be taken back to that record. But I need to get this functionality to work to add the records to the last visited table.

I started with Alan Browne's solution to a similar problem here and tried to adapt it: http://objectmix.com/ado-dao-rdo-rds...d-records.html

This solution only captures two items and I need to capture three.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.  
  3. Dim strCurrentMaterial As String
  4. Dim strSql As String
  5.  
  6. strCurrentMaterial = Me!sbfMaterials.Form!Material_Name 'the material name
  7.  
  8. strSql = "INSERT INTO tbl_Last_Visited ( Docket_ID, Log_Date, txtMaterial_Name ) " & vbCrLf & _
  9. "SELECT " & Nz(Me.[Docket_ID], "Null") & " AS Docket_ID, Now() AS Log_Date, " & strCurrentMaterial & " AS txtMaterial_Name;"
  10. DBEngine(0)(0).Execute strSql, dbFailOnError
  11.  
  12. End Sub

I am getting a Run-time error '3075' Syntax error (missing operator) in query expression 'the name of the strCurrentMaterial varible appears here'

Hoping someone can help get me on track or offer a solution.
Dec 20 '11 #1

✓ answered by NeoPa

It looks like a string Literal. You do indeed need to use quotes (') in that case. See Quotes (') and Double-Quotes (") - Where and When to use them for more on that.

Line #9 might then be :
Expand|Select|Wrap|Line Numbers
  1. "SELECT " & Nz(Me.[Docket_ID], "Null") & " AS Docket_ID, " & _
  2.         "Now() AS Log_Date, " & _
  3.         "'" & strCurrentMaterial & "' AS txtMaterial_Name;"

4 2343
Rabbit
12,516 Expert Mod 8TB
If that's a string (as the name of the variable would seem to imply) then in the SQL string, it needs to be surrounded by quotes.
Dec 20 '11 #2
NeoPa
32,556 Expert Mod 16PB
It looks like a string Literal. You do indeed need to use quotes (') in that case. See Quotes (') and Double-Quotes (") - Where and When to use them for more on that.

Line #9 might then be :
Expand|Select|Wrap|Line Numbers
  1. "SELECT " & Nz(Me.[Docket_ID], "Null") & " AS Docket_ID, " & _
  2.         "Now() AS Log_Date, " & _
  3.         "'" & strCurrentMaterial & "' AS txtMaterial_Name;"
Dec 21 '11 #3
Rabbit and NeoPa, thank you!
The quotes/double quotes reference will come in handy.

Now it works exactly like I wanted, and I can build the form to display the last 10 records.
Dec 21 '11 #4
NeoPa
32,556 Expert Mod 16PB
Excellent news. Good for you :-)
Dec 21 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Peter | last post by:
I have a program which was working fine. In it a function works out the number of .jpg files in a specified folder. I then realised that I needed to make sure the folder exists first & added the...
6
by: Dan Roberts | last post by:
I am running some off-the-shelf software that is written in ASP, which uses JScript to generate dynamic content within HTML forms. There are several ASP pages which are partially rendering to IE,...
5
by: jwa6 | last post by:
I get an runtime error w/ this code If Me!Obj = 5100 Then runtime error 13 type mismatch what is this testing for? IE: 5100 jim
3
by: bill_hounslow | last post by:
I'm trying to transfer data from one Sql Server into a table on another, using a simple INSERT query in an Access database with links to tables on both servers (the reasons for this are complicated...
4
by: VB Programmer | last post by:
If I try ANY aspx pages on my server, even INVALID, bogus pages, I get a "Runtime Error". Any idea why? Here is the full message: Server Error in '/' Application....
12
by: Kevin Farless | last post by:
Hi all, I'm having trouble viewing any .aspx pages on my hosting company's server. The server is a new box running Windows Server 2003 default installation. I'm the first client to try using...
2
by: Massimo Soricetti | last post by:
Hello, I'm trying to fix this little class: <code> #include <ifstream> #include <iostream> #include <iomanip> using namespace std;
0
by: Terry Tang | last post by:
Hi There, We are extending Python interpreter to support special functions of our tools. What we did is to compile Python's source code (which is got from the an installation on a Linux...
4
by: news.citenet.net | last post by:
I keep getting the following error message after my web site running 2 or 3 days I share one folder with about 200 domain names Any one can help? ...
8
by: g_man | last post by:
I am trying trap Runtime error 3022 (duplicates) in the click event of a command button that closes the form. I have code in the Form_Error event that does a good job of providing a more meaningful...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.